import marked from 'marked'
import Prism from 'prismjs'
import { helper as helperTpl, tree as treeTpl } from './tpl'
import { genTree } from './gen-tree'
import { slugify } from './slugify'
import { emojify } from './emojify'
import { isAbsolutePath, getPath } from '../router/util'
import { isFn, merge, cached } from '../util/core'
const cachedLinks = {}
function getAndRemoveConfig (str = '') {
const config = {}
if (str) {
str = str
.replace(/:([\w-]+)=?([\w-]+)?/g, (m, key, value) => {
config[key] = value || true
return ''
})
.trim()
}
return { str, config }
}
export class Compiler {
constructor (config, router) {
this.config = config
this.router = router
this.cacheTree = {}
this.toc = []
this.linkTarget = config.externalLinkTarget || '_blank'
this.contentBase = router.getBasePath()
const renderer = this._initRenderer()
let compile
const mdConf = config.markdown || {}
if (isFn(mdConf)) {
compile = mdConf(marked, renderer)
} else {
marked.setOptions(
merge(mdConf, {
renderer: merge(renderer, mdConf.renderer)
})
)
compile = marked
}
this.compile = cached(text => {
let html = ''
if (!text) return text
html = compile(text)
html = config.noEmoji ? html : emojify(html)
slugify.clear()
return html
})
}
matchNotCompileLink (link) {
const links = this.config.noCompileLinks || []
for (var i = 0; i < links.length; i++) {
const n = links[i]
const re = cachedLinks[n] || (cachedLinks[n] = new RegExp(`^${n}$`))
if (re.test(link)) {
return link
}
}
}
_initRenderer () {
const renderer = new marked.Renderer()
const { linkTarget, router, contentBase } = this
const _self = this
const origin = {}
/**
* render anchor tag
* @link https://github.com/chjj/marked#overriding-renderer-methods
*/
origin.heading = renderer.heading = function (text, level) {
const nextToc = { level, title: text }
if (/{docsify-ignore}/g.test(text)) {
text = text.replace('{docsify-ignore}', '')
nextToc.title = text
nextToc.ignoreSubHeading = true
}
if (/{docsify-ignore-all}/g.test(text)) {
text = text.replace('{docsify-ignore-all}', '')
nextToc.title = text
nextToc.ignoreAllSubs = true
}
const slug = slugify(text)
const url = router.toURL(router.getCurrentPath(), { id: slug })
nextToc.slug = url
_self.toc.push(nextToc)
return `
${hl}
`
}
origin.link = renderer.link = function (href, title = '', text) {
let attrs = ''
const { str, config } = getAndRemoveConfig(title)
title = str
if (
!/:|(\/{2})/.test(href) &&
!_self.matchNotCompileLink(href) &&
!config.ignore
) {
href = router.toURL(href, null, router.getCurrentPath())
} else {
attrs += ` target="${linkTarget}"`
}
if (config.target) {
attrs += ' target=' + config.target
}
if (config.disabled) {
attrs += ' disabled'
href = 'javascript:void(0)'
}
if (title) {
attrs += ` title="${title}"`
}
return `${text}`
}
origin.paragraph = renderer.paragraph = function (text) {
if (/^!>/.test(text)) {
return helperTpl('tip', text)
} else if (/^\?>/.test(text)) {
return helperTpl('warn', text)
}
return `${text}
` } origin.image = renderer.image = function (href, title, text) { let url = href let attrs = '' const { str, config } = getAndRemoveConfig(title) title = str if (config['no-zoom']) { attrs += ' data-no-zoom' } if (title) { attrs += ` title="${title}"` } if (!isAbsolutePath(href)) { url = getPath(contentBase, href) } return `