diff --git a/shared/editor/lib/markdown/serializer.test.ts b/shared/editor/lib/markdown/serializer.test.ts index 1848a2f720..5f0bcaeab8 100644 --- a/shared/editor/lib/markdown/serializer.test.ts +++ b/shared/editor/lib/markdown/serializer.test.ts @@ -1,3 +1,4 @@ +import type { Mark } from "prosemirror-model"; import { Node } from "prosemirror-model"; import { parser, schema, serializer } from "../../../test/editor"; import { MarkdownSerializerState } from "./serializer"; @@ -199,6 +200,68 @@ describe("escaping", () => { expect(output).toContain("| 10.12.14.21 |"); }); + + it("escapes pipes in a plain URL link inside a table cell", () => { + const href = "https://example.edu/?q=a|b"; + const cell = (type: string, content: object[]) => ({ + type: "tr", + content: [{ type, content: [{ type: "paragraph", content }] }], + }); + const doc = Node.fromJSON(schema, { + type: "doc", + content: [ + { + type: "table", + content: [ + cell("th", [{ type: "text", text: "Link" }]), + cell("td", [ + { + type: "text", + text: href, + marks: [{ type: "link", attrs: { href } }], + }, + ]), + ], + }, + ], + }); + + const output = serializer.serialize(doc); + let link: Mark | undefined; + parser.parse(output)?.descendants((node) => { + link ??= node.marks.find((mark) => mark.type.name === "link"); + }); + + expect(output).toContain("| |"); + expect(decodeURI(link?.attrs.href)).toBe(href); + }); + + it("does not escape the text of a plain URL link", () => { + const href = "https://example.edu/~alice/"; + const doc = Node.fromJSON(schema, { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: href, + marks: [{ type: "link", attrs: { href } }], + }, + { type: "text", text: " ~after" }, + ], + }, + ], + }); + + const output = serializer.serialize(doc); + + expect(output).toBe(`<${href}> \\~after`); + expect( + parser.parse(output)?.firstChild?.firstChild?.marks[0].attrs.href + ).toBe(href); + }); }); describe("esc", () => { diff --git a/shared/editor/lib/markdown/serializer.ts b/shared/editor/lib/markdown/serializer.ts index b3f90aecb8..57184f4039 100644 --- a/shared/editor/lib/markdown/serializer.ts +++ b/shared/editor/lib/markdown/serializer.ts @@ -104,6 +104,7 @@ export class MarkdownSerializerState { inTable = false; inList = false; inTightList = false; + inAutolink = false; closed = false; delim = ""; _out = ""; diff --git a/shared/editor/marks/Link.tsx b/shared/editor/marks/Link.tsx index a62d63ad83..5d5d7d4874 100644 --- a/shared/editor/marks/Link.tsx +++ b/shared/editor/marks/Link.tsx @@ -1,7 +1,7 @@ import { t } from "i18next"; import type Token from "markdown-it/lib/token.mjs"; import { InputRule } from "prosemirror-inputrules"; -import type { MarkdownSerializerState } from "prosemirror-markdown"; +import type { MarkdownSerializerState } from "../lib/markdown/serializer"; import type { Attrs, MarkSpec, @@ -361,23 +361,28 @@ export default class Link extends Mark { toMarkdown() { return { open: ( - _state: MarkdownSerializerState, + state: MarkdownSerializerState, mark: ProsemirrorMark, parent: Node, index: number - ) => (isPlainURL(mark, parent, index, 1) ? "<" : "["), + ) => { + state.inAutolink = isPlainURL(mark, parent, index, 1); + return state.inAutolink ? "<" : "["; + }, close: ( state: MarkdownSerializerState, mark: ProsemirrorMark, parent: Node, index: number - ) => - isPlainURL(mark, parent, index, -1) + ) => { + state.inAutolink = false; + return isPlainURL(mark, parent, index, -1) ? ">" : "](" + - state.esc(mark.attrs.href) + - (mark.attrs.title ? " " + quote(mark.attrs.title) : "") + - ")", + state.esc(mark.attrs.href, false) + + (mark.attrs.title ? " " + quote(mark.attrs.title) : "") + + ")"; + }, }; } diff --git a/shared/editor/nodes/Text.ts b/shared/editor/nodes/Text.ts index 05a80fb0f9..624bfb482f 100644 --- a/shared/editor/nodes/Text.ts +++ b/shared/editor/nodes/Text.ts @@ -14,6 +14,13 @@ export default class Text extends Node { } toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) { - state.text(node.text || "", undefined); + const text = node.text || ""; + if (!state.inAutolink) { + state.text(text, true); + return; + } + + // Autolink text is not escaped, except pipes that would split a table cell. + state.text(state.inTable ? text.split("|").join("\\|") : text, false); } }