From f4800e09f18ce4ffeff2cc817bd586f11dfdeca2 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Thu, 17 Aug 2017 16:52:54 +0800 Subject: [PATCH] feat(markdown): supports mermaid #137 --- docs/de-de/markdown.md | 25 +++++++++++++++++++++++++ docs/markdown.md | 23 +++++++++++++++++++++++ docs/zh-cn/markdown.md | 26 +++++++++++++++++++++++++- src/core/render/compiler.js | 14 +++++++++----- 4 files changed, 82 insertions(+), 6 deletions(-) diff --git a/docs/de-de/markdown.md b/docs/de-de/markdown.md index 0a70b4d38..9d0e25083 100644 --- a/docs/de-de/markdown.md +++ b/docs/de-de/markdown.md @@ -28,3 +28,28 @@ window.$docsify = { } } ``` + + +## Supports mermaid + +```js +// Import mermaid +// +// + +window.$docsify = { + markdown: { + renderer: { + code: function(code, lang) { + if (lang === "mermaid") { + return ( + '
' + mermaid.render(lang, code) + "
" + ); + } + return this.origin.code.apply(this, arguments); + } + } + } +} +``` + diff --git a/docs/markdown.md b/docs/markdown.md index a50411a3f..5f15608b5 100644 --- a/docs/markdown.md +++ b/docs/markdown.md @@ -28,3 +28,26 @@ window.$docsify = { } } ``` + +## Supports mermaid + +```js +// Import mermaid +// +// + +window.$docsify = { + markdown: { + renderer: { + code: function(code, lang) { + if (lang === "mermaid") { + return ( + '
' + mermaid.render(lang, code) + "
" + ); + } + return this.origin.code.apply(this, arguments); + } + } + } +} +``` diff --git a/docs/zh-cn/markdown.md b/docs/zh-cn/markdown.md index a0028399d..3d13154d9 100644 --- a/docs/zh-cn/markdown.md +++ b/docs/zh-cn/markdown.md @@ -27,4 +27,28 @@ window.$docsify = { return marked } } -``` \ No newline at end of file +``` + + +## Supports mermaid + +```js +// Import mermaid +// +// + +window.$docsify = { + markdown: { + renderer: { + code: function(code, lang) { + if (lang === "mermaid") { + return ( + '
' + mermaid.render(lang, code) + "
" + ); + } + return this.origin.code.apply(this, arguments); + } + } + } +} +``` diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 845af0432..9f04ebb66 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -61,11 +61,13 @@ export class Compiler { 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 */ - renderer.heading = function (text, level) { + origin.heading = renderer.heading = function (text, level) { const nextToc = { level, title: text } if (/{docsify-ignore}/g.test(text)) { @@ -88,12 +90,12 @@ export class Compiler { return `${text}` } // highlight code - renderer.code = function (code, lang = '') { + origin.code = renderer.code = function (code, lang = '') { const hl = Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup) return `
${hl}
` } - renderer.link = function (href, title, text) { + origin.link = renderer.link = function (href, title, text) { let blank = '' if (!/:|(\/{2})/.test(href) && @@ -117,7 +119,7 @@ export class Compiler { } return `${text}` } - renderer.paragraph = function (text) { + origin.paragraph = renderer.paragraph = function (text) { if (/^!>/.test(text)) { return helperTpl('tip', text) } else if (/^\?>/.test(text)) { @@ -125,7 +127,7 @@ export class Compiler { } return `

${text}

` } - renderer.image = function (href, title, text) { + origin.image = renderer.image = function (href, title, text) { let url = href const titleHTML = title ? ` title="${title}"` : '' @@ -136,6 +138,8 @@ export class Compiler { return `${text}` } + renderer.origin = origin + return renderer }