index.js 1021 B

12345678910111213141516171819202122232425262728293031323334
  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. import marked from './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.markdown) {
  14. return marked(content)
  15. }
  16. }
  17. Markdown.prototype.onParse = function (node, vm) {
  18. if (vm.options.markdown) {
  19. // 中文 id 需要转换,否则无法跳转
  20. if (vm.options.useAnchor && node.attrs && /[\u4e00-\u9fa5]/.test(node.attrs.id)) {
  21. const id = 't' + index++
  22. this.vm._ids[node.attrs.id] = id
  23. node.attrs.id = id
  24. }
  25. 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') {
  26. node.attrs.class = `md-${node.name} ${node.attrs.class || ''}`
  27. }
  28. }
  29. }
  30. export default Markdown