mirror of
https://github.com/outline/outline.git
synced 2026-11-27 16:08:58 +03:00
fix: Do not escape plain URL link text in Markdown output (#14707)
* fix: Do not escape plain URL link text in Markdown output * fix: Escape pipes in plain URL links inside table cells * fix: Escape table pipes in autolinks without a replace call
This commit is contained in:
1 parent
c265112bd0
commit
ef1790e143
4 files changed
+85
-9
No files matched your search
@@ -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("| <https://example.edu/?q=a\\|b> |");
|
||||
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", () => {
|
||||
|
||||
@@ -104,6 +104,7 @@ export class MarkdownSerializerState {
|
||||
inTable = false;
|
||||
inList = false;
|
||||
inTightList = false;
|
||||
inAutolink = false;
|
||||
closed = false;
|
||||
delim = "";
|
||||
_out = "";
|
||||
|
||||
@@ -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<LinkOptions> {
|
||||
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) : "") +
|
||||
")";
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user