diff --git a/.changeset/khaki-frogs-behave.md b/.changeset/khaki-frogs-behave.md deleted file mode 100644 index f1f3bc7..0000000 --- a/.changeset/khaki-frogs-behave.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"prism-react-renderer": minor ---- - -Added oneDark and oneLight themes diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml new file mode 100644 index 0000000..058e21e --- /dev/null +++ b/.github/actions/setup/action.yml @@ -0,0 +1,23 @@ +name: Setup +description: Setup Build Step +inputs: + node-version: + required: true + default: '18.x' + +runs: + using: "composite" + steps: + - uses: pnpm/action-setup@v3 + with: + version: 8.2.0 + + - name: Use Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ inputs.node-version }} + cache: 'pnpm' + + - name: Install dependencies + shell: bash + run: pnpm install diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 73c5145..9148a71 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: Prism React Renderer Release Workflow +name: Release Workflow on: push: @@ -18,26 +18,8 @@ jobs: pull-requests: write steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 - with: - node-version: 18 - - - uses: pnpm/action-setup@v2 - with: - version: 8.2.0 - - name: Get pnpm store directory - id: pnpm-cache - shell: bash - run: echo "STORE_PATH=$(pnpm store path)">> $GITHUB_OUTPUT - - uses: actions/cache@v3 - name: Setup pnpm cache - with: - path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} - key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} - restore-keys: ${{ runner.os }}-pnpm-store- - - name: Install dependencies - run: pnpm install + - uses: actions/checkout@v4 + - uses: ./.github/actions/setup - name: Build packages run: pnpm run build diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index df01631..cc8179b 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -5,37 +5,29 @@ on: branches: - master pull_request: - workflow_dispatch: + branches: + - master jobs: static-analysis: name: 'Lint and Type-check' runs-on: ubuntu-latest + steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 - with: - node-version: 18 - - - uses: pnpm/action-setup@v2 - with: - version: 8.2.0 - - name: Get pnpm store directory - id: pnpm-cache - shell: bash - run: echo "STORE_PATH=$(pnpm store path)">> $GITHUB_OUTPUT - - uses: actions/cache@v3 - name: Setup pnpm cache - with: - path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} - key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} - restore-keys: ${{ runner.os }}-pnpm-store- - - name: Install dependencies - run: pnpm install + - uses: actions/checkout@v4 + - uses: ./.github/actions/setup - name: Build library run: pnpm run build + - name: Type Check run: pnpm run typecheck + - name: Lint run: pnpm run lint + + - name: Generate language definitions + run: pnpm run build:languages + + - name: Unit Test + run: pnpm run test diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml deleted file mode 100644 index 8284cee..0000000 --- a/.github/workflows/unit-test.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: Unit Test - -on: - push: - branches: - - master - pull_request: - workflow_dispatch: - -jobs: - matrix: - name: 'Node ${{ matrix.node }}' - runs-on: ubuntu-latest - strategy: - matrix: - node: [16, 18] - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node }} - - - uses: pnpm/action-setup@v2 - with: - version: 8.2.0 - - name: Get pnpm store directory - id: pnpm-cache - shell: bash - run: echo "STORE_PATH=$(pnpm store path)">> $GITHUB_OUTPUT - - uses: actions/cache@v3 - name: Setup pnpm cache - with: - path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} - key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} - restore-keys: ${{ runner.os }}-pnpm-store- - - name: Install dependencies - run: pnpm install - - - name: Generate language definitions - run: pnpm run build:languages - - - name: Unit Test - run: pnpm run test - diff --git a/LICENSE b/LICENSE index 0d5e37e..8915d38 100755 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Formidable +Copyright (c) 2018 Nearform Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 6a38fc8..c9f1ca0 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,26 @@ - - Prism React Renderer — Formidable, We build the modern web + + Prism React Renderer

- A lean Prism highlighter component for React
- Comes with everything to render Prismjs highlighted code directly to React (Native) elements, global-pollution-free! + A lean Prism highlighter component for React

-[![Maintenance Status][maintenance-image]](#maintenance-status) -## Why? +

+ + + + Maintenance Status + +

-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? +

+ Comes with everything to render Prismjs syntax highlighted code directly in React & React Native! +

-Then you're right where you want to be! +## Introduction -## How? +Prism React Renderer powers syntax highlighting in the amazing [Docusaurus](https://docusaurus.io/) framework and many others. 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 @@ -36,8 +39,6 @@ _(If you just want to use your Prism CSS-file themes, that's also no problem)_ ## Table of Contents - - - [Installation](#installation) - [Usage](#usage) @@ -54,7 +55,19 @@ _(If you just want to use your Prism CSS-file themes, that's also no problem)_ - [prop getters](#prop-getters) - [`getLineProps`](#getlineprops) - [`getTokenProps`](#gettokenprops) +- [Utility Functions](#utility-functions) + - [`useTokenize`](#usetokenize) + - [`normalizeTokens`](#normalizetokens) - [Theming](#theming) + - [Using a built-in theme](#using-a-built-in-theme) + - [Providing a CSS based theme](#providing-a-css-based-theme) +- [Upgrade](#upgrade) + - [Change module imports](#change-module-imports) + - [Change theme imports](#change-theme-imports) + - [Check language support](#check-language-support) + - [Add language component](#add-language-component) +- [Development](#development) + - [Local Demo](#local-demo) - [LICENSE](#license) - [Maintenance Status](#maintenance-status) @@ -77,7 +90,8 @@ pnpm add prism-react-renderer > Prism React Renderer has a peer dependency on `react` ### Usage -Prism React Renderer has a named export for the `` component along with `themes`. To see Prism React Render in action with base styling check out `packages/demo` or run `pnpm run start:demo` from the root of this repository. + +Prism React Renderer has a named export for the `` component along with `themes`. To see Prism React Render in action with base styling, clone the repo and follow the [steps for local development](#development). ```tsx import React from "react" @@ -118,14 +132,18 @@ export const App = () => ( ) -ReactDOM.createRoot(document.getElementById("root") as HTMLElement) - .render() - +ReactDOM + .createRoot(document.getElementById("root") as HTMLElement) + .render() ``` ### Custom Language Support -By default `prism-react-renderer` only includes a [base set of languages](https://github.com/FormidableLabs/prism-react-renderer/blob/c914fdea48625ba59c8022174bb3df1ed802ce4d/packages/generate-prism-languages/index.ts#L9-L23) that Prism supports. **Depending on your app's build system you may need to `await` the `import` or use `require` to ensure `window.Prism` exists before importing the custom languages.** You can add support for more by including their definitions from the main `prismjs` package: +By default `prism-react-renderer` only includes a [base set of languages](https://github.com/FormidableLabs/prism-react-renderer/blob/master/packages/generate-prism-languages/index.ts#L9-L23) that Prism supports. + +> _Note_: Some languages (such as Javascript) are part of the bundle of other languages + +**Depending on your app's build system you may need to `await` the `import` or use `require` to ensure `window.Prism` exists before importing the custom languages.** You can add support for more by including their definitions from the main `prismjs` package: ```js import { Highlight, Prism } from "prism-react-renderer"; @@ -253,7 +271,7 @@ advisable. #### `getLineProps` You need to add a `line` property (type: `Token[]`) to the object you're passing to -`getLineProps`; It's also advisable to add a `key`. +`getLineProps`. This getter will return you props to spread onto your line elements (typically `
s`). @@ -266,7 +284,7 @@ The `className` will always contain `.token-line`. #### `getTokenProps` You need to add a `token` property (type: `Token`) to the object you're passing to -`getTokenProps`; It's also advisable to add a `key`. +`getTokenProps`. This getter will return you props to spread onto your token elements (typically `s`). @@ -277,14 +295,50 @@ to the input. The `className` will always contain `.token`. This also provides full compatibility with your old Prism CSS-file themes. +## Utility Functions + +### `useTokenize` + +> `(options: TokenizeOptions) => Token[][]` + +```ts +type TokenizeOptions = { + prism: PrismLib + code: string + grammar?: PrismGrammar + language: Language +} + +``` + +This is a React hook that tokenizes code using Prism. It returns an array of tokens that can be rendered using the built-in `` component or your own custom component. It uses `normalizeTokens` internally to convert the tokens into a shape that can be rendered. + +- `prism: PrismLib`: the Prism library to use for tokenization. This can be the vendored version of Prism that is included with `prism-react-renderer` or a custom version of Prism that you have configured. + +- `code: string`: a string containing the code to tokenize. +- `grammar?: PrismGrammar`: a Prism grammar object to use for tokenization. If this is omitted, the tokens will just be normalized. A grammar can be obtained from `Prism.languages` or by importing a language from `prismjs/components/`. +- `language: Language`: the language to use for tokenization. This should be a language that Prism supports. + +### `normalizeTokens` + +> `(tokens: (PrismToken | string)[]) => Token[][]` + +Takes an array of Prism’s tokens and groups them by line, converting strings into tokens. Tokens can become recursive in some cases which means that their types are concatenated. Plain-string tokens however are always of type `plain`. + +- `PrismToken` is an internal alias for `Token` exported by `prismjs` and is defined [here](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/prismjs/index.d.ts#L347). + +- `Token` is an internal object that represents a slice of tokenized content for Prism with three properties: + - `types: string[]`: an array of types that indicate the purpose and styling of a piece of text + - `content: string`: the content of the token + - `empty: boolean`: a flag indicating whether the token is empty or not. + ## Theming +### Using a built-in theme + The `defaultProps` you'd typically apply in a basic use-case, contain a default theme. This theme is [vsDark](./packages/prism-react-renderer/src/themes/vsDark.ts). -While all `className`s are provided with ``, so that you could use your good -old Prism CSS-file themes, you can also choose to use `prism-react-renderer`'s themes like so: - ```jsx import { Highlight, themes } from 'prism-react-renderer'; @@ -293,15 +347,13 @@ import { Highlight, themes } from 'prism-react-renderer'; These themes are JSON-based and are heavily inspired by VSCode's theme format. -Their syntax, expressed in Flow looks like the following: - -```js -{ - plain: StyleObj, +```ts +export type PrismTheme = { + plain: PrismThemeEntry styles: Array<{ - types: string[], - languages?: string[], - style: StyleObj + types: string[] + style: PrismThemeEntry + languages?: Language[] }> } ``` @@ -320,13 +372,105 @@ property limits styles to highlighted languages. When converting a Prism CSS theme it's mostly just necessary to use classes as `types` and convert the declarations to object-style-syntax and put them on `style`. +### Providing a CSS based theme + +In order to use a CSS based theme like the ones from [PrismJS](https://github.com/PrismJS/prism-themes), you need to disable the built in theme. + +```ts +const emptyTheme = { plain: {}, styles: [] }; + + +``` + +## Upgrade + +If you are migrating from v1.x to v2.x, follow these steps + +### Change module imports + +```diff +- import Highlight, { defaultProps } from "prism-react-renderer"; ++ import { Highlight } from "prism-react-renderer" + +const Content = ( +- ++ +``` + +### Change theme imports + +```diff +- const theme = require('prism-react-renderer/themes/github') ++ const theme = require('prism-react-renderer').themes.github +``` + +### Check language support + +> By default prism-react-renderer only includes a base set of languages that Prism supports. Depending on your app's build system you may need to await the import or use require to ensure window.Prism exists before importing the custom languages. + +See: https://github.com/FormidableLabs/prism-react-renderer#custom-language-support + +Install prismjs (if not available yet): + +``` +# npm +npm install --save prismjs +# yarn +yarn add prismjs +# pnpm +pnpm add prismjs +``` + +### Add language component + +If the language is not already bundled in the above, you can add additional languages with the following code: + +``` +import { Highlight, Prism } from "prism-react-renderer"; + +(typeof global !== "undefined" ? global : window).Prism = Prism +await import("prismjs/components/prism-applescript") +/** or **/ +require("prismjs/components/prism-applescript") +``` + +## Development + +Local development setup can be run with the following commands running Node 18.x. This project uses corepack to specify its package manager version and you should have it enabled locally using `corepack enable`. + +``` +$ pnpm install +$ pnpm build +$ pnpm test +``` + +### Local Demo + +To run the local demo, ensure you have first run `pnpm build`, then run `pnpm start:demo` and open the provided URL to the demo site in your terminal. + +``` +$ pnpm build +$ pnpm start:demo +``` + +The workspace projects are linked, so changes can be hot reloaded during development by running multiple terminals + +``` +// terminal 1 +$ pnpm build:watch +``` + +``` +// terminal 2 +$ pnpm start:demo +``` + ## LICENSE MIT ## Maintenance Status -**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome. +**Active:** Nearform is actively working on this project, and we expect to continue work for the foreseeable future. Bug reports, feature requests and pull requests are welcome. [maintenance-image]: https://img.shields.io/badge/maintenance-active-green.svg - diff --git a/package.json b/package.json index 2a43ff6..44f6976 100755 --- a/package.json +++ b/package.json @@ -50,5 +50,6 @@ "patchedDependencies": { "prismjs@1.29.0": "patches/prismjs@1.29.0.patch" } - } + }, + "packageManager": "pnpm@8.2.0+sha512.7f6fda6e5e86c9cc4b815650b56036cc124a31772fd8bf3a1c6470278aa74b4da05732e0b457a00b6e6a58a16d52e9c263be06530c6ad80ef2180244c8eb8262" } diff --git a/packages/demo/index.html b/packages/demo/index.html index d42d119..c20c08d 100644 --- a/packages/demo/index.html +++ b/packages/demo/index.html @@ -2,10 +2,10 @@ - + - Formidable – Prism React Renderer Demo + Prism React Renderer Demo

Formidable – Prism React Renderer Demo

diff --git a/packages/demo/public/formidable-icon.svg b/packages/demo/public/formidable-icon.svg deleted file mode 100644 index b1beef1..0000000 --- a/packages/demo/public/formidable-icon.svg +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - diff --git a/packages/demo/public/formidable-wordmark.svg b/packages/demo/public/formidable-wordmark.svg deleted file mode 100644 index 8c81362..0000000 --- a/packages/demo/public/formidable-wordmark.svg +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/packages/demo/public/nearform-icon.svg b/packages/demo/public/nearform-icon.svg new file mode 100644 index 0000000..4e95a02 --- /dev/null +++ b/packages/demo/public/nearform-icon.svg @@ -0,0 +1,4 @@ + + + + diff --git a/packages/demo/public/nearform-logo-white.svg b/packages/demo/public/nearform-logo-white.svg new file mode 100644 index 0000000..cd49ab3 --- /dev/null +++ b/packages/demo/public/nearform-logo-white.svg @@ -0,0 +1,4 @@ + + + + diff --git a/packages/demo/src/App.tsx b/packages/demo/src/App.tsx index 957fb9e..372ccec 100644 --- a/packages/demo/src/App.tsx +++ b/packages/demo/src/App.tsx @@ -26,9 +26,9 @@ function App() {
Formidable - Formidable + Nearform + @@ -108,6 +112,33 @@ end if `, }, + ["Python"]: { + language: "python", + code: ` +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +from sklearn.tree import DecisionTreeClassifier +from sklearn.metrics import accuracy_score, classification_report + +iris = load_iris() +X = iris.data +y = iris.target + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) + +clf = DecisionTreeClassifier(random_state=42) +clf.fit(X_train, y_train) + +y_pred = clf.predict(X_test) + +accuracy = accuracy_score(y_test, y_pred) +report = classification_report(y_test, y_pred) +print(f"Accuracy: {accuracy}") +print("Classification Report:\\n", report) +print("Feature Importances:", clf.feature_importances_) + `, + }, + ["Rust"]: { language: "rust", code: ` @@ -175,4 +206,31 @@ impl GroceryItem { } `, }, + ["JSON"]: { + language: "json", + code: ` + { + "id": 1, + "name": "John Doe", + "email": "johndoe@example.com", + "address": { + "street": "123 Main St", + "city": "San Diego", + "state": "CA", + "zip": "12345" + }, + "phoneNumbers": [ + { + "type": "home", + "number": "555-123-4567" + }, + { + "type": "work", + "number": "555-901-2345" + } + ], + "interests": ["reading", "hiking", "coding"] + } + `, + }, } diff --git a/packages/generate-prism-languages/index.ts b/packages/generate-prism-languages/index.ts index 9f3de8d..ca3b883 100644 --- a/packages/generate-prism-languages/index.ts +++ b/packages/generate-prism-languages/index.ts @@ -7,6 +7,7 @@ import { languages as prismLanguages } from "prismjs/components" import uglify from "uglify-js" export const languagesToBundle = [ + "markup", "jsx", "tsx", "swift", @@ -20,7 +21,8 @@ export const languagesToBundle = [ "go", "cpp", "markdown", - "html", + "python", + "json", ] /** @@ -88,10 +90,7 @@ const main = async () => { await addLanguageToOutput(language) } - console.info( - pc.bold(pc.bgYellow(pc.black("Formidable Prism React Renderer"))), - "\n" - ) + console.info(pc.bold(pc.bgYellow(pc.black("Prism React Renderer"))), "\n") console.info( pc.bgBlue(`Generated TypeScript output at:`), pc.cyan(outputPath) diff --git a/packages/prism-react-renderer/CHANGELOG.md b/packages/prism-react-renderer/CHANGELOG.md index d4d358d..474a01f 100644 --- a/packages/prism-react-renderer/CHANGELOG.md +++ b/packages/prism-react-renderer/CHANGELOG.md @@ -1,5 +1,52 @@ # prism-react-renderer +## 2.4.1 + +### Patch Changes + +- Remove client side hooks + ([#252](https://github.com/FormidableLabs/prism-react-renderer/pull/252)) + +## 2.4.0 + +### Minor Changes + +- 'Add JSON as default supported language' + ([#247](https://github.com/FormidableLabs/prism-react-renderer/pull/247)) + +## 2.3.1 + +### Patch Changes + +- Export `normalizeTokens` and `useTokenize` utility functions. + ([#237](https://github.com/FormidableLabs/prism-react-renderer/pull/237)) + +## 2.3.0 + +### Minor Changes + +- Upgrade clsx to v2.0.0. + ([#232](https://github.com/FormidableLabs/prism-react-renderer/pull/232)) + +## 2.2.0 + +### Minor Changes + +- Add Python support. + ([#226](https://github.com/FormidableLabs/prism-react-renderer/pull/226)) + +### Patch Changes + +- Fix html language preset by using markup instead. + ([#230](https://github.com/FormidableLabs/prism-react-renderer/pull/230)) + +## 2.1.0 + +### Minor Changes + +- Added oneDark and oneLight themes + ([#224](https://github.com/FormidableLabs/prism-react-renderer/pull/224)) + ## 2.0.6 ### Patch Changes diff --git a/packages/prism-react-renderer/package.json b/packages/prism-react-renderer/package.json index 7add180..de58e2b 100755 --- a/packages/prism-react-renderer/package.json +++ b/packages/prism-react-renderer/package.json @@ -1,6 +1,6 @@ { "name": "prism-react-renderer", - "version": "2.0.6", + "version": "2.4.1", "description": "Renders highlighted Prism output using React", "sideEffects": true, "main": "dist/index.js", @@ -51,7 +51,7 @@ }, "dependencies": { "@types/prismjs": "^1.26.0", - "clsx": "^1.2.1" + "clsx": "^2.0.0" }, "publishConfig": { "provenance": true diff --git a/packages/prism-react-renderer/src/components/highlight.ts b/packages/prism-react-renderer/src/components/highlight.ts index 3c6d5d5..3d23ddc 100644 --- a/packages/prism-react-renderer/src/components/highlight.ts +++ b/packages/prism-react-renderer/src/components/highlight.ts @@ -1,8 +1,8 @@ import { InternalHighlightProps } from "../types" -import { useThemeDictionary } from "./useThemeDictionary" import { useGetLineProps } from "./useGetLineProps" import { useGetTokenProps } from "./useGetTokenProps" import { useTokenize } from "./useTokenize" +import themeToDict from "../utils/themeToDict" export const Highlight = ({ children, @@ -12,7 +12,7 @@ export const Highlight = ({ prism, }: InternalHighlightProps) => { const language = _language.toLowerCase() - const themeDictionary = useThemeDictionary(language, theme) + const themeDictionary = themeToDict(theme, language) const getLineProps = useGetLineProps(themeDictionary) const getTokenProps = useGetTokenProps(themeDictionary) const grammar = prism.languages[language] diff --git a/packages/prism-react-renderer/src/components/useThemeDictionary.ts b/packages/prism-react-renderer/src/components/useThemeDictionary.ts deleted file mode 100644 index fd6746f..0000000 --- a/packages/prism-react-renderer/src/components/useThemeDictionary.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Language, PrismTheme } from "../types" -import { useEffect, useRef, useState } from "react" -import themeToDict, { ThemeDict } from "../utils/themeToDict" - -export const useThemeDictionary = (language: Language, theme: PrismTheme) => { - const [themeDictionary, setThemeDictionary] = useState( - themeToDict(theme, language) - ) - const previousTheme = useRef() - const previousLanguage = useRef() - - useEffect(() => { - if ( - theme !== previousTheme.current || - language !== previousLanguage.current - ) { - previousTheme.current = theme - previousLanguage.current = language - setThemeDictionary(themeToDict(theme, language)) - } - }, [language, theme]) - - return themeDictionary -} diff --git a/packages/prism-react-renderer/src/components/useTokenize.ts b/packages/prism-react-renderer/src/components/useTokenize.ts index 75901b9..7fac963 100644 --- a/packages/prism-react-renderer/src/components/useTokenize.ts +++ b/packages/prism-react-renderer/src/components/useTokenize.ts @@ -1,6 +1,6 @@ import { EnvConfig, Language, PrismGrammar, PrismLib } from "../types" import normalizeTokens from "../utils/normalizeTokens" -import { useMemo, useRef } from "react" +import { useMemo } from "react" type Options = { prism: PrismLib @@ -10,7 +10,6 @@ type Options = { } export const useTokenize = ({ prism, code, grammar, language }: Options) => { - const prismRef = useRef(prism) return useMemo(() => { if (grammar == null) return normalizeTokens([code]) @@ -21,9 +20,15 @@ export const useTokenize = ({ prism, code, grammar, language }: Options) => { tokens: [], } - prismRef.current.hooks.run("before-tokenize", prismConfig) - prismConfig.tokens = prismRef.current.tokenize(code, grammar) - prismRef.current.hooks.run("after-tokenize", prismConfig) + prism.hooks.run("before-tokenize", prismConfig) + prismConfig.tokens = prism.tokenize(code, grammar) + prism.hooks.run("after-tokenize", prismConfig) return normalizeTokens(prismConfig.tokens) - }, [code, grammar, language]) + }, [ + code, + grammar, + language, + // prism is a stable import + prism, + ]) } diff --git a/packages/prism-react-renderer/src/index.ts b/packages/prism-react-renderer/src/index.ts index 6def32a..60b9ba6 100644 --- a/packages/prism-react-renderer/src/index.ts +++ b/packages/prism-react-renderer/src/index.ts @@ -3,6 +3,8 @@ import * as themes from "./themes" import { createElement } from "react" import { Highlight as InternalHighlight } from "./components/highlight" import { HighlightProps, PrismLib } from "./types" +import normalizeTokens from "./utils/normalizeTokens" +import { useTokenize } from "./components/useTokenize" export * from "./types" /** @@ -17,4 +19,5 @@ const Highlight = (props: HighlightProps) => code: props.code, language: props.language, }) -export { Highlight, Prism, themes } + +export { Highlight, Prism, themes, normalizeTokens, useTokenize } diff --git a/packages/prism-react-renderer/src/themes/gruvboxMaterialDark.ts b/packages/prism-react-renderer/src/themes/gruvboxMaterialDark.ts new file mode 100644 index 0000000..64d120f --- /dev/null +++ b/packages/prism-react-renderer/src/themes/gruvboxMaterialDark.ts @@ -0,0 +1,79 @@ +// Gruvbox Material (dark) +// Author: Sainnhe Park (https://github.com/sainnhe) +// https://github.com/sainnhe/gruvbox-material +import type { PrismTheme } from "../types" +const theme: PrismTheme = { + plain: { + color: "#ebdbb2", + backgroundColor: "#292828", + }, + styles: [ + { + types: [ + "imports", + "class-name", + "maybe-class-name", + "constant", + "doctype", + "builtin", + "function", + ], + style: { + color: "#d8a657", + }, + }, + { + types: ["property-access"], + style: { + color: "#7daea3", + }, + }, + { + types: ["tag"], + style: { + color: "#e78a4e", + }, + }, + { + types: ["attr-name", "char", "url", "regex"], + style: { + color: "#a9b665", + }, + }, + { + types: ["attr-value", "string"], + style: { + color: "#89b482", + }, + }, + { + types: ["comment", "prolog", "cdata", "operator", "inserted"], + style: { + color: "#a89984", + }, + }, + { + types: [ + "delimiter", + "boolean", + "keyword", + "selector", + "important", + "atrule", + "property", + "variable", + "deleted", + ], + style: { + color: "#ea6962", + }, + }, + { + types: ["entity", "number", "symbol"], + style: { + color: "#d3869b", + }, + }, + ], +} +export default theme diff --git a/packages/prism-react-renderer/src/themes/gruvboxMaterialLight.ts b/packages/prism-react-renderer/src/themes/gruvboxMaterialLight.ts new file mode 100644 index 0000000..ae544f6 --- /dev/null +++ b/packages/prism-react-renderer/src/themes/gruvboxMaterialLight.ts @@ -0,0 +1,78 @@ +// Gruvbox Material (light) +// Author: Sainnhe Park (https://github.com/sainnhe) +// https://github.com/sainnhe/gruvbox-material +import type { PrismTheme } from "../types" +const theme: PrismTheme = { + plain: { + color: "#654735", + backgroundColor: "#f9f5d7", + }, + styles: [ + { + types: [ + "delimiter", + "boolean", + "keyword", + "selector", + "important", + "atrule", + "property", + "variable", + "deleted", + ], + style: { + color: "#af2528", + }, + }, + { + types: [ + "imports", + "class-name", + "maybe-class-name", + "constant", + "doctype", + "builtin", + ], + style: { + color: "#b4730e", + }, + }, + { + types: ["string", "attr-value"], + style: { + color: "#477a5b", + }, + }, + { + types: ["property-access"], + style: { + color: "#266b79", + }, + }, + { + types: ["function", "attr-name", "char", "url"], + style: { + color: "#72761e", + }, + }, + { + types: ["tag"], + style: { + color: "#b94c07", + }, + }, + { + types: ["comment", "prolog", "cdata", "operator", "inserted"], + style: { + color: "#a89984", + }, + }, + { + types: ["entity", "number", "symbol"], + style: { + color: "#924f79", + }, + }, + ], +} +export default theme diff --git a/packages/prism-react-renderer/src/themes/index.ts b/packages/prism-react-renderer/src/themes/index.ts index 827549e..e94ef1f 100644 --- a/packages/prism-react-renderer/src/themes/index.ts +++ b/packages/prism-react-renderer/src/themes/index.ts @@ -16,3 +16,5 @@ export { default as jettwaveDark } from "./jettwaveDark" export { default as jettwaveLight } from "./jettwaveLight" export { default as oneDark } from "./oneDark" export { default as oneLight } from "./oneLight" +export { default as gruvboxMaterialDark } from "./gruvboxMaterialDark" +export { default as gruvboxMaterialLight } from "./gruvboxMaterialLight" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 59d361b..01ace2f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -183,8 +183,8 @@ importers: specifier: ^1.26.0 version: 1.26.0 clsx: - specifier: ^1.2.1 - version: 1.2.1 + specifier: ^2.0.0 + version: 2.0.0 devDependencies: '@babel/core': specifier: '*' @@ -3457,6 +3457,11 @@ packages: engines: {node: '>=6'} dev: false + /clsx@2.0.0: + resolution: {integrity: sha512-rQ1+kcj+ttHG0MKVGBUXwayCCF1oh39BF5COIpRzuCEv8Mwjv0XucrI2ExNTOn9IlLifGClWQcU9BrZORvtw6Q==} + engines: {node: '>=6'} + dev: false + /codegen.macro@4.1.0: resolution: {integrity: sha512-T0A8vdX9SkhT00cd7nWQwlg8CT2qQwp1o2+ZD7Wqx38ABK0kJvhk/IJTjNgnE1W35dE98+OzLr1zluVREz5bPg==} engines: {node: '>=10'} @@ -4895,6 +4900,7 @@ packages: /is-number@7.0.0: resolution: {integrity: sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==} engines: {node: '>=0.12.0'} + requiresBuild: true dev: true /is-path-inside@3.0.3: @@ -6817,6 +6823,7 @@ packages: /to-regex-range@5.0.1: resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} engines: {node: '>=8.0'} + requiresBuild: true dependencies: is-number: 7.0.0 dev: true diff --git a/prism-react-renderer-Hero.png b/prism-react-renderer-Hero.png deleted file mode 100644 index a1f3bcc..0000000 Binary files a/prism-react-renderer-Hero.png and /dev/null differ

AltStyle によって変換されたページ (->オリジナル) /