@@ -5,64 +5,19 @@ import { tree as treeTpl } from './tpl.js';
5
5
import { genTree } from './gen-tree.js' ;
6
6
import { slugify } from './slugify.js' ;
7
7
import { emojify } from './emojify.js' ;
8
- import {
9
- getAndRemoveConfig ,
10
- removeAtag ,
11
- getAndRemoveDocsifyIgnoreConfig ,
12
- } from './utils.js' ;
8
+ import { getAndRemoveConfig } from './utils.js' ;
13
9
import { imageCompiler } from './compiler/image.js' ;
10
+ import { headingCompiler } from './compiler/heading.js' ;
14
11
import { highlightCodeCompiler } from './compiler/code.js' ;
15
12
import { paragraphCompiler } from './compiler/paragraph.js' ;
16
13
import { blockquoteCompiler } from './compiler/blockquote.js' ;
17
14
import { taskListCompiler } from './compiler/taskList.js' ;
18
15
import { taskListItemCompiler } from './compiler/taskListItem.js' ;
19
16
import { linkCompiler } from './compiler/link.js' ;
17
+ import { compileMedia } from './compiler/media.js' ;
20
18
21
19
const cachedLinks = { } ;
22
20
23
- const compileMedia = {
24
- markdown ( url ) {
25
- return {
26
- url,
27
- } ;
28
- } ,
29
- mermaid ( url ) {
30
- return {
31
- url,
32
- } ;
33
- } ,
34
- iframe ( url , title ) {
35
- return {
36
- html : `<iframe src="${ url } " ${
37
- title || 'width=100% height=400'
38
- } ></iframe>`,
39
- } ;
40
- } ,
41
- video ( url , title ) {
42
- return {
43
- html : `<video src="${ url } " ${ title || 'controls' } >Not Support</video>` ,
44
- } ;
45
- } ,
46
- audio ( url , title ) {
47
- return {
48
- html : `<audio src="${ url } " ${ title || 'controls' } >Not Support</audio>` ,
49
- } ;
50
- } ,
51
- code ( url , title ) {
52
- let lang = url . match ( / \. ( \w + ) $ / ) ;
53
-
54
- lang = title || ( lang && lang [ 1 ] ) ;
55
- if ( lang === 'md' ) {
56
- lang = 'markdown' ;
57
- }
58
-
59
- return {
60
- url,
61
- lang,
62
- } ;
63
- } ,
64
- } ;
65
-
66
21
export class Compiler {
67
22
constructor ( config , router ) {
68
23
this . config = config ;
@@ -174,7 +129,7 @@ export class Compiler {
174
129
type = 'audio' ;
175
130
}
176
131
177
- embed = compileMedia [ type ] . call ( this , href , title ) ;
132
+ embed = compileMedia [ type ] ( href , title ) ;
178
133
embed . type = type ;
179
134
}
180
135
@@ -199,48 +154,23 @@ export class Compiler {
199
154
_initRenderer ( ) {
200
155
const renderer = new marked . Renderer ( ) ;
201
156
const { linkTarget, linkRel, router, contentBase } = this ;
202
- const _self = this ;
157
+ // Supports mermaid
203
158
const origin = { } ;
204
159
205
- /**
206
- * Render anchor tag
207
- * @link https://github.com/markedjs/marked#overriding-renderer-methods
208
- * @param {String } tokens the content tokens
209
- * @param {Number } depth Type of heading (h<level> tag)
210
- * @returns {String } Heading element
211
- */
212
- origin . heading = renderer . heading = function ( { tokens, depth } ) {
213
- const text = this . parser . parseInline ( tokens ) ;
214
- let { str, config } = getAndRemoveConfig ( text ) ;
215
- const nextToc = { depth, title : str } ;
216
-
217
- const { content, ignoreAllSubs, ignoreSubHeading } =
218
- getAndRemoveDocsifyIgnoreConfig ( str ) ;
219
- str = content . trim ( ) ;
220
-
221
- nextToc . title = removeAtag ( str ) ;
222
- nextToc . ignoreAllSubs = ignoreAllSubs ;
223
- nextToc . ignoreSubHeading = ignoreSubHeading ;
224
- const slug = slugify ( config . id || str ) ;
225
- const url = router . toURL ( router . getCurrentPath ( ) , { id : slug } ) ;
226
- nextToc . slug = url ;
227
- _self . toc . push ( nextToc ) ;
228
-
229
- // Note: tabindex="-1" allows programmatically focusing on heading
230
- // elements after navigation. This is preferred over focusing on the link
231
- // within the heading because it matches the focus behavior of screen
232
- // readers when navigating page content.
233
- return `<h${ depth } id="${ slug } " tabindex="-1"><a href="${ url } " data-id="${ slug } " class="anchor"><span>${ str } </span></a></h${ depth } >` ;
234
- } ;
235
-
160
+ // Renderer customizers
161
+ origin . heading = headingCompiler ( {
162
+ renderer,
163
+ router,
164
+ compiler : this ,
165
+ } ) ;
236
166
origin . blockquoteCompiler = blockquoteCompiler ( { renderer } ) ;
237
167
origin . code = highlightCodeCompiler ( { renderer } ) ;
238
168
origin . link = linkCompiler ( {
239
169
renderer,
240
170
router,
241
171
linkTarget,
242
172
linkRel,
243
- compilerClass : _self ,
173
+ compiler : this ,
244
174
} ) ;
245
175
origin . paragraph = paragraphCompiler ( { renderer } ) ;
246
176
origin . image = imageCompiler ( { renderer, contentBase, router } ) ;
0 commit comments