Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
poc-fe
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poc
poc-fe
Commits
5b7c549b
Commit
5b7c549b
authored
Jun 02, 2025
by
nick zheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: 排除mermaid渲染的svg标签净化
parent
838771d1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
1 deletion
+30
-1
markdown-parser.ts
src/components/markdown-render/utils/markdown-parser.ts
+30
-1
No files found.
src/components/markdown-render/utils/markdown-parser.ts
View file @
5b7c549b
...
...
@@ -17,6 +17,7 @@ const katexOptions: MarkedKatexOptions = {
output
:
'html'
,
}
// Mermaid初始化
mermaid
.
initialize
({
startOnLoad
:
false
,
htmlLabels
:
true
,
...
...
@@ -167,12 +168,40 @@ export function createMarkedInst() {
},
postprocess
(
html
:
string
)
{
initMermaidPanzoom
()
return
DOMPurify
.
sanitize
(
html
)
return
getSanitizedHtml
(
html
)
},
},
})
}
// 获取净化HTML, 排除Mermaid render的svg标签
function
getSanitizedHtml
(
html
:
string
)
{
const
mermaidElementRegex
=
/<div class="mermaid-viewer"><svg
(
.*
?)
>
(
.*
?)
<
\/
svg><
\/
div>/g
s
const
mermaidObjects
:
{
placeholder
:
string
;
content
:
string
}[]
=
[]
let
processedHtml
=
html
let
match
:
RegExpExecArray
|
null
=
null
let
i
=
0
while
((
match
=
mermaidElementRegex
.
exec
(
html
))
!==
null
)
{
const
fullMatch
=
match
[
0
]
const
placeholder
=
`MERMAID_PLACEHOLDER_
${
i
}
`
mermaidObjects
.
push
({
placeholder
,
content
:
fullMatch
})
processedHtml
=
processedHtml
.
replace
(
fullMatch
,
placeholder
)
i
++
}
const
sanitizedHtmlWithoutMermaid
=
DOMPurify
.
sanitize
(
processedHtml
)
let
sanitizedHtml
=
sanitizedHtmlWithoutMermaid
for
(
const
mermaidItem
of
mermaidObjects
)
{
sanitizedHtml
=
sanitizedHtml
.
replace
(
mermaidItem
.
placeholder
,
mermaidItem
.
content
)
}
return
sanitizedHtml
}
export
function
markdownParser
(
markdown
:
string
)
{
return
createMarkedInst
().
parse
(
markdown
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment