1
1
import markdown from '@wcj/markdown-to-html' ;
2
2
import rehypeDocument from 'rehype-document' ;
3
3
import remarkGemoji from 'remark-gemoji' ;
4
+ import rehypeRaw from 'rehype-raw' ;
5
+ import rehypeAttrs from 'rehype-attr' ;
6
+ import rehypeKatex from 'rehype-katex' ;
4
7
import rehypeAutolinkHeadings from 'rehype-autolink-headings' ;
5
8
import rehypeSlug from 'rehype-slug' ;
6
9
import { htmlTagAddAttri } from './nodes/htmlTagAddAttri.mjs' ;
@@ -9,7 +12,7 @@ import { header } from './nodes/header.mjs';
9
12
import { rehypeUrls } from './utils/rehypeUrls.mjs' ;
10
13
import { tooltips } from './utils/tooltips.mjs' ;
11
14
import { homeCardIcons } from './utils/homeCardIcons.mjs' ;
12
- import { getTocsTree } from './utils/getTocsTree.mjs' ;
15
+ import { getTocsTree , getTocsTitleNode , getTocsTitleNodeWarpper , addTocsInWarp } from './utils/getTocsTree.mjs' ;
13
16
import { rehypeTitle } from './utils/rehypeTitle.mjs' ;
14
17
import { anchorPoint } from './utils/anchorPoint.mjs' ;
15
18
import { rehypePreviewHTML } from './utils/rehypePreviewHTML.mjs' ;
@@ -29,30 +32,49 @@ export function create(str = '', options = {}) {
29
32
rehypePlugins : [
30
33
rehypeSlug ,
31
34
rehypeAutolinkHeadings ,
32
- [ rehypeDocument , {
33
- title : `${ title ? `${ title } & ` : '' } ${ subTitle } Quick Reference` ,
34
- css : [ ...options . css ] ,
35
- link : [
36
- { rel : 'icon' , href : favicon , type : 'image/svg+xml' }
37
- ] ,
38
- meta : [
39
- { description : `${ description } 为开发人员分享快速参考备忘单。` } ,
40
- { keywords : `Quick,Reference,cheatsheet,${ ! options . isHome && options . filename || '' } ` }
41
- ]
42
- } ] ,
35
+ [ rehypeDocument , {
36
+ title : `${ title ? `${ title } & ` : '' } ${ subTitle } Quick Reference` ,
37
+ css : [ ...options . css ] ,
38
+ link : [
39
+ { rel : 'icon' , href : favicon , type : 'image/svg+xml' }
40
+ ] ,
41
+ meta : [
42
+ { description : `${ description } 为开发人员分享快速参考备忘单。` } ,
43
+ { keywords : `Quick,Reference,cheatsheet,${ ! options . isHome && options . filename || '' } ` }
44
+ ]
45
+ } ]
43
46
] ,
47
+ filterPlugins : ( type , plugins = [ ] ) => {
48
+ if ( type === 'rehype' ) {
49
+ const dt = plugins . filter ( plug => {
50
+ return / ( r e h y p e R a w ) / . test ( plug . name ) ? false : true ;
51
+ } ) ;
52
+ // 放在 rehypeDocument 前面
53
+ dt . unshift ( rehypeRaw )
54
+ return dt ;
55
+ }
56
+ return plugins
57
+ } ,
44
58
rewrite : ( node , index , parent ) => {
45
59
rehypePreviewHTML ( node , parent ) ;
46
60
rehypeTitle ( node , options . filename ) ;
47
61
homeCardIcons ( node , parent , options . isHome ) ;
48
62
tooltips ( node , index , parent ) ;
49
63
htmlTagAddAttri ( node , options ) ;
50
64
rehypeUrls ( node ) ;
51
- if ( node . type === 'element' && node . tagName === 'body' ) {
52
- node . children = getTocsTree ( [ ...node . children ] ) ;
53
- node . children . unshift ( header ( options ) ) ;
54
- node . children . push ( footer ( ) ) ;
55
- node . children . push ( anchorPoint ( ) ) ;
65
+ if ( node . children ) {
66
+ if ( node . type === 'element' && node . tagName === 'body' ) {
67
+ const tocsData = getTocsTree ( [ ...node . children ] ) ;
68
+ if ( ! options . isHome ) {
69
+ const tocsMenus = getTocsTitleNode ( [ ...tocsData ] ) ;
70
+ node . children = addTocsInWarp ( [ ...tocsData ] , getTocsTitleNodeWarpper ( tocsMenus ) )
71
+ } else {
72
+ node . children = tocsData ;
73
+ }
74
+ node . children . unshift ( header ( options ) ) ;
75
+ node . children . push ( footer ( ) ) ;
76
+ node . children . push ( anchorPoint ( ) ) ;
77
+ }
56
78
}
57
79
}
58
80
}
0 commit comments