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 59
59
"@mdx-js/react" : " 2.2.1" ,
60
60
"@modern-js/builder" : " workspace:*" ,
61
61
"@modern-js/builder-rspack-provider" : " workspace:*" ,
62
- "@modern-js/mdx-rs-binding" : " ^0.1.8 " ,
62
+ "@modern-js/mdx-rs-binding" : " ^0.2.0 " ,
63
63
"@modern-js/remark-container" : " workspace:*" ,
64
64
"@modern-js/doc-plugin-medium-zoom" : " workspace:*" ,
65
65
"@modern-js/utils" : " workspace:*" ,
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import remarkPluginMDXFrontMatter from 'remark-mdx-frontmatter';
9
9
import rehypePluginExternalLinks from 'rehype-external-links' ;
10
10
import { remarkPluginContainer } from '@modern-js/remark-container' ;
11
11
import { remarkPluginToc } from './remarkPlugins/toc' ;
12
- import { rehypePluginPreWrapper } from './rehypePlugins/preWrapper ' ;
12
+ import { rehypePluginCodeMeta } from './rehypePlugins/codeMeta ' ;
13
13
import { remarkPluginNormalizeLink } from './remarkPlugins/normalizeLink' ;
14
14
import { remarkCheckDeadLinks } from './remarkPlugins/checkDeadLink' ;
15
15
@@ -71,7 +71,7 @@ export async function createMDXOptions(
71
71
} ,
72
72
} ,
73
73
] ,
74
- rehypePluginPreWrapper ,
74
+ rehypePluginCodeMeta ,
75
75
[
76
76
rehypePluginExternalLinks ,
77
77
{
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';
20
20
let registered = false ;
21
21
const timeoutIdMap : Map < HTMLElement , NodeJS . Timeout > = new Map ( ) ;
22
22
23
+ export interface CodeProps {
24
+ children : string ;
25
+ className : string ;
26
+ meta ?: string ;
27
+ }
28
+
23
29
function registerLanguages ( ) {
24
30
SyntaxHighlighter . registerLanguage ( 'jsx' , jsx ) ;
25
31
SyntaxHighlighter . registerLanguage ( 'jsx' , tsx ) ;
@@ -38,11 +44,7 @@ function registerLanguages() {
38
44
registered = true ;
39
45
}
40
46
41
- export function Code ( props : {
42
- children : string ;
43
- className : string ;
44
- meta ?: string ;
45
- } ) {
47
+ export function Code ( props : CodeProps ) {
46
48
const copyButtonRef = useRef < HTMLButtonElement > ( null ) ;
47
49
const { siteData } = usePageData ( ) ;
48
50
const { showLineNumbers } = siteData . markdown ;
@@ -101,6 +103,7 @@ export function Code(props: {
101
103
language = { language }
102
104
style = { style }
103
105
wrapLines = { true }
106
+ className = "code"
104
107
// Notice: if the highlight line is specified, the line number must be displayed
105
108
showLineNumbers = { showLineNumbers || highlightLines . length > 0 }
106
109
lineProps = { lineNumber => {
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { Hr } from './hr';
5
5
import { A } from './link' ;
6
6
import { P , Strong , Blockquote } from './paragraph' ;
7
7
import { Code } from './code' ;
8
+ import { Pre } from './pre' ;
8
9
9
10
export function getCustomMDXComponent ( ) {
10
11
return {
@@ -27,5 +28,6 @@ export function getCustomMDXComponent() {
27
28
strong : Strong ,
28
29
a : A ,
29
30
code : Code ,
31
+ pre : Pre ,
30
32
} ;
31
33
}
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 166
166
right : 0 ;
167
167
padding-left : 20px ;
168
168
}
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({
9
9
plugins : [ docTools ( { } ) ] ,
10
10
doc : {
11
11
markdown : {
12
- // experimentalMdxRs: true,
12
+ experimentalMdxRs : true ,
13
13
checkDeadLinks : true ,
14
14
} ,
15
15
root : path . join ( __dirname , 'docs' ) ,
You can’t perform that action at this time.
0 commit comments