Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -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"],
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/inline-styles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
**bold with asterisks**

__bold with underscores__

<strong>bold with html</strong>

*italic with asterisks*

_italic with underscores_

<em>italic with html</em>

**bold *and italic* together**

<strong>html bold with <em>html italic</em> inside</strong>

Text with \#escaped hash and \*escaped asterisk\* here
31 changes: 20 additions & 11 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
});
});
});
Loading