File tree Expand file tree Collapse file tree 11 files changed +117
-121
lines changed
layout/DocLayout/docComponents Expand file tree Collapse file tree 11 files changed +117
-121
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ ' @modern-js/doc-core ' : patch
3+ ---
4+
5+ feat(doc-core): support space in code title
6+
7+ feat(doc-core): 代码块标题支持空格
Original file line number Diff line number Diff line change 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:*" ,
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import remarkPluginMDXFrontMatter from 'remark-mdx-frontmatter';
99import rehypePluginExternalLinks from 'rehype-external-links' ;
1010import { remarkPluginContainer } from '@modern-js/remark-container' ;
1111import { remarkPluginToc } from './remarkPlugins/toc' ;
12- import { rehypePluginPreWrapper } from './rehypePlugins/preWrapper ' ;
12+ import { rehypePluginCodeMeta } from './rehypePlugins/codeMeta ' ;
1313import { remarkPluginNormalizeLink } from './remarkPlugins/normalizeLink' ;
1414import { remarkCheckDeadLinks } from './remarkPlugins/checkDeadLink' ;
1515
@@ -71,7 +71,7 @@ export async function createMDXOptions(
7171 } ,
7272 } ,
7373 ] ,
74- rehypePluginPreWrapper ,
74+ rehypePluginCodeMeta ,
7575 [
7676 rehypePluginExternalLinks ,
7777 {
Original file line number Diff line number Diff line change 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+ } ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -20,6 +20,12 @@ import { usePageData } from '@/runtime';
2020let registered = false ;
2121const timeoutIdMap : Map < HTMLElement , NodeJS . Timeout > = new Map ( ) ;
2222
23+ export interface CodeProps {
24+ children : string ;
25+ className : string ;
26+ meta ?: string ;
27+ }
28+
2329function 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 => {
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { Hr } from './hr';
55import { A } from './link' ;
66import { P , Strong , Blockquote } from './paragraph' ;
77import { Code } from './code' ;
8+ import { Pre } from './pre' ;
89
910export 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 166166 right : 0 ;
167167 padding-left : 20px ;
168168}
169+
170+ .modern-doc [class *= 'language-' ] .modern-code-content > code {
171+ padding : 16px 20px ;
172+ }
Original file line number Diff line number Diff 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' ) ,
You can’t perform that action at this time.
0 commit comments