TypeScript
fix(web): bare URL links keep trailing underscores and tildes (#27794)
Dmytro Mishchenko58f8ab47a0f5
diff --git a/packages/shared/src/markdownPipeline.test.ts b/packages/shared/src/markdownPipeline.test.tsindex 44e6cebe558..5d699ffe8ab 100644--- a/packages/shared/src/markdownPipeline.test.ts+++ b/packages/shared/src/markdownPipeline.test.ts@@ -3,7 +3,120 @@ import { unified } from "unified";import rehypeRaw from "rehype-raw";import remarkParse from "remark-parse";import remarkRehype from "remark-rehype";-import { CHAT_MARKDOWN_REHYPE_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS } from "./markdownPipeline.ts";+import type { Root, RootContent } from "mdast";+import {+ CHAT_MARKDOWN_REHYPE_PLUGINS,+ CHAT_MARKDOWN_REMARK_PLUGINS,+ CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,+} from "./markdownPipeline.ts";++it.each([+ ["default", CHAT_MARKDOWN_REMARK_PLUGINS],+ ["with breaks", CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS],+] as const)("preserves trailing underscores and tildes in bare URLs (%s)", (_, plugins) => {+ const processor = unified().use(remarkParse).use(plugins);+ const shape = (node: Root | RootContent): string => {+ if (node.type === "text") return node.value;+ if (node.type === "inlineCode") return `\`${node.value}\``;+ if (node.type === "code") return `\`\`\`${node.value}\`\`\``;+ const children = "children" in node ? node.children.map(shape).join("") : "";+ if (node.type === "link") return `[${children}](${node.url})`;+ if (node.type === "linkReference") return `[${children}][${node.identifier}]`;+ if (node.type === "emphasis") return `*${children}*`;+ if (node.type === "strong") return `**${children}**`;+ if (node.type === "delete") return `~~${children}~~`;+ return children;+ };+ const link = (url: string) => `[${url}](${/^www\./i.test(url) ? `http://${url}` : url})`;+ for (const url of [+ "https://x.com/dimasike_",+ "http://example.com/name__",+ "HTTPS://example.com/name_",+ "https://example.com/a_(b)_",+ "https://example.com/?name=value_",+ "https://example.com/#section_",+ "https://example.com/a_?name=value_#section_",+ "https://example.com/name~",+ "https://example.com/name~~",+ "https://example.com/name_~",+ "https://example.com/name~_",+ "https://example.com/name__~~_",+ "https://example.com/?name=value~",+ "https://example.com/#section~",+ "www.example.com/name_",+ "www.example.com/name~_",+ "WWW.example.com/name~~",+ "https://example.com/name-",+ "https://example.com/name/",+ "https://example.com/name%5F",+ "https://example.com/name%7E",+ ]) {+ for (const punctuation of ["", ".", ",", "!", "?", ":", ";", ")", "]", '"']) {+ const markdown = `See ${url}${punctuation} next`;+ expect(shape(processor.runSync(processor.parse(markdown), markdown) as Root), markdown).toBe(+ `See ${link(url)}${punctuation} next`,+ );+ }+ }+ for (const [markdown, expected] of [+ ["*https://x.com/dimasike_*", `*${link("https://x.com/dimasike_")}*`],+ ["**https://x.com/dimasike_**", `**${link("https://x.com/dimasike_")}**`],+ ["_https://x.com/dimasike_", `*${link("https://x.com/dimasike")}*`],+ ["__https://x.com/dimasike__", `**${link("https://x.com/dimasike")}**`],+ ["~~https://example.com/name_~~", `~~${link("https://example.com/name_")}~~`],+ ["~~https://example.com/name~~", `~~${link("https://example.com/name")}~~`],+ ["~https://example.com/name~", `~~${link("https://example.com/name")}~~`],+ ["*https://example.com/name~*", `*${link("https://example.com/name~")}*`],+ ["_https://example.com/name~_", `*${link("https://example.com/name~")}*`],+ ["[test](https://example.com/name~)", "[test](https://example.com/name~)"],+ ["[https://x.com/a](https://x.com/a)~_", `${link("https://x.com/a")}~_`],+ ["<https://x.com/a>~_", `${link("https://x.com/a")}~_`],+ ["[test][ref]~_\n\n[ref]: https://x.com/a~_", "[test][ref]~_"],+ ["`https://example.com/name~_`", "`https://example.com/name~_`"],+ ["```\nhttps://example.com/name~_\n```", "```https://example.com/name~_```"],+ ["https\\://example.com/name~_", link("https://example.com/name~_")],+ ["https://example.com/name. _~", `${link("https://example.com/name")}. _~`],+ ["Mail [email protected]_", "Mail [email protected]_"],+ ["[test](https://x.com/dimasike_)", "[test](https://x.com/dimasike_)"],+ ["[https://x.com/a](https://x.com/a)_", `${link("https://x.com/a")}_`],+ ["<https://x.com/a>_", `${link("https://x.com/a")}_`],+ ["[test][ref]_\n\n[ref]: https://x.com/a_", "[test][ref]_"],+ ["`https://x.com/dimasike_`", "`https://x.com/dimasike_`"],+ ["```\nhttps://x.com/dimasike_\n```", "```https://x.com/dimasike_```"],+ ["https://x.com/a. _", `${link("https://x.com/a")}. _`],+ ["https\\://x.com/dimasike_", link("https://x.com/dimasike_")],+ ["https://x.com/a\\_", link("https://x.com/a\\_")],+ ["https://x.com/a_", `${link("https://x.com/a_")};`],+ [+ "https://x.com/a_ https://x.com/b_",+ `${link("https://x.com/a_")} ${link("https://x.com/b_")}`,+ ],+ ] as const) {+ expect(shape(processor.runSync(processor.parse(markdown), markdown) as Root), markdown).toBe(+ expected,+ );+ }+ for (const url of ["https://x.com/dimasike_", "www.example.com/name~_"]) {+ const markdown = `${url}, next`;+ const boundary = { line: 1, column: url.length + 1, offset: url.length };+ expect(processor.runSync(processor.parse(markdown), markdown)).toMatchObject({+ children: [+ {+ children: [+ {+ type: "link",+ url: /^www\./i.test(url) ? `http://${url}` : url,+ position: { end: boundary },+ children: [{ type: "text", value: url, position: { end: boundary } }],+ },+ { type: "text", value: ", next", position: { start: boundary } },+ ],+ },+ ],+ });+ }+});it("preserves in-app thread links through the shared Markdown sanitizer", () => {const processor = unified()diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.tsindex 85b9280ab8c..0d093872c07 100644--- a/packages/shared/src/markdownPipeline.ts+++ b/packages/shared/src/markdownPipeline.ts@@ -1,6 +1,6 @@import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts";-import { type PluggableList, type Processor, unified } from "unified";+import { type PluggableList, type Processor, type Transformer, unified } from "unified";import remarkParse from "remark-parse";import remarkRehype from "remark-rehype";import { find, html } from "property-information";@@ -204,6 +204,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [remarkGfm,+ remarkKeepAutolinkSuffixes,remarkKeepWindowsPaths,remarkGithubAlerts,remarkNormalizeListItemIndentation,@@ -215,6 +216,7 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [remarkGfm,+ remarkKeepAutolinkSuffixes,remarkKeepWindowsPaths,remarkGithubAlerts,remarkNormalizeListItemIndentation,@@ -368,6 +370,52 @@ type MarkdownAstNode = {children?: MarkdownAstNode[];};+function remarkKeepAutolinkSuffixes(): Transformer<Root> {+ return (tree, file) => {+ const source = String(file);+ const visit = (node: Root | RootContent) => {+ if (!("children" in node) || node.type === "link" || node.type === "linkReference") return;+ for (let index = 0; index < node.children.length; index++) {+ const child = node.children[index];+ const next = node.children[index + 1];+ if (child?.type === "link" && /^https?:\/\//i.test(child.url) && next?.type === "text") {+ const suffix = /^[_~]+/.exec(next.value)?.[0];+ if (!suffix) continue;+ const label = child.children.length === 1 ? child.children[0] : undefined;+ if (label?.type !== "text") continue;+ if (label.value !== child.url && `http://${label.value}` !== child.url) continue;+ const start = child.position?.start.offset;+ const end = child.position?.end.offset;+ const bare =+ !child.position ||+ (start !== undefined &&+ end !== undefined &&+ source.slice(start, end) === label.value &&+ source.slice(end, end + suffix.length) === suffix);+ if (bare) {+ child.url += suffix;+ label.value += suffix;+ next.value = next.value.slice(suffix.length);+ if (child.position && end !== undefined) {+ const boundary = {+ ...child.position.end,+ column: child.position.end.column + suffix.length,+ offset: end + suffix.length,+ };+ child.position.end = boundary;+ if (label.position) label.position.end = boundary;+ if (next.position) next.position.start = boundary;+ }+ if (!next.value) node.children.splice(index + 1, 1);+ }+ }+ if (child) visit(child);+ }+ };+ visit(tree);+ };+}+function remarkPreserveCodeMeta() {return (tree: MarkdownAstNode) => {const visit = (node: MarkdownAstNode) => {