@@ -5,63 +5,18 @@ import { tree as treeTpl } from './tpl.js';
55import { genTree } from './gen-tree.js' ;
66import { slugify } from './slugify.js' ;
77import { emojify } from './emojify.js' ;
8- import {
9- getAndRemoveConfig ,
10- removeAtag ,
11- getAndRemoveDocsifyIgnoreConfig ,
12- } from './utils.js' ;
8+ import { getAndRemoveConfig } from './utils.js' ;
139import { imageCompiler } from './compiler/image.js' ;
10+ import { headingCompiler } from './compiler/heading.js' ;
1411import { highlightCodeCompiler } from './compiler/code.js' ;
1512import { paragraphCompiler } from './compiler/paragraph.js' ;
1613import { taskListCompiler } from './compiler/taskList.js' ;
1714import { taskListItemCompiler } from './compiler/taskListItem.js' ;
1815import { linkCompiler } from './compiler/link.js' ;
16+ import { compileMedia } from './compiler/media.js' ;
1917
2018const cachedLinks = { } ;
2119
22- const compileMedia = {
23- markdown ( url ) {
24- return {
25- url,
26- } ;
27- } ,
28- mermaid ( url ) {
29- return {
30- url,
31- } ;
32- } ,
33- iframe ( url , title ) {
34- return {
35- html : `<iframe src="${ url } " ${
36- title || 'width=100% height=400'
37- } ></iframe>`,
38- } ;
39- } ,
40- video ( url , title ) {
41- return {
42- html : `<video src="${ url } " ${ title || 'controls' } >Not Support</video>` ,
43- } ;
44- } ,
45- audio ( url , title ) {
46- return {
47- html : `<audio src="${ url } " ${ title || 'controls' } >Not Support</audio>` ,
48- } ;
49- } ,
50- code ( url , title ) {
51- let lang = url . match ( / \. ( \w + ) $ / ) ;
52-
53- lang = title || ( lang && lang [ 1 ] ) ;
54- if ( lang === 'md' ) {
55- lang = 'markdown' ;
56- }
57-
58- return {
59- url,
60- lang,
61- } ;
62- } ,
63- } ;
64-
6520export class Compiler {
6621 constructor ( config , router ) {
6722 this . config = config ;
@@ -173,7 +128,7 @@ export class Compiler {
173128 type = 'audio' ;
174129 }
175130
176- embed = compileMedia [ type ] . call ( this , href , title ) ;
131+ embed = compileMedia [ type ] ( href , title ) ;
177132 embed . type = type ;
178133 }
179134
@@ -198,47 +153,22 @@ export class Compiler {
198153 _initRenderer ( ) {
199154 const renderer = new marked . Renderer ( ) ;
200155 const { linkTarget, linkRel, router, contentBase } = this ;
201- const _self = this ;
156+ // Supports mermaid
202157 const origin = { } ;
203158
204- /**
205- * Render anchor tag
206- * @link https://github.com/markedjs/marked#overriding-renderer-methods
207- * @param {String } tokens the content tokens
208- * @param {Number } depth Type of heading (h<level> tag)
209- * @returns {String } Heading element
210- */
211- origin . heading = renderer . heading = function ( { tokens, depth } ) {
212- const text = this . parser . parseInline ( tokens ) ;
213- let { str, config } = getAndRemoveConfig ( text ) ;
214- const nextToc = { depth, title : str } ;
215-
216- const { content, ignoreAllSubs, ignoreSubHeading } =
217- getAndRemoveDocsifyIgnoreConfig ( str ) ;
218- str = content . trim ( ) ;
219-
220- nextToc . title = removeAtag ( str ) ;
221- nextToc . ignoreAllSubs = ignoreAllSubs ;
222- nextToc . ignoreSubHeading = ignoreSubHeading ;
223- const slug = slugify ( config . id || str ) ;
224- const url = router . toURL ( router . getCurrentPath ( ) , { id : slug } ) ;
225- nextToc . slug = url ;
226- _self . toc . push ( nextToc ) ;
227-
228- // Note: tabindex="-1" allows programmatically focusing on heading
229- // elements after navigation. This is preferred over focusing on the link
230- // within the heading because it matches the focus behavior of screen
231- // readers when navigating page content.
232- return `<h${ depth } id="${ slug } " tabindex="-1"><a href="${ url } " data-id="${ slug } " class="anchor"><span>${ str } </span></a></h${ depth } >` ;
233- } ;
234-
159+ // renderer customizers
160+ origin . heading = headingCompiler ( {
161+ renderer,
162+ router,
163+ compiler : this ,
164+ } ) ;
235165 origin . code = highlightCodeCompiler ( { renderer } ) ;
236166 origin . link = linkCompiler ( {
237167 renderer,
238168 router,
239169 linkTarget,
240170 linkRel,
241- compilerClass : _self ,
171+ compiler : this ,
242172 } ) ;
243173 origin . paragraph = paragraphCompiler ( { renderer } ) ;
244174 origin . image = imageCompiler ( { renderer, contentBase, router } ) ;
0 commit comments