Skip to content

Commit 8f90795

Browse files
committed
feat(theme-classic): Add CodeBlockToken component for swizzling
1 parent b7e598f commit 8f90795

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

packages/docusaurus-theme-classic/src/theme-classic.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,18 @@ declare module '@theme/CodeBlock/WordWrapButton' {
502502
export default function WordWrapButton(props: Props): ReactNode;
503503
}
504504

505+
declare module '@theme/CodeBlock/Token' {
506+
import type {ReactNode} from 'react';
507+
import type {Props as LineProps} from '@theme/CodeBlock/Line';
508+
import type {TokenOutputProps} from 'prism-react-renderer';
509+
510+
export interface Props extends TokenOutputProps {
511+
line: LineProps;
512+
}
513+
514+
export default function CodeBlockToken(props: Props): ReactNode;
515+
}
516+
505517
declare module '@theme/DocCard' {
506518
import type {ReactNode} from 'react';
507519
import type {PropSidebarItem} from '@docusaurus/plugin-content-docs';

packages/docusaurus-theme-classic/src/theme/CodeBlock/Line/index.tsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import React, {type ReactNode} from 'react';
99
import clsx from 'clsx';
1010
import type {Props} from '@theme/CodeBlock/Line';
11+
import CodeBlockToken from '@theme/CodeBlock/Token';
1112

1213
import styles from './styles.module.css';
1314

@@ -26,13 +27,14 @@ function fixLineBreak(line: Token[]) {
2627
return line;
2728
}
2829

29-
export default function CodeBlockLine({
30-
line: lineProp,
31-
classNames,
32-
showLineNumbers,
33-
getLineProps,
34-
getTokenProps,
35-
}: Props): ReactNode {
30+
export default function CodeBlockLine(props: Props): ReactNode {
31+
const {
32+
line: lineProp,
33+
classNames,
34+
showLineNumbers,
35+
getLineProps,
36+
getTokenProps,
37+
} = props;
3638
const line = fixLineBreak(lineProp);
3739

3840
const lineProps = getLineProps({
@@ -41,7 +43,7 @@ export default function CodeBlockLine({
4143
});
4244

4345
const lineTokens = line.map((token, key) => (
44-
<span key={key} {...getTokenProps({token})} />
46+
<CodeBlockToken key={key} line={props} {...getTokenProps({token})} />
4547
));
4648

4749
return (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React, {type ReactNode} from 'react';
9+
import type {Props} from '@theme/CodeBlock/Token';
10+
11+
export default function CodeBlockToken(props: Props): ReactNode {
12+
// We omit the "line" information in the default rendering,
13+
// but its meant for devs who devs who Swizzle this component.
14+
const {line, ...tokenProps} = props;
15+
return <span {...tokenProps} />;
16+
}

website/docs/guides/markdown-features/markdown-features-code-blocks.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ npm run swizzle @docusaurus/theme-classic CodeBlock/Line
390390

391391
The `Line` component will receive the list of class names, based on which you can conditionally render different markup.
392392

393+
```bash npm2yarn
394+
npm run swizzle @docusaurus/theme-classic CodeBlock/Token
395+
```
396+
397+
The `Token` component will receive the CSS styles, a className, and the raw syntax token content, based on which you can conditionally render different markup. You also have access to the parent line via `props.line` to access information like the class names applied from magic comments.
398+
393399
## Line numbering {#line-numbering}
394400

395401
You can enable line numbering for your code block by using `showLineNumbers` key within the language meta string (don't forget to add space directly before the key).

0 commit comments

Comments
 (0)