prism-react-renderer

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

Github stars Tracking Chart

![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({})

Main metrics

Overview
Name With OwnerFormidableLabs/prism-react-renderer
Primary LanguageTypeScript
Program languageJavaScript (Language Count: 3)
PlatformWeb browsers
License:MIT License
所有者活动
Created At2018-08-17 17:11:38
Pushed At2025-01-02 18:23:10
Last Commit At2025-01-02 12:23:08
Release Count37
Last Release Nameprism-react-renderer@2.4.1 (Posted on 2024-12-11 17:04:57)
First Release Namev0.0.2 (Posted on 2018-08-17 23:36:00)
用户参与
Stargazers Count1.9k
Watchers Count35
Fork Count150
Commits Count279
Has Issues Enabled
Issues Count137
Issue Open Count7
Pull Requests Count94
Pull Requests Open Count3
Pull Requests Close Count26
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private