Skip to content

Commit

Permalink
feat(markdown): supports mermaid #137
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Aug 17, 2017
1 parent ac10de1 commit f4800e0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
25 changes: 25 additions & 0 deletions docs/de-de/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,28 @@ window.$docsify = {
}
}
```


## Supports mermaid

```js
// Import mermaid
// <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.css">
// <script src="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>

window.$docsify = {
markdown: {
renderer: {
code: function(code, lang) {
if (lang === "mermaid") {
return (
'<div class="mermaid">' + mermaid.render(lang, code) + "</div>"
);
}
return this.origin.code.apply(this, arguments);
}
}
}
}
```

23 changes: 23 additions & 0 deletions docs/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,26 @@ window.$docsify = {
}
}
```

## Supports mermaid

```js
// Import mermaid
// <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.css">
// <script src="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>

window.$docsify = {
markdown: {
renderer: {
code: function(code, lang) {
if (lang === "mermaid") {
return (
'<div class="mermaid">' + mermaid.render(lang, code) + "</div>"
);
}
return this.origin.code.apply(this, arguments);
}
}
}
}
```
26 changes: 25 additions & 1 deletion docs/zh-cn/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@ window.$docsify = {
return marked
}
}
```
```


## Supports mermaid

```js
// Import mermaid
// <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.css">
// <script src="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>

window.$docsify = {
markdown: {
renderer: {
code: function(code, lang) {
if (lang === "mermaid") {
return (
'<div class="mermaid">' + mermaid.render(lang, code) + "</div>"
);
}
return this.origin.code.apply(this, arguments);
}
}
}
}
```
14 changes: 9 additions & 5 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -88,12 +90,12 @@ export class Compiler {
return `<h${level} id="${slug}"><a href="${url}" data-id="${slug}" class="anchor"><span>${text}</span></a></h${level}>`
}
// 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 `<pre v-pre data-lang="${lang}"><code class="lang-${lang}">${hl}</code></pre>`
}
renderer.link = function (href, title, text) {
origin.link = renderer.link = function (href, title, text) {
let blank = ''

if (!/:|(\/{2})/.test(href) &&
Expand All @@ -117,15 +119,15 @@ export class Compiler {
}
return `<a href="${href}"${title || ''}${blank}>${text}</a>`
}
renderer.paragraph = function (text) {
origin.paragraph = renderer.paragraph = function (text) {
if (/^!&gt;/.test(text)) {
return helperTpl('tip', text)
} else if (/^\?&gt;/.test(text)) {
return helperTpl('warn', text)
}
return `<p>${text}</p>`
}
renderer.image = function (href, title, text) {
origin.image = renderer.image = function (href, title, text) {
let url = href
const titleHTML = title ? ` title="${title}"` : ''

Expand All @@ -136,6 +138,8 @@ export class Compiler {
return `<img src="${url}" data-origin="${href}" alt="${text}"${titleHTML}>`
}

renderer.origin = origin

return renderer
}

Expand Down

0 comments on commit f4800e0

Please # to comment.