render.js 3.0 KB

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