Skip to content

Commit

Permalink
feat: improve marked render
Browse files Browse the repository at this point in the history
  • Loading branch information
viko16 committed Nov 27, 2016
1 parent 247757e commit ae0397d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
37 changes: 37 additions & 0 deletions src/utils/render.js
@@ -0,0 +1,37 @@
import marked from 'marked'
import Prism from 'prismjs'

// https://github.com/chjj/marked#overriding-renderer-methods
const renderer = new marked.Renderer()

/**
* modify anchor tag for Non-English languages
*
* @override
* @param {any} text
* @param {any} level
* @returns
*/
renderer.heading = (text, level) => {
const slug = text.replace(/<(?:.|\n)*?>/gm, '').toLowerCase().replace(/[\s\n\t]+/g, '-')
return `<h${level} id="${slug}">${text}</h${level}>`
}
/**
* highlight my code
*
* @override
* @param {any} code
* @param {any} lang
* @returns
*/
renderer.code = (code, lang) => {
const highlight = Prism.highlight(code, Prism.languages[lang] || Prism.languages.javascript)
return `<pre><code class="lang-${escape(lang, true)}">${highlight}</code></pre>`
}
marked.setOptions({
renderer,
breaks: true,
gfm: true
})

export default marked
13 changes: 1 addition & 12 deletions src/views/Post.vue
Expand Up @@ -13,19 +13,8 @@
import Vue from 'vue'
import api from '../api'
import conf from '../conf.json'
import marked from 'marked'
import Prism from 'prismjs'
import fm from 'front-matter'
// https://github.com/chjj/marked#options-1
marked.setOptions({
highlight (code, lang) {
// http://prismjs.com/extending.html#api
return Prism.highlight(code, Prism.languages[lang] || Prism.languages.javascript)
},
breaks: true,
gfm: true
})
import marked from '../utils/render.js'
export default {
name: 'postView',
Expand Down

0 comments on commit ae0397d

Please sign in to comment.