index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * @fileoverview markdown 插件
  3. * Include marked (https://github.com/markedjs/marked)
  4. * Include github-markdown-css (https://github.com/sindresorhus/github-markdown-css)
  5. */
  6. const { marked } = require('./marked.min')
  7. let index = 0
  8. function Markdown (vm) {
  9. this.vm = vm
  10. vm._ids = {}
  11. }
  12. Markdown.prototype.onUpdate = function (content) {
  13. if (this.vm.properties.markdown) {
  14. // 解决中文标点符号后粗体失效的问题,增加零宽空格
  15. content = content.replace(/\*\*([^*]+)\*\*([,。!?;:])/g, '**$1**​$2')
  16. return marked(content)
  17. }
  18. }
  19. Markdown.prototype.onParse = function (node, vm) {
  20. if (vm.options.markdown) {
  21. // 中文 id 需要转换,否则无法跳转
  22. if (vm.options.useAnchor && node.attrs && /[\u4e00-\u9fa5]/.test(node.attrs.id)) {
  23. const id = 't' + index++
  24. this.vm._ids[node.attrs.id] = id
  25. node.attrs.id = id
  26. }
  27. if (node.name === 'p' || node.name === 'table' || node.name === 'tr' || node.name === 'th' || node.name === 'td' || node.name === 'blockquote' || node.name === 'pre' || node.name === 'code') {
  28. node.attrs.class = `md-${node.name} ${node.attrs.class || ''}`
  29. }
  30. }
  31. }
  32. module.exports = Markdown