Skip to content

Commit 76670cc

Browse files
committed
fix: escape code in code placeholder
1 parent 49823b4 commit 76670cc

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ type NotNull<T> = T extends null | undefined ? never : T
128128

129129
const wrappedSet = new WeakSet<NotNull<Options['highlight']>>()
130130

131+
function escapeHtml(unsafe: string): string {
132+
return unsafe
133+
.replace(/&/g, '&amp;')
134+
.replace(/</g, '&lt;')
135+
.replace(/>/g, '&gt;')
136+
.replace(/"/g, '&quot;')
137+
.replace(/'/g, '&#039;')
138+
}
139+
131140
function wrapHightlight(highlight: MarkdownItAsyncOptions['highlight'], map: MarkdownItAsyncPlaceholderMap): Options['highlight'] {
132141
if (!highlight)
133142
return undefined
@@ -141,7 +150,11 @@ function wrapHightlight(highlight: MarkdownItAsyncOptions['highlight'], map: Mar
141150
return promise
142151
const id = randStr()
143152
map.set(id, [promise, str, lang, attrs])
144-
return placeholder(id, str)
153+
let code = str
154+
if (code.endsWith('\n'))
155+
code = code.slice(0, -1)
156+
code = escapeHtml(code)
157+
return placeholder(id, code)
145158
}
146159

147160
wrappedSet.add(wrapped)

0 commit comments

Comments
 (0)