Files
outline/server/editor/rules/mentions.ts
T
Tom MoorandClaude Opus 5 f49b047648 feat: Convert @ prefixed Markdown links to mentions on server writes (#13403)
* checkpoint

* feat: Convert @ prefixed links to mentions on server Markdown writes

A Markdown link preceded by "@" now becomes a mention when brought in
through the documents API, MCP, or an import, so an exported mention
survives a round trip instead of degrading to a plain link.

The editor rule creates a generic mention carrying the href, and the
type of resource it points at is resolved by the plugin that recognizes
the URL through a new MentionProvider hook. Mentions of external
resources also serialize to their real URL rather than a mention:// URI,
so the reference resolves outside of Outline.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Escape values interpolated into rendered mention HTML

markdown-it leaves markup in a link label intact when html is disabled,
so the label and attributes are escaped before being written into the
tag.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 17:42:28 -04:00

56 lines
1.5 KiB
TypeScript

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);
}