Skip to content

Commit

Permalink
chore: upgrade renderer lib (#355)
Browse files Browse the repository at this point in the history
close #344
  • Loading branch information
yanglbme authored Aug 27, 2024
1 parent 8323604 commit bd31da9
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 606 deletions.
969 changes: 434 additions & 535 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"juice": "^8.0.0",
"katex": "^0.16.11",
"lucide-vue-next": "^0.428.0",
"marked": "^4.0.18",
"marked": "^14.1.0",
"marked-katex-extension": "^5.1.1",
"minio": "7.1.3",
"node-fetch": "^3.3.2",
Expand Down
2 changes: 0 additions & 2 deletions src/components/CodemirrorEditor/EditorHeader/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import { nextTick, reactive, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { ElNotification } from 'element-plus'
import CodeMirror from 'codemirror'
import { Command } from 'lucide-vue-next'
import PostInfo from './PostInfo.vue'
import FileDropdown from './FileDropdown.vue'
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/context-menu/ContextMenuCheckboxItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Check } from 'lucide-vue-next'
import { cn } from '@/lib/utils'
const props = defineProps<ContextMenuCheckboxItemProps & { class?: HTMLAttributes['class'] }>()
const props = defineProps<ContextMenuCheckboxItemProps & { class?: HTMLAttributes[`class`] }>()
const emits = defineEmits<ContextMenuCheckboxItemEmits>()
const delegatedProps = computed(() => {
Expand All @@ -30,7 +30,7 @@ const forwarded = useForwardPropsEmits(delegatedProps, emits)
props.class,
)"
>
<span class="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<span class="absolute left-2 h-3.5 w-3.5 flex items-center justify-center">
<ContextMenuItemIndicator>
<Check class="h-4 w-4" />
</ContextMenuItemIndicator>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/context-menu/ContextMenuContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'radix-vue'
import { cn } from '@/lib/utils'
const props = defineProps<ContextMenuContentProps & { class?: HTMLAttributes['class'] }>()
const props = defineProps<ContextMenuContentProps & { class?: HTMLAttributes[`class`] }>()
const emits = defineEmits<ContextMenuContentEmits>()
const delegatedProps = computed(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/context-menu/ContextMenuItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'radix-vue'
import { cn } from '@/lib/utils'
const props = defineProps<ContextMenuItemProps & { class?: HTMLAttributes['class'], inset?: boolean }>()
const props = defineProps<ContextMenuItemProps & { class?: HTMLAttributes[`class`], inset?: boolean }>()
const emits = defineEmits<ContextMenuItemEmits>()
const delegatedProps = computed(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/context-menu/ContextMenuLabel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type HTMLAttributes, computed } from 'vue'
import { ContextMenuLabel, type ContextMenuLabelProps } from 'radix-vue'
import { cn } from '@/lib/utils'
const props = defineProps<ContextMenuLabelProps & { class?: HTMLAttributes['class'], inset?: boolean }>()
const props = defineProps<ContextMenuLabelProps & { class?: HTMLAttributes[`class`], inset?: boolean }>()
const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/context-menu/ContextMenuRadioItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Circle } from 'lucide-vue-next'
import { cn } from '@/lib/utils'
const props = defineProps<ContextMenuRadioItemProps & { class?: HTMLAttributes['class'] }>()
const props = defineProps<ContextMenuRadioItemProps & { class?: HTMLAttributes[`class`] }>()
const emits = defineEmits<ContextMenuRadioItemEmits>()
const delegatedProps = computed(() => {
Expand All @@ -30,7 +30,7 @@ const forwarded = useForwardPropsEmits(delegatedProps, emits)
props.class,
)"
>
<span class="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<span class="absolute left-2 h-3.5 w-3.5 flex items-center justify-center">
<ContextMenuItemIndicator>
<Circle class="h-2 w-2 fill-current" />
</ContextMenuItemIndicator>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/context-menu/ContextMenuSeparator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from 'radix-vue'
import { cn } from '@/lib/utils'
const props = defineProps<ContextMenuSeparatorProps & { class?: HTMLAttributes['class'] }>()
const props = defineProps<ContextMenuSeparatorProps & { class?: HTMLAttributes[`class`] }>()
const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/context-menu/ContextMenuShortcut.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { HTMLAttributes } from 'vue'
import { cn } from '@/lib/utils'
const props = defineProps<{
class?: HTMLAttributes['class']
class?: HTMLAttributes[`class`]
}>()
</script>

Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/context-menu/ContextMenuSubContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'radix-vue'
import { cn } from '@/lib/utils'
const props = defineProps<DropdownMenuSubContentProps & { class?: HTMLAttributes['class'] }>()
const props = defineProps<DropdownMenuSubContentProps & { class?: HTMLAttributes[`class`] }>()
const emits = defineEmits<DropdownMenuSubContentEmits>()
const delegatedProps = computed(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/context-menu/ContextMenuSubTrigger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { ChevronRight } from 'lucide-vue-next'
import { cn } from '@/lib/utils'
const props = defineProps<ContextMenuSubTriggerProps & { class?: HTMLAttributes['class'], inset?: boolean }>()
const props = defineProps<ContextMenuSubTriggerProps & { class?: HTMLAttributes[`class`], inset?: boolean }>()
const delegatedProps = computed(() => {
const { class: _, ...delegated } = props
Expand Down
2 changes: 1 addition & 1 deletion src/config/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ const graceTheme = mergeTheme(defaultTheme, {
},

thead: {
'background': `linear-gradient(45deg, rgba(0, 152, 116, 0.9), rgba(0, 192, 146, 0.9))`,
'background': `rgba(0, 0, 0, 0.05)`,
'color': `#fff`,
'font-weight': `bold`,
},
Expand Down
123 changes: 75 additions & 48 deletions src/utils/wx-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@ class WxRenderer extends Renderer {

getStyles = (tokenName, addition = ``) => {
const dict = this.styleMapping[tokenName]
if (!dict)
if (!dict) {
return ``
}
const styles = Object.entries(dict)
.map(([key, value]) => `${key}:${value}`)
.join(`;`)
return `style="${styles}${addition}"`
}

styledContent = (styleLabel, content, label = styleLabel) => {
return `<${label} ${this.getStyles(styleLabel)}>${content}</${label}>`
}

addFootnote = (title, link) => {
this.footnotes.push([++this.footnoteIndex, title, link])
return this.footnoteIndex
Expand All @@ -77,9 +82,8 @@ class WxRenderer extends Renderer {
: `<code style="font-size: 90%; opacity: 0.6;">[${index}]</code> ${title}: <i style="word-break: break-all">${link}</i><br/>`,
)
.join(`\n`)
return `<h4 ${this.getStyles(`h4`)}>引用链接</h4><p ${this.getStyles(
`footnotes`,
)}>${footnoteArray}</p>`

return this.styledContent(`h4`, `引用链接`) + this.styledContent(`footnotes`, footnoteArray, `p`)
}

buildAddition = () => `
Expand All @@ -104,23 +108,29 @@ class WxRenderer extends Renderer {
this.styleMapping = this.buildTheme(this.opts.theme)
}

heading = (text, level) => {
const tag = `h${level}`
return `<${tag} ${this.getStyles(tag)}>${text}</${tag}>`
heading({ tokens, depth }) {
const text = this.parser.parseInline(tokens)
const tag = `h${depth}`
return this.styledContent(tag, text)
}

paragraph = (text) => {
paragraph({ tokens }) {
const text = this.parser.parseInline(tokens)
const isFigureImage = text.includes(`<figure`) && text.includes(`<img`)
const isEmpty = text.trim() === ``
return isFigureImage ? text : isEmpty ? `` : `<p ${this.getStyles(`p`)}>${text}</p>`
if (isFigureImage || isEmpty) {
return text
}
return this.styledContent(`p`, text)
}

blockquote = (text) => {
blockquote({ tokens }) {
let text = this.parser.parse(tokens)
text = text.replace(/<p.*?>/g, `<p ${this.getStyles(`blockquote_p`)}>`)
return `<blockquote ${this.getStyles(`blockquote`)}>${text}</blockquote>`
return this.styledContent(`blockquote`, text)
}

code = (text, lang = ``) => {
code({ text, lang }) {
if (lang.startsWith(`mermaid`)) {
setTimeout(() => {
window.mermaid?.run()
Expand All @@ -129,86 +139,103 @@ class WxRenderer extends Renderer {
}
const langText = lang.split(` `)[0]
const language = hljs.getLanguage(langText) ? langText : `plaintext`
text = hljs.highlight(text, { language }).value
text = text
let highlighted = hljs.highlight(text, { language }).value
highlighted = highlighted
.replace(/\r\n/g, `<br/>`)
.replace(/\n/g, `<br/>`)
.replace(/(>[^<]+)|(^[^<]+)/g, (str) => {
return str.replace(/\s/g, `&nbsp;`)
})
.replace(/(>[^<]+)|(^[^<]+)/g, str => str.replace(/\s/g, `&nbsp;`))

return `<pre class="hljs code__pre" ${this.getStyles(
`code_pre`,
)}><code class="language-${lang}" ${this.getStyles(
`code`,
)}>${text}</code></pre>`
)}>${highlighted}</code></pre>`
}

codespan = text => `<code ${this.getStyles(`codespan`)}>${text}</code>`

listitem = text => `<li ${this.getStyles(`listitem`)}><span><%s/></span>${text}</li>`

list = (text, ordered) => {
text = text.replace(/<\/*p.*?>/g, ``).replace(/<\/*p>/g, ``)
codespan({ text }) {
return this.styledContent(`codespan`, text, `code`)
}

const segments = text.split(`<%s/>`)
listitem(tokens, prefix) {
return `<li ${this.getStyles(`listitem`)}>${prefix}${this.parser.parseInline(tokens)}</li>`
}

if (!ordered) {
return `<ul ${this.getStyles(`ul`)}>${segments.join(`• `)}</ul>`
list({ ordered, items }) {
const listItems = []
for (let i = 0; i < items.length; i++) {
const { tokens } = items[i]
const prefix = ordered ? `${i + 1}. ` : `• `
listItems.push(this.listitem(tokens, prefix))
}

const orderedText = segments.map((segment, i) => (i > 0 ? `${i}. ` : ``) + segment).join(``)
return `<ol ${this.getStyles(`ol`)}>${orderedText}</ol>`
const label = ordered ? `ol` : `ul`
return this.styledContent(label, listItems.join(``))
}

image = (href, title, text) => {
const createSubText = s => s ? `<figcaption ${this.getStyles(`figcaption`)}>${s}</figcaption>` : ``
const transform = {
'alt': () => text,
'title': () => title,
'alt-title': () => text || title,
'title-alt': () => title || text,
}[this.opts.legend] || (() => ``)
image({ href, title, text }) {
const createSubText = s =>
s ? `<figcaption ${this.getStyles(`figcaption`)}>${s}</figcaption>` : ``
const transform
= {
'alt': () => text,
'title': () => title,
'alt-title': () => text || title,
'title-alt': () => title || text,
}[this.opts.legend] || (() => ``)

const subText = createSubText(transform())
const figureStyles = this.getStyles(`figure`)
const imgStyles = this.getStyles(`image`)

return `<figure ${figureStyles}><img ${imgStyles} src="${href}" title="${title}" alt="${text}"/>${subText}</figure>`
}

link = (href, title, text) => {
link({ href, title, text }) {
if (href.startsWith(`https://mp.weixin.qq.com`)) {
return `<a href="${href}" title="${title || text}" ${this.getStyles(
`wx_link`,
)}>${text}</a>`
}
if (href === text)
if (href === text) {
return text
}
if (this.opts.status) {
const ref = this.addFootnote(title || text, href)
return `<span ${this.getStyles(
`link`,
)}>${text}<sup>[${ref}]</sup></span>`
}
return `<span ${this.getStyles(`link`)}>${text}</span>`
return this.styledContent(`link`, text, `span`)
}

strong = text => `<strong ${this.getStyles(`strong`)}>${text}</strong>`
strong({ text }) {
return this.styledContent(`strong`, text)
}

em = text => `<span style="font-style: italic;">${text}</span>`
em({ text }) {
return `<span style="font-style: italic;">${text}</span>`
}

table = (header, body) => `
table({ header, rows }) {
const headerRow = header.map(cell => this.styledContent(`td`, cell.text)).join(``)
const body = rows.map((row) => {
const rowContent = row.map(cell => this.styledContent(`td`, cell.text)).join(``)
return this.styledContent(`tr`, rowContent)
}).join(``)
return `
<section style="padding:0 8px;">
<table class="preview-table">
<thead ${this.getStyles(`thead`)}>${header}</thead>
<thead ${this.getStyles(`thead`)}>${headerRow}</thead>
<tbody>${body}</tbody>
</table>
</section>`
}

tablecell = text => `<td ${this.getStyles(`td`)}>${text}</td>`
tablecell({ text }) {
return this.styledContent(`td`, text)
}

hr = () => `<hr ${this.getStyles(`hr`)}/>`
hr(_) {
return this.styledContent(`hr`, ``)
}
}

export default WxRenderer
9 changes: 1 addition & 8 deletions src/views/CodemirrorEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,14 @@ import RunLoading from '@/components/RunLoading.vue'
import {
ContextMenu,
ContextMenuCheckboxItem,
ContextMenuContent,
ContextMenuItem,
ContextMenuLabel,
ContextMenuRadioGroup,
ContextMenuRadioItem,
ContextMenuSeparator,
ContextMenuShortcut,
ContextMenuSub,
ContextMenuSubContent,
ContextMenuSubTrigger,
ContextMenuTrigger,
} from '@/components/ui/context-menu'
import { altKey, altSign, ctrlKey, ctrlSign, shiftKey, shiftSign } from '@/config'
import { altKey, altSign, ctrlKey, shiftKey, shiftSign } from '@/config'
import {
checkImage,
Expand Down

0 comments on commit bd31da9

Please sign in to comment.