render.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Vditor from '../src/method'
  2. import '../src/assets/less/index.less'
  3. const outlineElement = document.getElementById('outline')
  4. const previewElement = document.getElementById('preview')
  5. document.querySelector('.markdown-list').
  6. addEventListener('click', function (event) {
  7. if (event.target.tagName === 'SPAN') {
  8. render(event.target.getAttribute('data-file'))
  9. }
  10. })
  11. previewElement.addEventListener('click', function (event) {
  12. if (event.target.tagName === 'IMG') {
  13. Vditor.previewImage(event.target, 'zh_CN',
  14. outlineElement.classList.contains('dark') ? 'dark' : 'classic')
  15. }
  16. })
  17. const render = (fileName) => {
  18. fetch(`/demo/markdown/${fileName}.md`).
  19. then(response => response.text()).
  20. then(markdown => {
  21. Vditor.preview(previewElement, markdown, {
  22. markdown: {
  23. toc: true,
  24. listStyle: fileName === 'cute-list',
  25. },
  26. speech: {
  27. enable: true,
  28. },
  29. anchor: 1,
  30. after () {
  31. if (window.innerWidth <= 768) {
  32. return
  33. }
  34. Vditor.outlineRender(previewElement, outlineElement)
  35. if (outlineElement.innerText.trim() !== '') {
  36. outlineElement.style.display = 'block'
  37. }
  38. },
  39. lazyLoadImage: 'https://unpkg.com/vditor/dist/images/img-loading.svg',
  40. renderers: {
  41. renderHeading: (node, entering) => {
  42. const id = Lute.GetHeadingID(node)
  43. if (entering) {
  44. return [
  45. `<h${node.__internal_object__.HeadingLevel} id="${id}" class="vditor__heading"><span class="prefix"></span><span>`,
  46. Lute.WalkContinue]
  47. } else {
  48. return [
  49. `</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}>`,
  50. Lute.WalkContinue]
  51. }
  52. },
  53. },
  54. })
  55. })
  56. }
  57. render('zh_CN')
  58. window.setTheme = (theme) => {
  59. if (theme === 'dark') {
  60. Vditor.setCodeTheme('native')
  61. Vditor.setContentTheme('dark',
  62. 'https://unpkg.com/vditor/dist/css/content-theme')
  63. outlineElement.classList.add('dark')
  64. document.querySelector('html').style.backgroundColor = '#2f363d'
  65. } else {
  66. Vditor.setCodeTheme('github')
  67. Vditor.setContentTheme('light',
  68. 'https://unpkg.com/vditor/dist/css/content-theme')
  69. outlineElement.classList.remove('dark')
  70. document.querySelector('html').style.backgroundColor = '#fff'
  71. }
  72. }