diff --git a/plugins/github/server/index.ts b/plugins/github/server/index.ts index 1c47fbac26..68f31193a9 100644 --- a/plugins/github/server/index.ts +++ b/plugins/github/server/index.ts @@ -1,6 +1,7 @@ import { Minute } from "@shared/utils/time"; import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; +import { GitHubUtils } from "../shared/GitHubUtils"; import { GitHubIssueProvider } from "./GitHubIssueProvider"; import router from "./api/github"; import env from "./env"; @@ -34,6 +35,10 @@ if (enabled) { type: Hook.UnfurlProvider, value: { unfurl: GitHub.unfurl, cacheExpiry: Minute.seconds }, }, + { + type: Hook.MentionProvider, + value: (url: URL) => GitHubUtils.mentionType(url), + }, { type: Hook.Uninstall, value: uninstall, diff --git a/plugins/github/shared/GitHubUtils.ts b/plugins/github/shared/GitHubUtils.ts index 0b9a70aa7a..dffc2e390e 100644 --- a/plugins/github/shared/GitHubUtils.ts +++ b/plugins/github/shared/GitHubUtils.ts @@ -1,5 +1,6 @@ import queryString from "query-string"; import env from "@shared/env"; +import { MentionType } from "@shared/types"; import { integrationSettingsPath } from "@shared/utils/routeHelpers"; export const GitHubOAuthNonceCookie = "githubOAuthNonce"; @@ -60,6 +61,29 @@ export class GitHubUtils { return `${this.url}?install_request=true`; } + /** + * Determines the type of mention a GitHub URL represents. + * + * @param url the URL to evaluate. + * @returns the mention type, or undefined if this is not a GitHub URL for a + * resource that can be mentioned. + */ + public static mentionType(url: URL): MentionType | undefined { + if (url.hostname !== "github.com") { + return undefined; + } + + const type = url.pathname.split("/")[3]; + + return type === "pull" + ? MentionType.PullRequest + : type === "issues" + ? MentionType.Issue + : type === "projects" + ? MentionType.Project + : undefined; + } + public static getColorForStatus(status: string, isDraftPR: boolean = false) { switch (status) { case "open": diff --git a/plugins/gitlab/server/index.ts b/plugins/gitlab/server/index.ts index c4f9c11d09..b98988445e 100644 --- a/plugins/gitlab/server/index.ts +++ b/plugins/gitlab/server/index.ts @@ -1,6 +1,7 @@ import { Minute } from "@shared/utils/time"; import { PluginManager, Hook } from "@server/utils/PluginManager"; import config from "../plugin.json"; +import { GitLabUtils } from "../shared/GitLabUtils"; import { GitLabIssueProvider } from "./GitLabIssueProvider"; import router from "./api/gitlab"; import { GitLab } from "./gitlab"; @@ -20,6 +21,12 @@ PluginManager.add([ type: Hook.UnfurlProvider, value: { unfurl: GitLab.unfurl, cacheExpiry: Minute.seconds }, }, + { + // Only the public host is recognized here, a self-hosted instance is not + // known without the settings of a connected integration. + type: Hook.MentionProvider, + value: (url: URL) => GitLabUtils.mentionType(url), + }, { type: Hook.Task, value: GitLabWebhookTask, diff --git a/plugins/gitlab/shared/GitLabUtils.ts b/plugins/gitlab/shared/GitLabUtils.ts index 4834960652..a37e83a774 100644 --- a/plugins/gitlab/shared/GitLabUtils.ts +++ b/plugins/gitlab/shared/GitLabUtils.ts @@ -1,6 +1,6 @@ import env from "@shared/env"; import { integrationSettingsPath } from "@shared/utils/routeHelpers"; -import { UnfurlResourceType } from "@shared/types"; +import { MentionType, UnfurlResourceType } from "@shared/types"; export const GitLabOAuthNonceCookie = "gitlabOAuthNonce"; @@ -244,6 +244,32 @@ export class GitLabUtils { } } + /** + * Determines the type of mention a GitLab URL represents. + * + * @param url the URL to evaluate. + * @param customUrl optional custom GitLab URL from integration settings. + * @returns the mention type, or undefined if this is not a GitLab URL for a + * resource that can be mentioned. + */ + public static mentionType( + url: URL, + customUrl?: string + ): MentionType | undefined { + const parsed = this.parseUrl(url.toString(), customUrl); + + switch (parsed?.type) { + case UnfurlResourceType.PR: + return MentionType.PullRequest; + case UnfurlResourceType.Issue: + return MentionType.Issue; + case UnfurlResourceType.Project: + return MentionType.Project; + default: + return undefined; + } + } + /** * Checks if the first path segment is a known GitLab system path. * diff --git a/plugins/linear/server/index.ts b/plugins/linear/server/index.ts index 9b533dcbaa..69c62b065d 100644 --- a/plugins/linear/server/index.ts +++ b/plugins/linear/server/index.ts @@ -1,6 +1,7 @@ import { Minute } from "@shared/utils/time"; import { Hook, PluginManager } from "@server/utils/PluginManager"; import config from "../plugin.json"; +import { LinearUtils } from "../shared/LinearUtils"; import router from "./api/linear"; import env from "./env"; import { Linear } from "./linear"; @@ -24,6 +25,10 @@ if (enabled) { type: Hook.UnfurlProvider, value: { unfurl: Linear.unfurl, cacheExpiry: Minute.seconds }, }, + { + type: Hook.MentionProvider, + value: (url: URL) => LinearUtils.mentionType(url), + }, { type: Hook.Uninstall, value: uninstall, diff --git a/plugins/linear/shared/LinearUtils.ts b/plugins/linear/shared/LinearUtils.ts index 6c2e3f1b13..e362805675 100644 --- a/plugins/linear/shared/LinearUtils.ts +++ b/plugins/linear/shared/LinearUtils.ts @@ -1,5 +1,6 @@ import queryString from "query-string"; import env from "@shared/env"; +import { MentionType } from "@shared/types"; import { integrationSettingsPath } from "@shared/utils/routeHelpers"; export const LinearOAuthNonceCookie = "linearOAuthNonce"; @@ -26,6 +27,27 @@ export class LinearUtils { } } + /** + * Determines the type of mention a Linear URL represents. + * + * @param url the URL to evaluate. + * @returns the mention type, or undefined if this is not a Linear URL for a + * resource that can be mentioned. + */ + public static mentionType(url: URL): MentionType | undefined { + if (url.hostname !== "linear.app") { + return undefined; + } + + const type = url.pathname.split("/")[2]; + + return type === "issue" + ? MentionType.Issue + : type === "project" + ? MentionType.Project + : undefined; + } + static successUrl() { return this.settingsUrl; } diff --git a/server/editor/index.ts b/server/editor/index.ts index c66728ef0b..57a1f9d721 100644 --- a/server/editor/index.ts +++ b/server/editor/index.ts @@ -13,6 +13,7 @@ import { import CodeBlock from "@shared/editor/nodes/CodeBlock"; import CodeFence from "@shared/editor/nodes/CodeFence"; import Mention from "@shared/editor/nodes/Mention"; +import mentionRules from "./rules/mentions"; populateEmojiData(data as EmojiMartData); @@ -43,7 +44,7 @@ for (const extension of extensionManager.extensions) { export const parser = extensionManager.parser({ schema, - plugins: extensionManager.rulePlugins, + plugins: [...extensionManager.rulePlugins, mentionRules], }); export const serializer = extensionManager.serializer(); @@ -80,5 +81,5 @@ for (const extension of commentExtensionManager.extensions) { export const commentParser = commentExtensionManager.parser({ schema: commentSchema, - plugins: commentExtensionManager.rulePlugins, + plugins: [...commentExtensionManager.rulePlugins, mentionRules], }); diff --git a/server/editor/rules/mentions.test.ts b/server/editor/rules/mentions.test.ts new file mode 100644 index 0000000000..2630d74256 --- /dev/null +++ b/server/editor/rules/mentions.test.ts @@ -0,0 +1,72 @@ +import type { Node } from "prosemirror-model"; +import { MentionType } from "@shared/types"; +import { parser } from ".."; + +function firstMention(markdown: string): Node | undefined { + const doc = parser.parse(markdown); + let mention: Node | undefined; + + doc?.descendants((node: Node) => { + if (!mention && node.type.name === "mention") { + mention = node; + } + }); + + return mention; +} + +describe("mention type rule", () => { + it("should resolve a GitHub issue link", () => { + const mention = firstMention( + "@[Fix parser](https://github.com/acme/infra/issues/2)" + ); + + expect(mention?.attrs.type).toBe(MentionType.Issue); + expect(mention?.attrs.href).toBe("https://github.com/acme/infra/issues/2"); + expect(mention?.attrs.label).toBe("Fix parser"); + }); + + it("should resolve a GitHub pull request link", () => { + const mention = firstMention( + "@[Add parser](https://github.com/acme/infra/pull/5)" + ); + + expect(mention?.attrs.type).toBe(MentionType.PullRequest); + }); + + it("should resolve a GitHub project link", () => { + const mention = firstMention( + "@[Roadmap](https://github.com/orgs/acme/projects/1)" + ); + + expect(mention?.attrs.type).toBe(MentionType.Project); + }); + + it("should leave a GitHub link to an unmentionable resource generic", () => { + const mention = firstMention("@[Repo](https://github.com/acme/infra)"); + + expect(mention?.attrs.type).toBe(MentionType.URL); + }); + + it("should leave a link no plugin recognizes generic", () => { + const mention = firstMention("@[Example](https://example.com/page)"); + + expect(mention?.attrs.type).toBe(MentionType.URL); + expect(mention?.attrs.href).toBe("https://example.com/page"); + }); + + it("should not affect mention:// links", () => { + const mention = firstMention( + "@[John Doe](mention://a1b2c3d4-e5f6-7890-abcd-ef1234567890/user/f0e1d2c3-b4a5-6789-0abc-def123456789)" + ); + + expect(mention?.attrs.type).toBe(MentionType.User); + expect(mention?.attrs.href).toBeUndefined(); + }); + + it("should not convert a link without an @ prefix", () => { + expect( + firstMention("[Fix parser](https://github.com/acme/infra/issues/2)") + ).toBeUndefined(); + }); +}); diff --git a/server/editor/rules/mentions.ts b/server/editor/rules/mentions.ts new file mode 100644 index 0000000000..9d4f53ec3f --- /dev/null +++ b/server/editor/rules/mentions.ts @@ -0,0 +1,55 @@ +import type MarkdownIt from "markdown-it"; +import type StateCore from "markdown-it/lib/rules_core/state_core.mjs"; +import { Hook, PluginManager } from "@server/utils/PluginManager"; + +/** + * Narrows the generic mentions produced for external links to the resource + * they point at, by asking each plugin whether it recognizes the URL. + * + * @param state the markdown-it parser state. + */ +function resolveMentionTypes(state: StateCore) { + const providers = PluginManager.getHooks(Hook.MentionProvider); + if (!providers.length) { + return; + } + + for (const token of state.tokens) { + if (token.type !== "inline" || !token.children) { + continue; + } + + for (const child of token.children) { + const href = child.type === "mention" ? child.attrGet("href") : null; + if (!href) { + continue; + } + + let url: URL; + try { + url = new URL(href); + } catch { + continue; + } + + for (const provider of providers) { + const type = provider.value(url); + if (type) { + child.attrSet("type", type); + break; + } + } + } + } +} + +/** + * Markdown-it plugin that resolves the type of mentions created from external + * links. It is only applied to parsers on the server, where the plugins that + * recognize a URL are registered. + * + * @param md the markdown-it instance. + */ +export default function mentions(md: MarkdownIt) { + md.core.ruler.after("mention", "mention_type", resolveMentionTypes); +} diff --git a/server/models/helpers/ProseMirrorHelper.test.ts b/server/models/helpers/ProseMirrorHelper.test.ts index b89ec2821c..005ac55049 100644 --- a/server/models/helpers/ProseMirrorHelper.test.ts +++ b/server/models/helpers/ProseMirrorHelper.test.ts @@ -7,7 +7,7 @@ import type { ProsemirrorData } from "@shared/types"; import { MentionType } from "@shared/types"; import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { createContext } from "@server/context"; -import { parser, schema } from "@server/editor"; +import { parser, schema, serializer } from "@server/editor"; import env from "@server/env"; import { Attachment } from "@server/models"; import { buildProseMirrorDoc, buildUser } from "@server/test/factories"; @@ -1055,6 +1055,69 @@ describe("ProsemirrorHelper", () => { expect(thirdItem.attrs.checked).toBe(true); expect(thirdItem.textContent).toBe("Third"); }); + + it("should convert an @ prefixed GitHub link to an issue mention", () => { + const markdown = + "Please review @[Fix parser](https://github.com/acme/infra/issues/2)"; + + const doc = ProsemirrorHelper.toProsemirror(markdown); + const paragraph = doc.content.child(0); + const mention = paragraph.content.child(1); + + expect(mention.type.name).toBe("mention"); + expect(mention.attrs.type).toBe(MentionType.Issue); + expect(mention.attrs.href).toBe("https://github.com/acme/infra/issues/2"); + expect(mention.attrs.label).toBe("Fix parser"); + expect(paragraph.content.child(0).text).toBe("Please review "); + }); + + it("should convert an @ prefixed GitHub pull request link", () => { + const markdown = "@[Add parser](https://github.com/acme/infra/pull/5)"; + + const doc = ProsemirrorHelper.toProsemirror(markdown); + const mention = doc.content.child(0).content.child(0); + + expect(mention.attrs.type).toBe(MentionType.PullRequest); + }); + + it("should convert an @ prefixed link to an unclaimed host to a url mention", () => { + const markdown = "@[Example](https://example.com/page)"; + + const doc = ProsemirrorHelper.toProsemirror(markdown); + const mention = doc.content.child(0).content.child(0); + + expect(mention.attrs.type).toBe(MentionType.URL); + expect(mention.attrs.href).toBe("https://example.com/page"); + }); + + it("should keep a link without an @ prefix as a link", () => { + const markdown = + "Please review [Fix parser](https://github.com/acme/infra/issues/2)"; + + const doc = ProsemirrorHelper.toProsemirror(markdown); + const paragraph = doc.content.child(0); + + expect( + paragraph.content + .child(1) + .marks.some((mark) => mark.type.name === "link") + ).toBe(true); + }); + + it("should round-trip an external mention through Markdown", () => { + const markdown = "@[Fix parser](https://github.com/acme/infra/issues/2)"; + + const doc = ProsemirrorHelper.toProsemirror(markdown); + const mention = doc.content.child(0).content.child(0); + + expect(mention.type.name).toBe("mention"); + expect(mention.attrs.type).toBe(MentionType.Issue); + // Markdown that leaves Outline carries the url, so it comes back in as a + // mention rather than a plain link. + expect(serializer.serialize(doc, { commonMark: true }).trim()).toBe( + "@[Fix parser](https://github.com/acme/infra/issues/2)" + ); + }); }); describe("replaceDocumentReferences", () => { diff --git a/server/types.ts b/server/types.ts index 49037c1435..adad7772a5 100644 --- a/server/types.ts +++ b/server/types.ts @@ -9,6 +9,7 @@ import type { Client, CollectionPermission, JSONValue, + MentionType, UnfurlResourceType, ProsemirrorData, UnfurlResponse, @@ -623,6 +624,13 @@ export type UnfurlSignature = ( actor?: User ) => Promise; +/** + * Recognizes the URL of a resource belonging to the service and returns the + * type of mention that represents it, or undefined when the URL is not one the + * service can mention. + */ +export type MentionSignature = (url: URL) => MentionType | undefined; + export type UninstallSignature = (integration: Integration) => Promise; export type Replace = { diff --git a/server/utils/PluginManager.ts b/server/utils/PluginManager.ts index 9caa54e6e3..0ba1057554 100644 --- a/server/utils/PluginManager.ts +++ b/server/utils/PluginManager.ts @@ -7,7 +7,11 @@ import env from "@server/env"; import Logger from "@server/logging/Logger"; import type BaseProcessor from "@server/queues/processors/BaseProcessor"; import type { BaseTask } from "@server/queues/tasks/base/BaseTask"; -import type { UnfurlSignature, UninstallSignature } from "@server/types"; +import type { + MentionSignature, + UnfurlSignature, + UninstallSignature, +} from "@server/types"; import type { BaseIssueProvider } from "./BaseIssueProvider"; import type { GroupSyncProvider } from "./GroupSyncProvider"; import type { BaseSearchProvider } from "./BaseSearchProvider"; @@ -34,6 +38,7 @@ export enum Hook { IssueProvider = "issueProvider", Processor = "processor", SearchProvider = "searchProvider", + MentionProvider = "mentionProvider", Task = "task", UnfurlProvider = "unfurl", Uninstall = "uninstall", @@ -50,6 +55,7 @@ type PluginValueMap = { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- typeof BaseEmail isn't assignable from BaseEmail; plugins register heterogeneous template Props. [Hook.EmailTemplate]: typeof BaseEmail; [Hook.IssueProvider]: BaseIssueProvider; + [Hook.MentionProvider]: MentionSignature; [Hook.Processor]: typeof BaseProcessor; [Hook.SearchProvider]: BaseSearchProvider; [Hook.Task]: typeof BaseTask; diff --git a/shared/editor/nodes/Mention.test.ts b/shared/editor/nodes/Mention.test.ts index 2177ef2cf9..bf3eb087c1 100644 --- a/shared/editor/nodes/Mention.test.ts +++ b/shared/editor/nodes/Mention.test.ts @@ -22,7 +22,7 @@ const serializeMention = ( describe("Mention serialization", () => { describe("markdown leaving Outline", () => { - it("serializes an issue mention as a link to the external url", () => { + it("serializes an issue mention as an @ prefixed link to the external url", () => { expect( serializeMention( { @@ -33,11 +33,11 @@ describe("Mention serialization", () => { { commonMark: true } ) ).toBe( - "[Epic 1: Control plane Helm chart](https://github.com/acme/infra/issues/2)" + "@[Epic 1: Control plane Helm chart](https://github.com/acme/infra/issues/2)" ); }); - it("serializes a pull request mention as a link to the external url", () => { + it("serializes a pull request mention as an @ prefixed link to the external url", () => { expect( serializeMention( { @@ -47,10 +47,10 @@ describe("Mention serialization", () => { }, { commonMark: true } ) - ).toBe("[Add Helm chart](https://github.com/acme/infra/pull/42)"); + ).toBe("@[Add Helm chart](https://github.com/acme/infra/pull/42)"); }); - it("serializes a project mention as a link to the external url", () => { + it("serializes a project mention as an @ prefixed link to the external url", () => { expect( serializeMention( { @@ -60,7 +60,20 @@ describe("Mention serialization", () => { }, { commonMark: true } ) - ).toBe("[Q3 Roadmap](https://github.com/orgs/acme/projects/7)"); + ).toBe("@[Q3 Roadmap](https://github.com/orgs/acme/projects/7)"); + }); + + it("serializes a url mention as an @ prefixed link", () => { + expect( + serializeMention( + { + type: MentionType.URL, + label: "Example", + href: "https://example.com/page", + }, + { commonMark: true } + ) + ).toBe("@[Example](https://example.com/page)"); }); it("sanitizes an unsafe url", () => { diff --git a/shared/editor/nodes/Mention.tsx b/shared/editor/nodes/Mention.tsx index 46a91fb7d4..9813247ce8 100644 --- a/shared/editor/nodes/Mention.tsx +++ b/shared/editor/nodes/Mention.tsx @@ -65,6 +65,15 @@ function isExternalMention(type: MentionType): boolean { ); } +/** The mention type that represents each kind of unfurled resource. */ +const MentionTypeForResource: Partial> = + { + [UnfurlResourceType.Issue]: MentionType.Issue, + [UnfurlResourceType.PR]: MentionType.PullRequest, + [UnfurlResourceType.Project]: MentionType.Project, + [UnfurlResourceType.URL]: MentionType.URL, + }; + export default class Mention extends Node { get name() { return "mention"; @@ -394,12 +403,13 @@ export default class Mention extends Node { state.write(`[${label}](/collection/${mId})`); } else if ( state.options.commonMark && - isExternalMention(mType) && + (isExternalMention(mType) || mType === MentionType.URL) && node.attrs.href ) { // Markdown that leaves Outline cannot resolve a mention:// reference, so - // external mentions fall back to the URL they already carry. - state.write(`[${label}](${sanitizeUrl(node.attrs.href)})`); + // external mentions fall back to the URL they already carry. The "@" + // prefix allows them to be parsed back into a mention on the way in. + state.write(`@[${label}](${sanitizeUrl(node.attrs.href)})`); } else { // Keep the mention:// format for everything else, it round-trips back // into a live mention through Outline's own parser. @@ -414,6 +424,7 @@ export default class Mention extends Node { id: tok.attrGet("id"), type: tok.attrGet("type"), modelId: tok.attrGet("modelId"), + href: tok.attrGet("href") ?? undefined, label: tok.content, }), }; @@ -456,6 +467,18 @@ export default class Mention extends Node { const overrides: Record = label ? { label } : {}; overrides.unfurl = unfurl; + // The resource an external link points at is only known once it has been + // unfurled, so narrow a generic URL mention to the type it turned out to + // be – an issue, pull request or project. + const unfurledType = MentionTypeForResource[unfurl.type]; + if ( + unfurledType && + node.attrs.type === MentionType.URL && + unfurledType !== node.attrs.type + ) { + overrides.type = unfurledType; + } + const pos = getPos(); if (!isMatch(node.attrs, overrides)) { diff --git a/shared/editor/rules/mention.test.ts b/shared/editor/rules/mention.test.ts index c403b71eb6..65193101ae 100644 --- a/shared/editor/rules/mention.test.ts +++ b/shared/editor/rules/mention.test.ts @@ -1,4 +1,5 @@ import markdownit from "markdown-it"; +import { MentionType } from "../../types"; import mentionRule from "./mention"; function findMentionTokens(tokens: ReturnType) { @@ -6,6 +7,7 @@ function findMentionTokens(tokens: ReturnType) { id: string | null; type: string | null; modelId: string | null; + href: string | null; label: string; }> = []; @@ -17,6 +19,7 @@ function findMentionTokens(tokens: ReturnType) { id: child.attrGet("id"), type: child.attrGet("type"), modelId: child.attrGet("modelId"), + href: child.attrGet("href"), label: child.content, }); } @@ -43,6 +46,7 @@ describe("mention rule", () => { id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890", type: "user", modelId: "f0e1d2c3-b4a5-6789-0abc-def123456789", + href: null, label: "John Doe", }); }); @@ -165,6 +169,56 @@ describe("mention rule", () => { }); }); + describe("external links", () => { + it("should parse an external link as a generic mention carrying the href", () => { + const result = md.parse("@[Example](https://example.com/page)", {}); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0].type).toBe(MentionType.URL); + expect(mentions[0].href).toBe("https://example.com/page"); + expect(mentions[0].label).toBe("Example"); + expect(mentions[0].id).toBeTruthy(); + expect(mentions[0].modelId).toBeTruthy(); + }); + + it("should parse an external link within text", () => { + const result = md.parse( + "Please review @[Example](https://example.com/page) today", + {} + ); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0].href).toBe("https://example.com/page"); + }); + + it("should not parse a link with an unsupported protocol", () => { + const result = md.parse("@[Mail](mailto:jane@example.com)", {}); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(0); + }); + }); + + describe("rendering", () => { + it("should escape markup in the label", () => { + const html = md.render( + "@[](mention://user/abc123)" + ); + + expect(html).not.toContain(" { + const html = md.render('@[Example](https://example.com/"onmouseover=1)'); + + expect(html).not.toContain('"onmouseover'); + expect(html).toContain('href="https://example.com/%22onmouseover=1"'); + }); + }); + describe("non-mentions", () => { it("should not parse regular links as mentions", () => { const result = md.parse("[John Doe](https://example.com)", {}); diff --git a/shared/editor/rules/mention.ts b/shared/editor/rules/mention.ts index 3d04e5cde5..11919df2d2 100644 --- a/shared/editor/rules/mention.ts +++ b/shared/editor/rules/mention.ts @@ -2,7 +2,9 @@ import type MarkdownIt from "markdown-it"; import type StateCore from "markdown-it/lib/rules_core/state_core.mjs"; import type Token from "markdown-it/lib/token.mjs"; import { v4 as uuidv4 } from "uuid"; +import { MentionType } from "../../types"; import parseMentionUrl from "@shared/utils/parseMentionUrl"; +import { sanitizeUrl } from "@shared/utils/urls"; /** * Check whether a URL is a valid mention:// href. @@ -15,6 +17,24 @@ function isMentionHref(href: string) { return mentionType !== undefined && modelId !== undefined; } +/** + * Parse an href that points at an external resource that can be represented as + * a mention. + * + * @param href the URL string to parse. + * @returns the parsed URL, or undefined when the href cannot be mentioned. + */ +function parseExternalHref(href: string): URL | undefined { + try { + const url = new URL(href); + return url.protocol === "http:" || url.protocol === "https:" + ? url + : undefined; + } catch { + return undefined; + } +} + /** * Parse a mention:// href into the id, type and modelId needed by the editor. * For 2-segment URLs (no instance id) a fresh UUID is generated. @@ -37,14 +57,22 @@ function parseMentionHref(href: string): { return { id: id ?? uuidv4(), type: mentionType, modelId }; } -function renderMention(tokens: Token[], idx: number) { - const id = tokens[idx].attrGet("id"); - const mType = tokens[idx].attrGet("type"); - const mId = tokens[idx].attrGet("modelId"); - const label = tokens[idx].content; +const renderMention = (md: MarkdownIt) => (tokens: Token[], idx: number) => { + // The label and attributes originate in the document, and markdown-it leaves + // any markup in them intact when `html` is disabled, so everything written + // into the tag is escaped here. + const esc = (value: string | null) => md.utils.escapeHtml(value ?? ""); - return `${label}`; -} + const id = esc(tokens[idx].attrGet("id")); + const mType = esc(tokens[idx].attrGet("type")); + const mId = esc(tokens[idx].attrGet("modelId")); + const href = tokens[idx].attrGet("href"); + const label = esc(tokens[idx].content); + + return href + ? `${label}` + : `${label}`; +}; function parseMentions(state: StateCore) { for (let i = 0; i < state.tokens.length; i++) { @@ -75,9 +103,16 @@ function parseMentions(state: StateCore) { return false; } - // "link_open" token should have valid href + // "link_open" token should have a mention:// href, or an external href + // that can be represented as a mention. const attr = openToken.attrs?.[0]; - if (!(attr && attr[0] === "href" && isMentionHref(attr[1]))) { + if ( + !( + attr && + attr[0] === "href" && + (isMentionHref(attr[1]) || parseExternalHref(attr[1])) + ) + ) { return false; } @@ -91,15 +126,30 @@ function parseMentions(state: StateCore) { // remove "@" from preceding token precToken.content = precToken.content.slice(0, -1); - // href must be present, otherwise the isMentionHref test would've failed + // href must be present, otherwise the href test would've failed // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion const href = openToken.attrs![0][1]; - const { id, type: mType, modelId: mId } = parseMentionHref(href); - const mentionToken = new state.Token("mention", "", 0); - mentionToken.attrSet("id", id); - mentionToken.attrSet("type", mType); - mentionToken.attrSet("modelId", mId); + const externalUrl = isMentionHref(href) + ? undefined + : parseExternalHref(href); + + if (externalUrl) { + // External links carry their identity in the href, so the ids are + // generated the same way a paste in the editor would. The mention is + // generic here and is narrowed to the resource it points at by the + // service that recognizes the URL. + mentionToken.attrSet("id", uuidv4()); + mentionToken.attrSet("type", MentionType.URL); + mentionToken.attrSet("modelId", uuidv4()); + mentionToken.attrSet("href", href); + } else { + const { id, type: mType, modelId: mId } = parseMentionHref(href); + mentionToken.attrSet("id", id); + mentionToken.attrSet("type", mType); + mentionToken.attrSet("modelId", mId); + } + mentionToken.content = textToken.content; // "link_open", followed by "text" and "link_close" tokens are coalesced @@ -129,6 +179,6 @@ function parseMentions(state: StateCore) { } export default function mention(md: MarkdownIt) { - md.renderer.rules.mention = renderMention; + md.renderer.rules.mention = renderMention(md); md.core.ruler.after("inline", "mention", parseMentions); }