DocsComp.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <script lang="ts" setup>
  2. import { ref } from 'vue'
  3. import markdownit from 'markdown-it'
  4. import hljs from 'highlight.js'
  5. import 'highlight.js/styles/atom-one-light.css'
  6. const md: markdownit = markdownit({
  7. highlight: function (str: string, lang: string) {
  8. if (lang && hljs.getLanguage(lang)) {
  9. try {
  10. return (
  11. '<pre><code class="hljs">' +
  12. hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
  13. '</code></pre>'
  14. )
  15. } catch (__) {
  16. // do nothing
  17. }
  18. }
  19. return '<pre><code class="hljs">' + md.utils.escapeHtml(str) + '</code></pre>'
  20. }
  21. })
  22. const html = ref('')
  23. fetch('//cdn.jsdelivr.net/npm/chinese-days/README.md')
  24. .then((response) => {
  25. if (!response.ok) {
  26. throw new Error('Network response was not ok ' + response.statusText)
  27. }
  28. return response.text()
  29. })
  30. .then((data) => {
  31. html.value = md.render(data) as string
  32. // console.log(data)
  33. })
  34. .catch((error) => {
  35. console.error('There has been a problem with your fetch operation:', error)
  36. })
  37. </script>
  38. <template>
  39. <div class="markdown-body" v-html="html"></div>
  40. </template>
  41. <style>
  42. .markdown-body {
  43. max-width: 700px;
  44. padding: 0 20px;
  45. margin: 0 auto;
  46. text-align: left;
  47. line-height: 1.7;
  48. .hljs {
  49. border-radius: 10px;
  50. border: 1px solid #ddd;
  51. }
  52. h1,
  53. h2,
  54. h3,
  55. h4,
  56. h5,
  57. h6,
  58. p {
  59. margin: 20px 0;
  60. }
  61. p {
  62. & > img {
  63. display: inline;
  64. }
  65. & > a {
  66. display: inline;
  67. & > img {
  68. display: inline;
  69. }
  70. }
  71. }
  72. a {
  73. text-decoration: underline;
  74. display: inline;
  75. }
  76. ol, ul {
  77. padding-left: 20px;
  78. }
  79. li {
  80. list-style: circle;
  81. margin-bottom: 10px;
  82. }
  83. }
  84. </style>