LoginSignup
36
9

More than 3 years have passed since last update.

react-syntax-highlighterのstyleを見比べるサンプルページ作った

Posted at

Reactで画面上にソースコードを載せるときにシンタックスハイライトをしてくれるパッケージ「react-syntax-highlighter」のstyleを見比べる為のページを作ったので紹介。

作ったもの

こんな感じで動きます。
captured.gif

react-syntax-highlighter

react-syntax-highlighterは上記の通りソースコードのシンタックスハイライトをいい感じにしてくれるReact用のパッケージです。以下のように、languageとstyleを指定して使用します。

import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { dark } from 'react-syntax-highlighter/dist/esm/styles/prism';
const Component = () => {
  const codeString = '(num) => num + 1';
  return (
    <SyntaxHighlighter language="javascript" style={dark}>
      {codeString}
    </SyntaxHighlighter>
  );
};

languageとしてはpython、javascript, json等様々な形式に対応しているようで、また、シンタックスハイライのstyleとしてgithub風やxcode風等かなり多くのstyleがあります。今回は言語ごとにスタイルをいっぺんに見比べられるサンプルページを作りました。

その他

このサンプルページはGithub Pagesで公開しています。
build前のソースコードはこちらです。

36
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
9