feat: Markdown parser is configurable, #42

This commit is contained in:
qingwei.li 2017-01-04 21:41:08 +08:00
commit 8b1000a4a5

View file

@ -5,6 +5,7 @@ import { activeLink, scrollActiveSidebar, bindToggle, scroll2Top, sticky } from
import { genTree, getRoute, isMobile, slugify, merge } from './util' import { genTree, getRoute, isMobile, slugify, merge } from './util'
let OPTIONS = {} let OPTIONS = {}
let markdown = marked
let toc = [] let toc = []
const CACHE = {} const CACHE = {}
@ -54,7 +55,13 @@ export function init (options) {
return `<a href="${href}" title="${title || ''}">${text}</a>` return `<a href="${href}" title="${title || ''}">${text}</a>`
} }
marked.setOptions(merge({ renderer }, OPTIONS.marked))
if (typeof OPTIONS.markdown === 'function') {
markdown.setOptions({ renderer })
markdown = OPTIONS.markdown.call(this, markdown)
} else {
markdown.setOptions(merge({ renderer }, OPTIONS.markdown))
}
} }
/** /**
@ -84,7 +91,7 @@ export function renderApp (dom, replace) {
* article * article
*/ */
export function renderArticle (content) { export function renderArticle (content) {
renderTo('article', content ? marked(content) : 'not found') renderTo('article', content ? markdown(content) : 'not found')
if (!OPTIONS.sidebar && !OPTIONS.loadSidebar) renderSidebar() if (!OPTIONS.sidebar && !OPTIONS.loadSidebar) renderSidebar()
if (content && typeof Vue !== 'undefined' && typeof Vuep !== 'undefined') { if (content && typeof Vue !== 'undefined' && typeof Vuep !== 'undefined') {
@ -101,7 +108,7 @@ export function renderNavbar (content) {
if (CACHE.navbar && CACHE.navbar === content) return if (CACHE.navbar && CACHE.navbar === content) return
CACHE.navbar = content CACHE.navbar = content
if (content) renderTo('nav', marked(content)) if (content) renderTo('nav', markdown(content))
activeLink('nav') activeLink('nav')
} }
@ -112,7 +119,7 @@ export function renderSidebar (content) {
let html let html
if (content) { if (content) {
html = marked(content) html = markdown(content)
} else if (OPTIONS.sidebar) { } else if (OPTIONS.sidebar) {
html = tpl.tree(OPTIONS.sidebar, '<ul>') html = tpl.tree(OPTIONS.sidebar, '<ul>')
} else { } else {
@ -145,7 +152,7 @@ export function renderCover (content) {
if (renderCover.rendered) return if (renderCover.rendered) return
// render cover // render cover
let html = marked(content) let html = markdown(content)
const match = html.trim().match('<p><img[^s]+src="(.*?)"[^a]+alt="(.*?)"></p>$') const match = html.trim().match('<p><img[^s]+src="(.*?)"[^a]+alt="(.*?)"></p>$')
// render background // render background