Skip to content

Commit

Permalink
fix: don't dedent contents of html comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Dec 28, 2024
1 parent f782892 commit 90c1202
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ const plugin: Plugin = {
indent = lastLine.match(useTabs ? /^\t*/ : /^ */)![0].length
}

return contents[0]!.replace(new RegExp(`^${useTabs ? '\t' : ' '}{0,${indent}}`, 'gm'), '')
const comments: { placeholder: string; content: string }[] = []
let commentIndex = 0

const dedentedContent = contents[0]!
.replace(/<!--([^]*?)-->/g, (match) => {
const placeholder = `__HTML_COMMENT_${commentIndex++}__`
comments.push({ placeholder, content: match })
return placeholder
})
.replace(new RegExp(`^${useTabs ? '\t' : ' '}{0,${indent}}`, 'gm'), '')

let finalContent = dedentedContent
comments.forEach(({ placeholder, content }) => {
finalContent = finalContent.replace(placeholder, content)
})

return finalContent
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Comments <!-- comment -->
comment
-->
<!-- <span
<!-- <span
:style="{
'--vp-hue': hue,
}"
Expand Down

0 comments on commit 90c1202

Please sign in to comment.