Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/poor-seas-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/doc-core': patch
---

feat(doc-core): support space in code title

feat(doc-core): 代码块标题支持空格
2 changes: 1 addition & 1 deletion packages/cli/doc-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@mdx-js/react": "2.2.1",
"@modern-js/builder": "workspace:*",
"@modern-js/builder-rspack-provider": "workspace:*",
"@modern-js/mdx-rs-binding": "^0.1.8",
"@modern-js/mdx-rs-binding": "^0.2.0",
"@modern-js/remark-container": "workspace:*",
"@modern-js/doc-plugin-medium-zoom": "workspace:*",
"@modern-js/utils": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/doc-core/src/node/mdx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import remarkPluginMDXFrontMatter from 'remark-mdx-frontmatter';
import rehypePluginExternalLinks from 'rehype-external-links';
import { remarkPluginContainer } from '@modern-js/remark-container';
import { remarkPluginToc } from './remarkPlugins/toc';
import { rehypePluginPreWrapper } from './rehypePlugins/preWrapper';
import { rehypePluginCodeMeta } from './rehypePlugins/codeMeta';
import { remarkPluginNormalizeLink } from './remarkPlugins/normalizeLink';
import { remarkCheckDeadLinks } from './remarkPlugins/checkDeadLink';

Expand Down Expand Up @@ -71,7 +71,7 @@ export async function createMDXOptions(
},
},
],
rehypePluginPreWrapper,
rehypePluginCodeMeta,
[
rehypePluginExternalLinks,
{
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/doc-core/src/node/mdx/rehypePlugins/codeMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Plugin } from 'unified';
import { visit } from 'unist-util-visit';
import type { Root } from 'hast';

export const rehypePluginCodeMeta: Plugin<[], Root> = () => {
return tree => {
visit(tree, 'element', node => {
// <pre><code>...</code></pre>
// 1. Find pre element
if (
node.tagName === 'pre' &&
node.children[0]?.type === 'element' &&
node.children[0].tagName === 'code'
) {
const codeNode = node.children[0];
// language-xxx
const meta = (codeNode.data?.meta as string) || '';
codeNode.properties.meta = meta;
}
});
};
};
77 changes: 0 additions & 77 deletions packages/cli/doc-core/src/node/mdx/rehypePlugins/preWrapper.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import { usePageData } from '@/runtime';
let registered = false;
const timeoutIdMap: Map<HTMLElement, NodeJS.Timeout> = new Map();

export interface CodeProps {
children: string;
className: string;
meta?: string;
}

function registerLanguages() {
SyntaxHighlighter.registerLanguage('jsx', jsx);
SyntaxHighlighter.registerLanguage('jsx', tsx);
Expand All @@ -38,11 +44,7 @@ function registerLanguages() {
registered = true;
}

export function Code(props: {
children: string;
className: string;
meta?: string;
}) {
export function Code(props: CodeProps) {
const copyButtonRef = useRef<HTMLButtonElement>(null);
const { siteData } = usePageData();
const { showLineNumbers } = siteData.markdown;
Expand Down Expand Up @@ -101,6 +103,7 @@ export function Code(props: {
language={language}
style={style}
wrapLines={true}
className="code"
// Notice: if the highlight line is specified, the line number must be displayed
showLineNumbers={showLineNumbers || highlightLines.length > 0}
lineProps={lineNumber => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Hr } from './hr';
import { A } from './link';
import { P, Strong, Blockquote } from './paragraph';
import { Code } from './code';
import { Pre } from './pre';

export function getCustomMDXComponent() {
return {
Expand All @@ -27,5 +28,6 @@ export function getCustomMDXComponent() {
strong: Strong,
a: A,
code: Code,
pre: Pre,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CodeProps } from './code';

const DEFAULT_LANGUAGE_CLASS = 'language-bash';

export function parseTitleFromMeta(meta: string): string {
if (!meta) {
return '';
}
let result = meta;
const highlightReg = /{[\d,-]*}/i;
const highlightMeta = highlightReg.exec(meta)?.[0];
if (highlightMeta) {
result = meta.replace(highlightReg, '').trim();
}
result = result.split('=')[1] ?? '';
return result?.replace(/["'`]/g, '');
}

export function Pre({
children,
}: {
children: React.ReactElement[] | React.ReactElement;
}) {
if (Array.isArray(children)) {
return <pre>{children}</pre>;
}

const { className, meta } = children.props as CodeProps;

const codeTitle = parseTitleFromMeta(meta);
return (
<div className={className || DEFAULT_LANGUAGE_CLASS}>
{codeTitle && <div className="modern-code-title">{codeTitle}</div>}
<div className="modern-code-content">{children}</div>
</div>
);
}
4 changes: 4 additions & 0 deletions packages/cli/doc-core/src/theme-default/styles/doc/code.css
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,7 @@
right: 0;
padding-left: 20px;
}

.modern-doc [class*='language-'] .modern-code-content > code {
padding: 16px 20px;
}
2 changes: 1 addition & 1 deletion packages/document/doc-tools-doc/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineConfig({
plugins: [docTools({})],
doc: {
markdown: {
// experimentalMdxRs: true,
experimentalMdxRs: true,
checkDeadLinks: true,
},
root: path.join(__dirname, 'docs'),
Expand Down
Loading