aboutsummaryrefslogtreecommitdiff
path: root/node_modules/vuepress/lib/markdown/index.js
blob: f4942f6ce63c8ec708f2d0aa58ee58d86a62aa24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const highlight = require('./highlight')
const highlightLines = require('./highlightLines')
const preWrapper = require('./preWrapper')
const lineNumbers = require('./lineNumbers')
const component = require('./component')
const hoistScriptStyle = require('./hoist')
const convertRouterLink = require('./link')
const containers = require('./containers')
const snippet = require('./snippet')
const emoji = require('markdown-it-emoji')
const anchor = require('markdown-it-anchor')
const toc = require('markdown-it-table-of-contents')
const _slugify = require('./slugify')
const { parseHeaders } = require('../util/parseHeaders')

module.exports = ({ markdown = {}} = {}) => {
  // allow user config slugify
  const slugify = markdown.slugify || _slugify

  const md = require('markdown-it')({
    html: true,
    highlight
  })
    // custom plugins
    .use(component)
    .use(highlightLines)
    .use(preWrapper)
    .use(snippet)
    .use(convertRouterLink, Object.assign({
      target: '_blank',
      rel: 'noopener noreferrer'
    }, markdown.externalLinks))
    .use(hoistScriptStyle)
    .use(containers)

    // 3rd party plugins
    .use(emoji)
    .use(anchor, Object.assign({
      slugify,
      permalink: true,
      permalinkBefore: true,
      permalinkSymbol: '#'
    }, markdown.anchor))
    .use(toc, Object.assign({
      slugify,
      includeLevel: [2, 3],
      format: parseHeaders
    }, markdown.toc))

  // apply user config
  if (markdown.config) {
    markdown.config(md)
  }

  if (markdown.lineNumbers) {
    md.use(lineNumbers)
  }

  module.exports.dataReturnable(md)

  // expose slugify
  md.slugify = slugify

  return md
}

module.exports.dataReturnable = function dataReturnable (md) {
  // override render to allow custom plugins return data
  const render = md.render
  md.render = (...args) => {
    md.__data = {}
    const html = render.call(md, ...args)
    return {
      html,
      data: md.__data
    }
  }
}