prism-react-renderer

渲染高亮的 Prism 输出到 React(+ 主题和 vendored Prism)。「Renders highlighted Prism output to React (+ theming & vendored Prism)」

Github星跟蹤圖

![Maintenance Status][maintenance-image]

Why?

Maybe you need to render some extra UI with your Prismjs-highlighted code,
or maybe you'd like to manipulate what Prism renders completely,
or maybe you're just using Prism with React and are searching for an easier,
global-pollution-free way?

Then you're right where you want to be!

How?

This library tokenises code using Prism and provides a small render-props-driven
component to quickly render it out into React. This is why it even works with
React Native! It's bundled with a modified version of Prism that won't pollute
the global namespace and comes with
a couple of common language syntaxes.

(There's also an escape-hatch to use your own Prism setup, just in case)

It also comes with its own VSCode-like theming format, which means by default
you can easily drop in different themes, use the ones this library ships with, or
create new ones programmatically on the fly.

(If you just want to use your Prism CSS-file themes, that's also no problem)

Table of Contents

Installation

This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies:

# npm
npm install --save prism-react-renderer
# yarn
yarn add prism-react-renderer

This package also depends on react. Please make sure you
have those installed as well.

Usage

Try it out in the browser

import React from "react";
import { render } from "react-dom";
import Highlight, { defaultProps } from "prism-react-renderer";

const exampleCode = `
(function someDemo() {
  var test = "Hello World!";
  console.log(test);
})();

return () => <App />;
`;

render((
  <Highlight {...defaultProps} code={exampleCode} language="jsx">
    {({ className, style, tokens, getLineProps, getTokenProps }) => (
      <pre className={className} style={style}>
        {tokens.map((line, i) => (
          <div {...getLineProps({ line, key: i })}>
            {line.map((token, key) => (
              <span {...getTokenProps({ token, key })} />
            ))}
          </div>
        ))}
      </pre>
    )}
  </Highlight>,
  document.getElementById('root')
);

<Highlight /> is the only component exposed by this package, as inspired by
downshift.

It also exports a defaultProps object which for basic usage can simply be spread
onto the <Highlight /> component. It also provides some default theming.

It doesn't render anything itself, it just calls the render function and renders that.
"Use a render prop!"!
<Highlight>{highlight => <pre>/* your JSX here! */</pre>}</Highlight>

Basic Props

This is the list of props that you should probably know about. There are some
advanced props below as well.

Most of these advanced props are included in the defaultProps.

children

function({})

主要指標

概覽
名稱與所有者FormidableLabs/prism-react-renderer
主編程語言TypeScript
編程語言JavaScript (語言數: 3)
平台Web browsers
許可證MIT License
所有者活动
創建於2018-08-17 17:11:38
推送於2025-01-02 18:23:10
最后一次提交2025-01-02 12:23:08
發布數37
最新版本名稱prism-react-renderer@2.4.1 (發布於 2024-12-11 17:04:57)
第一版名稱v0.0.2 (發布於 2018-08-17 23:36:00)
用户参与
星數1.9k
關注者數35
派生數150
提交數279
已啟用問題?
問題數137
打開的問題數7
拉請求數94
打開的拉請求數3
關閉的拉請求數26
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?