Skip to content

Commit 4a10c81

Browse files
committed
fix(blog): 优化Markdown部分插件语法处理
1 parent b49f525 commit 4a10c81

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/components/forum/ForumTopic.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import { getTopicTypeMap } from '~/composables/getTopicTypeMap'
106106
import { useToggle } from '@vueuse/core'
107107
import { useLocalized } from '@/hooks/useLocalized'
108108
import { sessionCacheRedirect } from '~/composables/sessionCacheRedirect'
109+
import { sanitizeMarkdown } from '~/composables/sanitizeMarkdown'
109110
110111
const userInfo = useUserInfoStore()
111112
const topicTypeMap = getTopicTypeMap()
@@ -122,9 +123,7 @@ const { title, author, topic } = defineProps<{
122123
const [isExpanded, toggleExpand] = useToggle()
123124
124125
const renderText = computed(() => {
125-
const contentSanitized = topic.contentRaw
126-
.replace(/!\[.*?\]\(.*?\)/g, '')
127-
.replace(/<!--.*(?=-->)-->/giu, '')
126+
const contentSanitized = sanitizeMarkdown(topic.contentRaw)
128127
if (isAnn.value) return contentSanitized
129128
return contentSanitized.slice(0, isExpanded.value ? undefined : 180)
130129
})

src/components/forum/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { catchError } from '@/apis/utils'
22
import { useUrlSearchParams } from '@vueuse/core'
3+
import { sanitizeMarkdown } from '~/composables/sanitizeMarkdown'
34

45
export function transformLabelsToArray(labels: GITEE.IssueLabel[]) {
56
const arr: string[] = []
@@ -157,6 +158,9 @@ export function extractPlainText(input: string): string {
157158
.replace(/([*_]{1,3}|~{2}|`{1,3}|#+|!\[|\]|\[|\]|\(|\)|>)/g, '') // Remove Markdown special characters
158159
.replace(/\s*[-+*] /g, '') // Remove list markers
159160
.replace(/\n{2,}/g, '\n') // Normalize multiple newlines
160-
console.log(markdownToPlainText, htmlEntityDecode, htmlToNewline)
161-
return markdownToPlainText.trim()
161+
162+
// Step 4: Sanitize to remove markdown plugin syntax
163+
const plaintextSanitized = sanitizeMarkdown(markdownToPlainText)
164+
165+
return plaintextSanitized.trim()
162166
}

src/composables/sanitizeMarkdown.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export const sanitizeMarkdown = (
22
markdown: string | null | undefined,
33
): string => {
44
return (markdown || '')
5-
.replace(/<!--.*(?=-->)-->/giu, '')
65
.replaceAll(/!\[(.*?)\]\((.*?)(?:\s+"(.*?)")?\s*\)/g, '')
6+
.replace(/<!--.*(?=-->)-->/giu, '')
7+
.replace(/\{define:[^}]*\}.*?\{\/define\}/giu, '')
8+
.replace(/\{color:[^}]*\}(.*?)\{\/color\}/giu, '$1')
79
}

0 commit comments

Comments
 (0)