11import marked from 'marked'
22import Prism from 'prismjs'
3+ import { helper as helperTpl } from './tpl'
4+ import { slugify , clearSlugCache } from './slugify'
5+ import { emojify } from './emojify'
6+ import { toURL } from '../route/hash'
7+ import { isFn , merge , cached } from '../util/core'
38
4- export const renderer = new marked . Renderer ( )
9+ let markdownCompiler = marked
10+ let contentBase = ''
11+ let renderer = new marked . Renderer ( )
512
6- export function markdown ( ) {
13+ const toc = [ ]
714
8- }
15+ /**
16+ * Compile markdown content
17+ */
18+ export const markdown = cached ( text => {
19+ let html = ''
920
10- const toc = [ ]
21+ if ( ! text ) return text
22+
23+ html = markdownCompiler ( text )
24+ html = emojify ( html )
25+ clearSlugCache ( )
26+
27+ return html
28+ } )
29+
30+ markdown . renderer = renderer
31+
32+ markdown . init = function ( config = { } , context = window . location . pathname ) {
33+ contentBase = context
34+
35+ if ( isFn ( config ) ) {
36+ markdownCompiler = config ( marked , renderer )
37+ } else {
38+ renderer = merge ( renderer , config . renderer )
39+ marked . setOptions ( merge ( config , { renderer } ) )
40+ }
41+ }
1142
1243/**
1344 * render anchor tag
1445 * @link https://github.com/chjj/marked#overriding-renderer-methods
1546 */
1647renderer . heading = function ( text , level ) {
1748 const slug = slugify ( text )
18- let route = ''
49+ const url = toURL ( contentBase , { id : slug } )
1950
20- route = `#/${ getRoute ( ) } `
21- toc . push ( { level, slug : `${ route } #${ encodeURIComponent ( slug ) } ` , title : text } )
51+ toc . push ( { level, slug : url , title : text } )
2252
23- return `<h${ level } id="${ slug } "><a href="${ route } # ${ slug } " data-id="${ slug } " class="anchor"><span>${ text } </span></a></h${ level } >`
53+ return `<h${ level } id="${ slug } "><a href="${ url } " data-id="${ slug } " class="anchor"><span>${ text } </span></a></h${ level } >`
2454}
2555// highlight code
2656renderer . code = function ( code , lang = '' ) {
@@ -30,21 +60,31 @@ renderer.code = function (code, lang = '') {
3060}
3161renderer . link = function ( href , title , text ) {
3262 if ( ! / : | ( \/ { 2 } ) / . test ( href ) ) {
63+ // TODO
3364 href = `#/${ href } ` . replace ( / \/ + / g, '/' )
3465 }
3566 return `<a href="${ href } " title="${ title || '' } ">${ text } </a>`
3667}
3768renderer . paragraph = function ( text ) {
3869 if ( / ^ ! & g t ; / . test ( text ) ) {
39- return tpl . helper ( 'tip' , text )
70+ return helperTpl ( 'tip' , text )
4071 } else if ( / ^ \? & g t ; / . test ( text ) ) {
41- return tpl . helper ( 'warn' , text )
72+ return helperTpl ( 'warn' , text )
4273 }
4374 return `<p>${ text } </p>`
4475}
4576renderer . image = function ( href , title , text ) {
46- const url = / : | ( \/ { 2 } ) / . test ( href ) ? href : ( $docsify . basePath + href ) . replace ( / \/ + / g, '/' )
47- const titleHTML = title ? ` title="${ title } "` : ''
77+ // TODO
78+ // get base path
79+ // const url = /:|(\/{2})/.test(href) ? href : ($docsify.basePath + href).replace(/\/+/g, '/')
80+ // const titleHTML = title ? ` title="${title}"` : ''
81+
82+ // return `<img src="${url}" alt="${text}"${titleHTML} />`
83+ }
84+
85+ /**
86+ * Compile sidebar
87+ */
88+ export function sidebar ( text ) {
4889
49- return `<img src="${ url } " alt="${ text } "${ titleHTML } />`
5090}
0 commit comments