render.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Vditor from '../src/method'
  2. import '../src/assets/scss/index.scss'
  3. document.querySelector('.markdown-list').
  4. addEventListener('click', function (event) {
  5. if (event.target.tagName === 'SPAN') {
  6. render(event.target.getAttribute('data-file'))
  7. }
  8. })
  9. const render = (fileName) => {
  10. fetch(`/demo/markdown/${fileName}.md`).
  11. then(response => response.text()).
  12. then(markdown => {
  13. Vditor.preview(document.getElementById('preview'),
  14. markdown, {
  15. markdown: {
  16. toc: true,
  17. listStyle: fileName === 'cute-list',
  18. },
  19. speech: {
  20. enable: true,
  21. },
  22. anchor: 1,
  23. after () {
  24. if (window.innerWidth <= 768) {
  25. return
  26. }
  27. const outlineElement = document.getElementById('outline')
  28. Vditor.outlineRender(document.getElementById('preview'),
  29. outlineElement)
  30. if (outlineElement.innerText.trim() !== '') {
  31. outlineElement.style.display = 'block'
  32. }
  33. },
  34. lazyLoadImage: 'https://cdn.jsdelivr.net/npm/vditor/dist/images/img-loading.svg',
  35. renderers: {
  36. renderHeading: (node, entering) => {
  37. const id = Lute.GetHeadingID(node)
  38. if (entering) {
  39. return [
  40. `<h${node.__internal_object__.HeadingLevel} id="${id}" class="vditor__heading">
  41. <span class="prefix"></span><span>`,
  42. Lute.WalkContinue]
  43. } else {
  44. return [
  45. `</span><a id="vditorAnchor-${id}" class="vditor-anchor" href="#${id}"><svg viewBox="0 0 16 16" version="1.1" width="16" height="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a></h${node.__internal_object__.HeadingLevel}>`,
  46. Lute.WalkContinue]
  47. }
  48. },
  49. },
  50. })
  51. })
  52. }
  53. render('zh_CN')
  54. window.setTheme = (theme) => {
  55. const outlineElement = document.getElementById('outline')
  56. if (theme === 'dark') {
  57. Vditor.setCodeTheme('native')
  58. Vditor.setContentTheme('dark', 'https://cdn.jsdelivr.net/npm/vditor/dist/css/content-theme')
  59. outlineElement.classList.add('dark')
  60. document.querySelector('html').style.backgroundColor = '#2f363d'
  61. } else {
  62. Vditor.setCodeTheme('github')
  63. Vditor.setContentTheme('light', 'https://cdn.jsdelivr.net/npm/vditor/dist/css/content-theme')
  64. outlineElement.classList.remove('dark')
  65. document.querySelector('html').style.backgroundColor = '#fff'
  66. }
  67. }