|
| 1 | +<template> |
| 2 | + <div class="markdown markdown-github"> |
| 3 | + <PerfectScrollbar> |
| 4 | + <div v-html="renderer" /> |
| 5 | + </PerfectScrollbar> |
| 6 | + </div> |
| 7 | +</template> |
| 8 | + |
| 9 | +<script setup lang="ts"> |
| 10 | +import { useAppStore } from '@/store/app' |
| 11 | +import { useSnippetStore } from '@/store/snippets' |
| 12 | +import MarkdownIt from 'markdown-it' |
| 13 | +import sanitizeHtml from 'sanitize-html' |
| 14 | +import hljs from 'highlight.js' |
| 15 | +import mila from 'markdown-it-link-attributes' |
| 16 | +import 'highlight.js/styles/github.css' |
| 17 | +import { computed, onBeforeUnmount, onMounted, ref } from 'vue' |
| 18 | +import { ipc } from '@/electron' |
| 19 | +
|
| 20 | +interface Props { |
| 21 | + value: string |
| 22 | +} |
| 23 | +
|
| 24 | +const props = defineProps<Props>() |
| 25 | +
|
| 26 | +const appStore = useAppStore() |
| 27 | +const snippetStore = useSnippetStore() |
| 28 | +
|
| 29 | +let md: MarkdownIt |
| 30 | +const forceRefresh = ref() |
| 31 | +
|
| 32 | +const init = () => { |
| 33 | + md = new MarkdownIt({ |
| 34 | + html: true, |
| 35 | + langPrefix: 'language-', |
| 36 | + highlight (str, lang) { |
| 37 | + if (lang && hljs.getLanguage(lang)) { |
| 38 | + try { |
| 39 | + return `<pre class="hljs"><code>${ |
| 40 | + hljs.highlight(str, { |
| 41 | + language: lang, |
| 42 | + ignoreIllegals: true |
| 43 | + }).value |
| 44 | + }</code></pre>` |
| 45 | + } catch (err) { |
| 46 | + console.log(err) |
| 47 | + } |
| 48 | + } |
| 49 | + return `<pre class="hljs"><code>${MarkdownIt().utils.escapeHtml( |
| 50 | + str |
| 51 | + )}</code></pre>` |
| 52 | + } |
| 53 | + }) |
| 54 | +
|
| 55 | + md.use(mila, { |
| 56 | + attrs: { |
| 57 | + class: 'external' |
| 58 | + } |
| 59 | + }) |
| 60 | +} |
| 61 | +
|
| 62 | +const getRenderer = () => { |
| 63 | + const raw = md?.render(props.value) |
| 64 | + const html = sanitizeHtml(raw, { |
| 65 | + allowedTags: false, |
| 66 | + allowedAttributes: { |
| 67 | + '*': [ |
| 68 | + 'align', |
| 69 | + 'alt', |
| 70 | + 'height', |
| 71 | + 'href', |
| 72 | + 'name', |
| 73 | + 'src', |
| 74 | + 'target', |
| 75 | + 'width', |
| 76 | + 'class' |
| 77 | + ] |
| 78 | + } |
| 79 | + }) |
| 80 | + return html |
| 81 | +} |
| 82 | +
|
| 83 | +const renderer = computed(() => getRenderer()) |
| 84 | +
|
| 85 | +const openExternal = (e: Event) => { |
| 86 | + const el = e.target as HTMLAnchorElement |
| 87 | + if (el.classList.contains('external')) { |
| 88 | + e.preventDefault() |
| 89 | + ipc.invoke('main:open-url', el.href) |
| 90 | + } |
| 91 | +} |
| 92 | +
|
| 93 | +const height = computed(() => { |
| 94 | + // eslint-disable-next-line no-unused-expressions |
| 95 | + forceRefresh.value |
| 96 | +
|
| 97 | + let result = |
| 98 | + appStore.sizes.editor.titleHeight + |
| 99 | + appStore.sizes.titlebar + |
| 100 | + appStore.sizes.editor.footerHeight |
| 101 | +
|
| 102 | + if (snippetStore.isFragmentsShow) { |
| 103 | + result += appStore.sizes.editor.fragmentsHeight |
| 104 | + } |
| 105 | +
|
| 106 | + if (snippetStore.isTagsShow) { |
| 107 | + result += appStore.sizes.editor.tagsHeight |
| 108 | + } |
| 109 | +
|
| 110 | + return window.innerHeight - result + 'px' |
| 111 | +}) |
| 112 | +
|
| 113 | +init() |
| 114 | +
|
| 115 | +onMounted(() => { |
| 116 | + document.addEventListener('click', openExternal) |
| 117 | +}) |
| 118 | +
|
| 119 | +onBeforeUnmount(() => { |
| 120 | + document.removeEventListener('click', openExternal) |
| 121 | +}) |
| 122 | +
|
| 123 | +window.addEventListener('resize', () => { |
| 124 | + forceRefresh.value = Math.random() |
| 125 | +}) |
| 126 | +</script> |
| 127 | + |
| 128 | +<style lang="scss" scoped> |
| 129 | +.markdown { |
| 130 | + padding: 0 var(--spacing-xs); |
| 131 | + :deep(h1, h2, h3, h4, h5, h6) { |
| 132 | + &:first-child { |
| 133 | + margin-top: 0; |
| 134 | + } |
| 135 | + } |
| 136 | + :deep(.ps) { |
| 137 | + height: v-bind(height); |
| 138 | + } |
| 139 | +} |
| 140 | +</style> |
0 commit comments