Skip to content

Commit 0a6b969

Browse files
authored
feat(doc-core): support code title space (#3744)
* fix: sidebar link i18n * feat: add icon lark * feat: support space in code title
1 parent cfc5bda commit 0a6b969

File tree

11 files changed

+117
-121
lines changed

11 files changed

+117
-121
lines changed

.changeset/poor-seas-talk.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/doc-core': patch
3+
---
4+
5+
feat(doc-core): support space in code title
6+
7+
feat(doc-core): 代码块标题支持空格

packages/cli/doc-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"@mdx-js/react": "2.2.1",
6060
"@modern-js/builder": "workspace:*",
6161
"@modern-js/builder-rspack-provider": "workspace:*",
62-
"@modern-js/mdx-rs-binding": "^0.1.8",
62+
"@modern-js/mdx-rs-binding": "^0.2.0",
6363
"@modern-js/remark-container": "workspace:*",
6464
"@modern-js/doc-plugin-medium-zoom": "workspace:*",
6565
"@modern-js/utils": "workspace:*",

packages/cli/doc-core/src/node/mdx/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import remarkPluginMDXFrontMatter from 'remark-mdx-frontmatter';
99
import rehypePluginExternalLinks from 'rehype-external-links';
1010
import { remarkPluginContainer } from '@modern-js/remark-container';
1111
import { remarkPluginToc } from './remarkPlugins/toc';
12-
import { rehypePluginPreWrapper } from './rehypePlugins/preWrapper';
12+
import { rehypePluginCodeMeta } from './rehypePlugins/codeMeta';
1313
import { remarkPluginNormalizeLink } from './remarkPlugins/normalizeLink';
1414
import { remarkCheckDeadLinks } from './remarkPlugins/checkDeadLink';
1515

@@ -71,7 +71,7 @@ export async function createMDXOptions(
7171
},
7272
},
7373
],
74-
rehypePluginPreWrapper,
74+
rehypePluginCodeMeta,
7575
[
7676
rehypePluginExternalLinks,
7777
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Plugin } from 'unified';
2+
import { visit } from 'unist-util-visit';
3+
import type { Root } from 'hast';
4+
5+
export const rehypePluginCodeMeta: Plugin<[], Root> = () => {
6+
return tree => {
7+
visit(tree, 'element', node => {
8+
// <pre><code>...</code></pre>
9+
// 1. Find pre element
10+
if (
11+
node.tagName === 'pre' &&
12+
node.children[0]?.type === 'element' &&
13+
node.children[0].tagName === 'code'
14+
) {
15+
const codeNode = node.children[0];
16+
// language-xxx
17+
const meta = (codeNode.data?.meta as string) || '';
18+
codeNode.properties.meta = meta;
19+
}
20+
});
21+
};
22+
};

packages/cli/doc-core/src/node/mdx/rehypePlugins/preWrapper.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

packages/cli/doc-core/src/theme-default/layout/DocLayout/docComponents/code.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import { usePageData } from '@/runtime';
2020
let registered = false;
2121
const timeoutIdMap: Map<HTMLElement, NodeJS.Timeout> = new Map();
2222

23+
export interface CodeProps {
24+
children: string;
25+
className: string;
26+
meta?: string;
27+
}
28+
2329
function registerLanguages() {
2430
SyntaxHighlighter.registerLanguage('jsx', jsx);
2531
SyntaxHighlighter.registerLanguage('jsx', tsx);
@@ -38,11 +44,7 @@ function registerLanguages() {
3844
registered = true;
3945
}
4046

41-
export function Code(props: {
42-
children: string;
43-
className: string;
44-
meta?: string;
45-
}) {
47+
export function Code(props: CodeProps) {
4648
const copyButtonRef = useRef<HTMLButtonElement>(null);
4749
const { siteData } = usePageData();
4850
const { showLineNumbers } = siteData.markdown;
@@ -101,6 +103,7 @@ export function Code(props: {
101103
language={language}
102104
style={style}
103105
wrapLines={true}
106+
className="code"
104107
// Notice: if the highlight line is specified, the line number must be displayed
105108
showLineNumbers={showLineNumbers || highlightLines.length > 0}
106109
lineProps={lineNumber => {

packages/cli/doc-core/src/theme-default/layout/DocLayout/docComponents/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Hr } from './hr';
55
import { A } from './link';
66
import { P, Strong, Blockquote } from './paragraph';
77
import { Code } from './code';
8+
import { Pre } from './pre';
89

910
export function getCustomMDXComponent() {
1011
return {
@@ -27,5 +28,6 @@ export function getCustomMDXComponent() {
2728
strong: Strong,
2829
a: A,
2930
code: Code,
31+
pre: Pre,
3032
};
3133
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { CodeProps } from './code';
2+
3+
const DEFAULT_LANGUAGE_CLASS = 'language-bash';
4+
5+
export function parseTitleFromMeta(meta: string): string {
6+
if (!meta) {
7+
return '';
8+
}
9+
let result = meta;
10+
const highlightReg = /{[\d,-]*}/i;
11+
const highlightMeta = highlightReg.exec(meta)?.[0];
12+
if (highlightMeta) {
13+
result = meta.replace(highlightReg, '').trim();
14+
}
15+
result = result.split('=')[1] ?? '';
16+
return result?.replace(/["'`]/g, '');
17+
}
18+
19+
export function Pre({
20+
children,
21+
}: {
22+
children: React.ReactElement[] | React.ReactElement;
23+
}) {
24+
if (Array.isArray(children)) {
25+
return <pre>{children}</pre>;
26+
}
27+
28+
const { className, meta } = children.props as CodeProps;
29+
30+
const codeTitle = parseTitleFromMeta(meta);
31+
return (
32+
<div className={className || DEFAULT_LANGUAGE_CLASS}>
33+
{codeTitle && <div className="modern-code-title">{codeTitle}</div>}
34+
<div className="modern-code-content">{children}</div>
35+
</div>
36+
);
37+
}

packages/cli/doc-core/src/theme-default/styles/doc/code.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,7 @@
166166
right: 0;
167167
padding-left: 20px;
168168
}
169+
170+
.modern-doc [class*='language-'] .modern-code-content > code {
171+
padding: 16px 20px;
172+
}

packages/document/doc-tools-doc/modern.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default defineConfig({
99
plugins: [docTools({})],
1010
doc: {
1111
markdown: {
12-
// experimentalMdxRs: true,
12+
experimentalMdxRs: true,
1313
checkDeadLinks: true,
1414
},
1515
root: path.join(__dirname, 'docs'),

0 commit comments

Comments
 (0)