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?
已存档?
是复刻?
已锁定?
是镜像?
是私有?