Skip to content

Commit 0604b05

Browse files
committed
fix(core): keep backslashes before escaped HTML-like text
Text like \<details> exported as \\<details>, which the parser reads as a literal backslash followed by raw HTML, so the text was lost. Double the backslashes right before an escaped < so they load back unchanged. Refs #3189
1 parent 0bdea2f commit 0604b05

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎packages/core/src/api/exporters/markdown/htmlToMarkdown.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,10 +849,14 @@ function serializeInlineContent(el: Element): string {
849849
/**
850850
* Escape `<` where the markdown parser would read raw HTML or an autolink, so
851851
* text like `<details>` loads back as text. A `<` before a space or digit
852-
* stays as is, as in `1 < 2`.
852+
* stays as is, as in `1 < 2`. Backslashes right before an escaped `<` are
853+
* doubled too, so `\<details>` doesn't become an escaped backslash plus HTML.
853854
*/
854855
function escapeHtmlLikeText(text: string): string {
855-
return text.replace(/<(?=[A-Za-z/!?])/g, "\\<");
856+
return text.replace(
857+
/(\\*)<(?=[A-Za-z/!?])/g,
858+
(_, backslashes: string) => `${backslashes}${backslashes}\\<`,
859+
);
856860
}
857861

858862
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"children": [],
4+
"content": [
5+
{
6+
"styles": {},
7+
"text": "Escaped \<details> and \\<b> stay text, as does C:\Users.",
8+
"type": "text",
9+
},
10+
],
11+
"id": "1",
12+
"props": {
13+
"backgroundColor": "default",
14+
"textAlignment": "left",
15+
"textColor": "default",
16+
},
17+
"type": "paragraph",
18+
},
19+
]

‎tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,19 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance<
513513
},
514514
executeTest: testExportParseEqualityMarkdown,
515515
},
516+
{
517+
testCase: {
518+
name: "markdown/paragraphWithBackslashBeforeHtmlLikeText",
519+
content: [
520+
{
521+
type: "paragraph",
522+
content:
523+
"Escaped \\<details> and \\\\<b> stay text, as does C:\\Users.",
524+
},
525+
],
526+
},
527+
executeTest: testExportParseEqualityMarkdown,
528+
},
516529
{
517530
testCase: {
518531
name: "markdown/nestedLists",

0 commit comments

Comments
 (0)