From 90c12028c01faff09f23c1cb05c95f250ac07cfb Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sat, 28 Dec 2024 16:01:38 +0530 Subject: [PATCH] fix: don't dedent contents of html comments --- src/index.ts | 18 +++++++++++++++++- tests/index.test.ts | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a9fe0e2..52507de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } }, }, diff --git a/tests/index.test.ts b/tests/index.test.ts index 4cd9989..d3d895a 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -170,7 +170,7 @@ Comments comment --> -