diff --git a/.changeset/quiet-ticks-keep.md b/.changeset/quiet-ticks-keep.md new file mode 100644 index 00000000..176aacc4 --- /dev/null +++ b/.changeset/quiet-ticks-keep.md @@ -0,0 +1,5 @@ +--- +"@stackoverflow/stacks-editor": patch +--- + +Stop inline code fences from growing on each Markdown/Rich Text round trip, and keep the full image alt text when it contains escapes, entities or formatting diff --git a/src/shared/markdown-parser.ts b/src/shared/markdown-parser.ts index 15b26e92..ad5a46a4 100644 --- a/src/shared/markdown-parser.ts +++ b/src/shared/markdown-parser.ts @@ -118,7 +118,10 @@ const customMarkdownParserTokens: MarkdownParser["tokens"] = { src: tok.attrGet("src"), width: tok.attrGet("width"), height: tok.attrGet("height"), - alt: tok.attrGet("alt") || tok.children?.[0]?.content || null, + alt: + tok.attrGet("alt") || + tok.children?.map((child) => child.content).join("") || + null, title: tok.attrGet("title"), }; diff --git a/src/shared/markdown-serializer.ts b/src/shared/markdown-serializer.ts index 1c4da3e6..a6f90485 100644 --- a/src/shared/markdown-serializer.ts +++ b/src/shared/markdown-serializer.ts @@ -588,6 +588,23 @@ const extendedLinkMarkDeserializer: typeof defaultLinkMarkDeserializer = { }, }; +/** + * Applies the original markup of a code mark to the delimiter computed by the default serializer + * @param defaultResult The default delimiter, e.g. "`" or "``` " when the code contains backticks + * @param markup The markup the code mark was parsed from, e.g. "``" or "" + */ +function applyCodeMarkup(defaultResult: string, markup: string): string { + if (!/^`+$/.test(markup)) { + return defaultResult.replace("`", markup); + } + + // the default delimiter is already long enough for any backticks in the code; + // only lengthen it to keep a longer original fence, never add to it + return defaultResult.replace(/`+/, (ticks) => + markup.length > ticks.length ? markup : ticks + ); +} + // add support for markup const defaultCodeMarkDeserializer = defaultMarkdownSerializer.marks.code; const extendedCodeMarkDeserializer: typeof defaultCodeMarkDeserializer = { @@ -608,8 +625,8 @@ const extendedCodeMarkDeserializer: typeof defaultCodeMarkDeserializer = { ); if (mark.attrs.markup) { - defaultResult = defaultResult.replace( - "`", + defaultResult = applyCodeMarkup( + defaultResult, mark.attrs.markup as string ); } @@ -635,7 +652,7 @@ const extendedCodeMarkDeserializer: typeof defaultCodeMarkDeserializer = { if (mark.attrs.markup) { // insert the `/` on html closing tags const markup = (mark.attrs.markup as string).replace(/^ { `alt1`, `alt1`, ``, + // alt text keeps all of its inline content + [ + `![my *cool* pic](https://example.com/a.png)`, + `![my cool pic](https://example.com/a.png)`, + ], + [ + `![Tom & Jerry](https://example.com/a.png)`, + `![Tom & Jerry](https://example.com/a.png)`, + ], + [ + `![C\\# logo](https://example.com/a.png)`, + `![C# logo](https://example.com/a.png)`, + ], + [ + `![array[0]](https://example.com/a.png)`, + `![array\\[0\\]](https://example.com/a.png)`, + ], + `![array\\[0\\]](https://example.com/a.png)`, // attributes render in alpha order [ ``, @@ -176,6 +194,10 @@ describe("markdown-serializer", () => { `test`, `test`, "`test`", + "``test``", + "`` `test` ``", + ["`a``b`", "``` a``b ```"], + "``` a``b ```", "test", `[test](${crazyTestUrl} "title1")`, `<${crazyTestUrl}>`,