Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/quiet-ticks-keep.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion src/shared/markdown-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};

Expand Down
23 changes: 20 additions & 3 deletions src/shared/markdown-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<code>"
*/
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 <code> markup
const defaultCodeMarkDeserializer = defaultMarkdownSerializer.marks.code;
const extendedCodeMarkDeserializer: typeof defaultCodeMarkDeserializer = {
Expand All @@ -608,8 +625,8 @@ const extendedCodeMarkDeserializer: typeof defaultCodeMarkDeserializer = {
);

if (mark.attrs.markup) {
defaultResult = defaultResult.replace(
"`",
defaultResult = applyCodeMarkup(
defaultResult,
mark.attrs.markup as string
);
}
Expand All @@ -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(/^</, "</");
defaultResult = defaultResult.replace("`", markup);
defaultResult = applyCodeMarkup(defaultResult, markup);
}

return defaultResult;
Expand Down
22 changes: 22 additions & 0 deletions test/shared/markdown-serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ describe("markdown-serializer", () => {
`<img alt="alt1" src="src1" title="title1"/>`,
`<img alt="alt1" src="src1" title="title1" />`,
`<img height="10" src="src1" width="10" />`,
// 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 &amp; 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
[
`<img src="src1" width="10" height="10" />`,
Expand Down Expand Up @@ -176,6 +194,10 @@ describe("markdown-serializer", () => {
`<sup>test</sup>`,
`<sub>test</sub>`,
"`test`",
"``test``",
"`` `test` ``",
["`a``b`", "``` a``b ```"],
"``` a``b ```",
"<code>test</code>",
`[test](${crazyTestUrl} "title1")`,
`<${crazyTestUrl}>`,
Expand Down