Files
outline/shared/editor/nodes/Mention.test.ts
T
Tom Moor a59a01cffe fix: Serialize external mentions as links in exported markdown (#13459)
* fix: Serialize external mentions as links in exported markdown

GitHub issue, pull request and project mentions carry the real URL in
`href`, but always serialized to a mention:// reference that cannot be
resolved outside of Outline. Write a plain link instead when the
commonMark serializer option is set, and set that option on the paths
where markdown leaves the platform – document and revision export, zip
exports, and the public share markdown response.

* Sanitize external mention url before navigation
2026-08-16 14:14:20 -04:00

152 lines
4.4 KiB
TypeScript

import { MentionType } from "../../types";
import { schema, serializer } from "../../test/editor";
const id = "0c440212-8b40-49fa-8a64-2548d6b60d59";
const modelId = "c85a0d80-3a89-4b25-a0cd-e7fc83f0d226";
const serializeMention = (
attrs: Record<string, unknown>,
options?: { commonMark?: boolean }
) => {
const doc = schema.nodeFromJSON({
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "mention", attrs: { id, modelId, ...attrs } }],
},
],
});
return serializer.serialize(doc, options).trim();
};
describe("Mention serialization", () => {
describe("markdown leaving Outline", () => {
it("serializes an issue mention as a link to the external url", () => {
expect(
serializeMention(
{
type: MentionType.Issue,
label: "Epic 1: Control plane Helm chart",
href: "https://github.com/acme/infra/issues/2",
},
{ commonMark: true }
)
).toBe(
"[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", () => {
expect(
serializeMention(
{
type: MentionType.PullRequest,
label: "Add Helm chart",
href: "https://github.com/acme/infra/pull/42",
},
{ commonMark: true }
)
).toBe("[Add Helm chart](https://github.com/acme/infra/pull/42)");
});
it("serializes a project mention as a link to the external url", () => {
expect(
serializeMention(
{
type: MentionType.Project,
label: "Q3 Roadmap",
href: "https://github.com/orgs/acme/projects/7",
},
{ commonMark: true }
)
).toBe("[Q3 Roadmap](https://github.com/orgs/acme/projects/7)");
});
it("sanitizes an unsafe url", () => {
const markdown = serializeMention(
{
type: MentionType.Issue,
label: "Epic 1",
// oxlint-disable-next-line no-script-url
href: "javascript:alert(1)",
},
{ commonMark: true }
);
expect(markdown).not.toContain("(javascript:");
});
it("keeps the mention:// format when there is no url to link to", () => {
expect(
serializeMention(
{ type: MentionType.Issue, label: "Epic 1" },
{ commonMark: true }
)
).toBe(`@[Epic 1](mention://${id}/issue/${modelId})`);
});
it("keeps the mention:// format for internal mentions", () => {
expect(
serializeMention(
{ type: MentionType.User, label: "John Doe" },
{ commonMark: true }
)
).toBe(`@[John Doe](mention://${id}/user/${modelId})`);
});
});
describe("markdown staying within Outline", () => {
it("keeps the mention:// format for an issue mention", () => {
expect(
serializeMention({
type: MentionType.Issue,
label: "Epic 1: Control plane Helm chart",
href: "https://github.com/acme/infra/issues/2",
})
).toBe(
`@[Epic 1: Control plane Helm chart](mention://${id}/issue/${modelId})`
);
});
it("keeps the mention:// format for a pull request mention", () => {
expect(
serializeMention({
type: MentionType.PullRequest,
label: "Add Helm chart",
href: "https://github.com/acme/infra/pull/42",
})
).toBe(`@[Add Helm chart](mention://${id}/pull_request/${modelId})`);
});
it("keeps the mention:// format for a project mention", () => {
expect(
serializeMention({
type: MentionType.Project,
label: "Q3 Roadmap",
href: "https://github.com/orgs/acme/projects/7",
})
).toBe(`@[Q3 Roadmap](mention://${id}/project/${modelId})`);
});
});
describe("document and collection mentions", () => {
it("serializes a document mention as an internal link", () => {
expect(
serializeMention(
{ type: MentionType.Document, label: "Onboarding" },
{ commonMark: true }
)
).toBe(`[Onboarding](/doc/${modelId})`);
});
it("serializes a collection mention as an internal link", () => {
expect(
serializeMention(
{ type: MentionType.Collection, label: "Engineering" },
{ commonMark: true }
)
).toBe(`[Engineering](/collection/${modelId})`);
});
});
});