Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade syntax highlighting dependencies, prism-react-renderer to v2, react-live to v4 #9316

Merged
merged 25 commits into from Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0b713f0
chore: upgrade `prism-react-renderer` to v2
harryzcy Sep 17, 2023
549fbb3
chore: update usage of `prism-react-renderer` api
harryzcy Sep 17, 2023
f174bd7
fix(website): use new `prism-react-renderer` api
harryzcy Sep 17, 2023
37ea8b0
fix: update prism import path
harryzcy Sep 17, 2023
51d65df
fix: update function type and use prism-react-renderer exposed types
harryzcy Sep 17, 2023
de2e168
fix: bug with prism-react-renderer
harryzcy Sep 17, 2023
1ae1629
fix: revert version in examples
harryzcy Sep 19, 2023
42ae5fe
fix: revert less restrictive typing
harryzcy Sep 19, 2023
f7d1336
fix: typing for `PrismThemeEntry`
harryzcy Sep 19, 2023
59a8c79
chore: bump `react-live`
harryzcy Sep 19, 2023
05cbb63
fix: typing errors caused by `react-live`
harryzcy Sep 19, 2023
9e47fa0
docs: update website on theme usage
harryzcy Sep 19, 2023
56fbd1b
fix: missing closing quote
harryzcy Sep 19, 2023
000ee6d
chore: upgrade react-live again
harryzcy Sep 19, 2023
f2874aa
fix: eslint rule that doesn't need to be disabled
harryzcy Sep 20, 2023
3763ed5
fix: pages build issue
harryzcy Sep 25, 2023
5f7abda
chore: Merge branch 'main' into prism-react-renderer-v2
harryzcy Sep 25, 2023
7b603e6
fix: error TS2578: Unused "@ts-expect-error" directive
harryzcy Sep 25, 2023
f4cac9e
fix: ts-expect-error issue again
harryzcy Sep 25, 2023
df94ec3
Merge branch 'main' into prism-react-renderer-v2
slorber Oct 6, 2023
e062e62
regen lockfile
slorber Oct 6, 2023
4140168
prism-react-renderer 2.1.0
slorber Oct 6, 2023
6abdf22
Use vendored prism, remove prism props
slorber Oct 6, 2023
00c4947
Add missing prism languages
slorber Oct 6, 2023
bcc6bdf
add scss language
slorber Oct 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,7 +19,7 @@
"@docusaurus/preset-classic": "3.0.0-beta.0",
"@mdx-js/react": "^2.3.0",
"clsx": "^1.2.1",
"prism-react-renderer": "^1.3.5",
"prism-react-renderer": "^2.1.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
Expand Down
Expand Up @@ -4,8 +4,10 @@
// There are various equivalent ways to declare your Docusaurus config.
// See: https://docusaurus.io/docs/api/docusaurus-config

const lightCodeTheme = require('prism-react-renderer/themes/github');
const darkCodeTheme = require('prism-react-renderer/themes/dracula');
const {themes} = require('prism-react-renderer');

const lightCodeTheme = themes.github;
const darkCodeTheme = themes.dracula;

/** @type {import('@docusaurus/types').Config} */
const config = {
Expand Down
Expand Up @@ -18,7 +18,7 @@
"@docusaurus/preset-classic": "3.0.0-beta.0",
"@mdx-js/react": "^2.3.0",
"clsx": "^1.2.1",
"prism-react-renderer": "^1.3.5",
"prism-react-renderer": "^2.1.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-classic/package.json
Expand Up @@ -39,7 +39,7 @@
"lodash": "^4.17.21",
"nprogress": "^0.2.0",
"postcss": "^8.4.26",
"prism-react-renderer": "^1.3.5",
"prism-react-renderer": "^2.1.0",
"prismjs": "^1.29.0",
"react-router-dom": "^5.3.4",
"rtlcss": "^4.1.0",
Expand Down
Expand Up @@ -11,8 +11,7 @@ import {
normalizeThemeConfig,
normalizePluginOptions,
} from '@docusaurus/utils-validation';
import theme from 'prism-react-renderer/themes/github';
import darkTheme from 'prism-react-renderer/themes/dracula';
import {themes} from 'prism-react-renderer';
import {ThemeConfigSchema, DEFAULT_CONFIG, validateOptions} from '../options';
import type {Options, PluginOptions} from '@docusaurus/theme-classic';
import type {ThemeConfig} from '@docusaurus/theme-common';
Expand All @@ -36,8 +35,8 @@ describe('themeConfig', () => {
it('accepts valid theme config', () => {
const userConfig = {
prism: {
theme,
darkTheme,
theme: themes.github,
darkTheme: themes.dracula,
defaultLanguage: 'javaSCRIPT',
additionalLanguages: ['koTLin', 'jaVa'],
magicComments: [
Expand Down Expand Up @@ -579,7 +578,7 @@ describe('themeConfig', () => {
const prismConfig = {
prism: {
additionalLanguages: ['kotlin', 'java'],
theme: darkTheme,
theme: themes.dracula,
magicComments: [],
},
};
Expand All @@ -590,7 +589,7 @@ describe('themeConfig', () => {
const prismConfig2 = {
prism: {
additionalLanguages: [],
theme: darkTheme,
theme: themes.dracula,
magicComments: [
{
className: 'a',
Expand All @@ -606,7 +605,7 @@ describe('themeConfig', () => {
const prismConfig3 = {
prism: {
additionalLanguages: [],
theme: darkTheme,
theme: themes.dracula,
magicComments: [
{
className: 'a',
Expand Down
13 changes: 0 additions & 13 deletions packages/docusaurus-theme-classic/src/deps.d.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/docusaurus-theme-classic/src/options.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import defaultPrismTheme from 'prism-react-renderer/themes/palenight';
import {themes} from 'prism-react-renderer';
import {Joi, URISchema} from '@docusaurus/utils-validation';
import type {Options, PluginOptions} from '@docusaurus/theme-classic';
import type {ThemeConfig} from '@docusaurus/theme-common';
Expand All @@ -14,6 +14,7 @@ import type {
OptionValidationContext,
} from '@docusaurus/types';

const defaultPrismTheme = themes.palenight;
const DEFAULT_DOCS_CONFIG: ThemeConfig['docs'] = {
versionPersistence: 'localStorage',
sidebar: {
Expand Down
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import Prism from 'prism-react-renderer/prism';
import {Prism} from 'prism-react-renderer';
import prismIncludeLanguages from '@theme/prism-include-languages';

prismIncludeLanguages(Prism);
21 changes: 9 additions & 12 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Expand Up @@ -400,23 +400,20 @@ declare module '@theme/CodeBlock/Content/String' {
}

declare module '@theme/CodeBlock/Line' {
import type {ComponentProps} from 'react';
import type Highlight from 'prism-react-renderer';

// Lib does not make this easy
type RenderProps = Parameters<
ComponentProps<typeof Highlight>['children']
>[0];
type GetLineProps = RenderProps['getLineProps'];
type GetTokenProps = RenderProps['getTokenProps'];
type Token = RenderProps['tokens'][number][number];
import type {
LineInputProps,
LineOutputProps,
Token,
TokenInputProps,
TokenOutputProps,
} from 'prism-react-renderer';

export interface Props {
readonly line: Token[];
readonly classNames: string[] | undefined;
readonly showLineNumbers: boolean;
readonly getLineProps: GetLineProps;
readonly getTokenProps: GetTokenProps;
readonly getLineProps: (input: LineInputProps) => LineOutputProps;
readonly getTokenProps: (input: TokenInputProps) => TokenOutputProps;
}

export default function CodeBlockLine(props: Props): JSX.Element;
Expand Down
Expand Up @@ -15,7 +15,8 @@ import {
containsLineNumbers,
useCodeWordWrap,
} from '@docusaurus/theme-common/internal';
import Highlight, {defaultProps, type Language} from 'prism-react-renderer';
import {Highlight, type Language} from 'prism-react-renderer';
import Prism from 'prismjs';
import Line from '@theme/CodeBlock/Line';
import CopyButton from '@theme/CodeBlock/CopyButton';
import WordWrapButton from '@theme/CodeBlock/WordWrapButton';
Expand Down Expand Up @@ -74,16 +75,17 @@ export default function CodeBlockString({
{title && <div className={styles.codeBlockTitle}>{title}</div>}
<div className={styles.codeBlockContent}>
<Highlight
{...defaultProps}
prism={Prism}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the issue, we used the "vendored prism library" and should keep using it so passing a prism instance is not needed now. I'll remove it.

theme={prismTheme}
code={code}
language={(language ?? 'text') as Language}>
{({className, tokens, getLineProps, getTokenProps}) => (
{({className, style, tokens, getLineProps, getTokenProps}) => (
<pre
/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */
tabIndex={0}
ref={wordWrap.codeBlockRef}
className={clsx(className, styles.codeBlock, 'thin-scrollbar')}>
className={clsx(className, styles.codeBlock, 'thin-scrollbar')}
style={style}>
<code
className={clsx(
styles.codeBlockLines,
Expand Down
Expand Up @@ -26,6 +26,10 @@ export default function prismIncludeLanguages(
globalThis.Prism = PrismObject;

additionalLanguages.forEach((lang) => {
if (lang === 'php') {
// eslint-disable-next-line global-require
require('prismjs/components/prism-markup-templating.js');
}
Comment on lines +29 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering why do we need this now?

Copy link
Contributor Author

@harryzcy harryzcy Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it's not an issue previously, but the error is mentioned here: PrismJS/prism#1400 (comment). Without it, there's a TypeError: Cannot read property 'tokenizePlaceholders' of undefined

// eslint-disable-next-line global-require, import/no-dynamic-require
require(`prismjs/components/prism-${lang}`);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-common/package.json
Expand Up @@ -42,7 +42,7 @@
"@types/react-router-config": "*",
"clsx": "^1.2.1",
"parse-numeric-range": "^1.3.0",
"prism-react-renderer": "^1.3.5",
"prism-react-renderer": "^2.1.0",
"tslib": "^2.6.0",
"utility-types": "^3.10.0"
},
Expand Down
Expand Up @@ -7,7 +7,7 @@

import type {CSSProperties} from 'react';
import rangeParser from 'parse-numeric-range';
import type {PrismTheme} from 'prism-react-renderer';
import type {PrismTheme, PrismThemeEntry} from 'prism-react-renderer';

const codeBlockTitleRegex = /title=(?<quote>["'])(?<title>.*?)\1/;
const metastringLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
Expand Down Expand Up @@ -254,14 +254,14 @@ export function parseLines(
}

export function getPrismCssVariables(prismTheme: PrismTheme): CSSProperties {
const mapping: {[name: keyof PrismTheme['plain']]: string} = {
const mapping: PrismThemeEntry = {
color: '--prism-color',
backgroundColor: '--prism-background-color',
};

const properties: {[key: string]: string} = {};
Object.entries(prismTheme.plain).forEach(([key, value]) => {
const varName = mapping[key];
const varName = mapping[key as keyof PrismThemeEntry];
if (varName && typeof value === 'string') {
properties[varName] = value;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-theme-live-codeblock/package.json
Expand Up @@ -30,7 +30,7 @@
"@philpl/buble": "^0.19.7",
"clsx": "^1.2.1",
"fs-extra": "^11.1.1",
"react-live": "2.2.3",
"react-live": "^4.1.5",
"tslib": "^2.6.0"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-theme-live-codeblock/src/deps.d.ts
Expand Up @@ -7,7 +7,8 @@

declare module '@philpl/buble' {
import type {TransformOptions as OriginalTransformOptions} from 'buble';
// eslint-disable-next-line import/no-extraneous-dependencies, no-restricted-syntax

// eslint-disable-next-line no-restricted-syntax
export * from 'buble';
export const features: string[];
export type TransformOptions = OriginalTransformOptions & {
Expand Down
Expand Up @@ -18,9 +18,10 @@ declare module '@docusaurus/theme-live-codeblock' {

declare module '@theme/Playground' {
import type {Props as BaseProps} from '@theme/CodeBlock';
import type {LiveProviderProps} from 'react-live';
import type {LiveProvider} from 'react-live';

type CodeBlockProps = Omit<BaseProps, 'className' | 'language' | 'title'>;
type LiveProviderProps = React.ComponentProps<typeof LiveProvider>;

export interface Props extends CodeBlockProps, LiveProviderProps {
children: string;
Expand Down
Expand Up @@ -13,7 +13,6 @@ import CodeBlock, {type Props} from '@theme-init/CodeBlock';
const withLiveEditor = (Component: typeof CodeBlock) => {
function WrappedComponent(props: Props) {
if (props.live) {
// @ts-expect-error: we have deliberately widened the type of language prop
return <Playground scope={ReactLiveScope} {...props} />;
}

Expand Down
Expand Up @@ -115,7 +115,6 @@ export default function Playground({

return (
<div className={styles.playgroundContainer}>
{/* @ts-expect-error: type incompatibility with refs */}
<LiveProvider
code={children.replace(/\n$/, '')}
noInline={noInline}
Expand Down
Expand Up @@ -31,7 +31,6 @@ describe('interpolate', () => {
object: {hello: 'world'},
array: ['Hello'],
};
// @ts-expect-error: test
expect(interpolate(text, values)).toMatchInlineSnapshot(
`"42 Hello [object Object] Hello"`,
);
Expand All @@ -52,7 +51,6 @@ describe('interpolate', () => {
// Should we emit warnings in such case?
const text = 'Hello {name} how are you {unprovidedValue}?';
const values = {name: 'Sébastien', extraValue: 'today'};
// @ts-expect-error: test
expect(interpolate(text, values)).toMatchInlineSnapshot(
`"Hello Sébastien how are you {unprovidedValue}?"`,
);
Expand All @@ -62,7 +60,6 @@ describe('interpolate', () => {
// Should we emit warnings in such case?
const text = 'Hello {name} how are you {day}?';
expect(interpolate(text)).toEqual(text);
// @ts-expect-error: test
expect(interpolate(text, {})).toEqual(text);
});

Expand All @@ -86,7 +83,6 @@ describe('interpolate', () => {
extraUselessValue1: <div>test</div>,
extraUselessValue2: 'hi',
};
// @ts-expect-error: test
expect(interpolate(text, values)).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -128,15 +124,13 @@ describe('<Interpolate>', () => {
expect(() =>
renderer.create(
<Interpolate>
{/* @ts-expect-error: for test */}
<span>aaa</span>
</Interpolate>,
),
).toThrowErrorMatchingInlineSnapshot(
`"The Docusaurus <Interpolate> component only accept simple string values. Received: React element"`,
);
expect(() =>
// @ts-expect-error: test
renderer.create(<Interpolate>{null}</Interpolate>),
).toThrowErrorMatchingInlineSnapshot(
`"The Docusaurus <Interpolate> component only accept simple string values. Received: object"`,
Expand Down
Expand Up @@ -477,7 +477,7 @@ describe('config warning and error', () => {
url: 'https://mysite.com/someSubpath',
});
expect(error).toBeDefined();
expect(error.message).toBe(
expect(error?.message).toBe(
'The url is not supposed to contain a sub-path like "/someSubpath". Please use the baseUrl field for sub-paths.',
);
});
Expand Down
6 changes: 4 additions & 2 deletions website/docs/api/themes/theme-configuration.mdx
Expand Up @@ -799,12 +799,14 @@ By default, we use [Palenight](https://github.com/FormidableLabs/prism-react-ren
Example configuration:

```js title="docusaurus.config.js"
const {themes} = require('prism-react-renderer');

module.exports = {
themeConfig: {
prism: {
// highlight-start
theme: require('prism-react-renderer/themes/github'),
darkTheme: require('prism-react-renderer/themes/dracula'),
theme: themes.github,
darkTheme: themes.dracula,
// highlight-end
},
},
Expand Down
Expand Up @@ -64,11 +64,13 @@ By default, the Prism [syntax highlighting theme](https://github.com/FormidableL
For example, if you prefer to use the `dracula` highlighting theme:

```js title="docusaurus.config.js"
const {themes} = require('prism-react-renderer');

module.exports = {
themeConfig: {
prism: {
// highlight-next-line
theme: require('prism-react-renderer/themes/dracula'),
theme: themes.dracula,
},
},
};
Expand Down
4 changes: 3 additions & 1 deletion website/src/utils/prismDark.mjs
Expand Up @@ -5,7 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import darkTheme from 'prism-react-renderer/themes/vsDark/index.cjs.js';
import {themes} from 'prism-react-renderer';

const darkTheme = themes.vsDark;

export default {
plain: {
Expand Down
4 changes: 3 additions & 1 deletion website/src/utils/prismLight.mjs
Expand Up @@ -5,7 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import lightTheme from 'prism-react-renderer/themes/github/index.cjs.js';
import {themes} from 'prism-react-renderer';

const lightTheme = themes.github;

export default {
...lightTheme,
Expand Down