diff --git a/src/processor.ts b/src/processor.ts index eaaa77b..047a227 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -426,6 +426,24 @@ const keepMarkerPlugin = (option: { doc: string }) => { return transformer; }; +const restoreEscapes = (option: { doc: string }) => { + const { doc } = option; + const transformer = (ast: HastRoot) => { + visitParents( + ast, + (node) => node.type === "text" && !!node.position, + (node: any, _parent: any) => { + const { start, end } = node.position; + const original = doc.slice(start.offset, end.offset); + if (original.includes("\\") && original.replace(/\\/g, "") === node.value) { + node.value = original; + } + }, + ); + }; + return transformer; +}; + const postProcessHtmlMarker = (option: { doc: string }) => { const { doc } = option; const allowHtmlTags = ["summary"]; @@ -852,6 +870,7 @@ class MdProcessor extends Processor { const hast = unified() .use(keepMarkerPlugin, { doc: newDoc }) + .use(restoreEscapes, { doc: newDoc }) .use(prepareMdast, { contentsAvoidMarkdown }) .use(remark2rehype, { passThrough: ["definition"], diff --git a/test/fixtures/inline-styles.md b/test/fixtures/inline-styles.md new file mode 100644 index 0000000..09817fc --- /dev/null +++ b/test/fixtures/inline-styles.md @@ -0,0 +1,17 @@ +**bold with asterisks** + +__bold with underscores__ + +bold with html + +*italic with asterisks* + +_italic with underscores_ + +italic with html + +**bold *and italic* together** + +html bold with html italic inside + +Text with \#escaped hash and \*escaped asterisk\* here diff --git a/test/index.spec.ts b/test/index.spec.ts index 0c2f448..d4f30c6 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -24,23 +24,17 @@ function processAndCompare(filename: string) { console.log(filename); } -function processAndCompareWithExpected(filename: string) { - const inDoc = eol.lf( +function processAndCompareRoundtrip(filename: string) { + const input = eol.lf( fs.readFileSync(path.join("test", "fixtures", filename), { encoding: "utf-8", }), ); - const inDocExpected = eol.lf( - fs.readFileSync(path.join("test", "expected", filename), { - encoding: "utf-8", - }), - ); - const doc = processor.parse(inDoc); - const outDoc = processor.stringify(doc); + const doc = processor.parse(input); + const output = eol.lf(processor.stringify(doc)); - assert.equal(outDoc, inDocExpected); - console.log(filename); + assert.equal(output, input); } describe("MdProcessorTest", function () { @@ -72,4 +66,19 @@ describe("MdProcessorTest", function () { processAndCompare(filename); }); }); + + const roundtripFiles = [ + "link.md", + "images.md", + "images-html.md", + "break.md", + "description-list.md", + "inline-styles.md", + ]; + + roundtripFiles.forEach((filename) => { + it(`should roundtrip ${filename} exactly`, function () { + processAndCompareRoundtrip(filename); + }); + }); });