Skip to content

Commit

Permalink
fix: improve code block parser
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Aug 13, 2024
1 parent 1f8e4a3 commit e72cb2d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/renderer/src/lib/parse-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,27 @@ function extractCodeFromHtml(htmlString: string) {
// 2. line wrapper like <span><span>...</span></span>
const spanElements = tempDiv.querySelectorAll("span > span")

// 2.1 outside <span /> as a line break?

let spanAsLineBreak = false

if (tempDiv.children.length > 2) {
for (const node of tempDiv.children) {
const span = node as HTMLSpanElement
// 2.2 If the span has only one child and it's a line break, then span can be as a line break
spanAsLineBreak =
span.children.length === 1 &&
span.childNodes.item(0).textContent === "\n"
if (spanAsLineBreak) break
}
}
if (spanElements.length > 0) {
for (const node of tempDiv.children) {
code += `${node.textContent}\n`
if (spanAsLineBreak) {
code += `${node.textContent}`
} else {
code += `${node.textContent}\n`
}
}

return code
Expand Down

0 comments on commit e72cb2d

Please sign in to comment.