Page link
The page link plugin opens a search of your app's pages when you type [[, and inserts the page you pick as an inline link, as in Notion.
createPageLinkPlugin brings Notion’s [[ page links. Typing [[ opens a menu of the pages your app’s search(query) answers; the text typed after it filters them. Picking one replaces [[query with a pageLink atom, a link to the page, and puts the caret after it. The menu is your menu snippet, as the mention’s is.
<script lang="ts">
import { Edytor, createPageLinkPlugin, type PageLinkItem } from 'edytor';
const search = async (query: string) => {
const response = await fetch(`/api/pages?q=${encodeURIComponent(query)}`);
return (await response.json()) as PageLinkItem[];
};
const pageLinks = createPageLinkPlugin({ search, href: (page) => `/pages/${page.id}` });
</script>
<Edytor plugins={[pageLinks]} />
Pasted references
fromPaste turns a pasted text your app recognizes into a page link: your own URL for a page, or any text you can read as one. It answers the page, or null to leave the paste alone.
<script lang="ts">
import { Edytor, createPageLinkPlugin, type PageLinkItem } from 'edytor';
const pages: PageLinkItem[] = [{ id: '123', title: 'Roadmap', icon: '🗺️' }];
const search = (query: string) => pages.filter((page) => page.title.includes(query));
// https://app.example.com/p/123 is page 123.
const fromPaste = (text: string) => {
const id = /^https:\/\/app\.example\.com\/p\/(\w+)$/.exec(text)?.[1];
return pages.find((page) => page.id === id) ?? null;
};
const pageLinks = createPageLinkPlugin({ search, fromPaste });
</script>
<Edytor plugins={[pageLinks]} />
It is asked for a paste at a caret whose plain text is one item: trimmed, on one line. A paste of several lines, or over a selection, is left alone. A page answered at once replaces the pasted text with the page link, an undo step of its own after the paste, so one undo gives the plain text back. The answer wins over the rich text link and the URL menu of an empty line, whatever the order you list the plugins in: a URL fromPaste recognizes becomes the page link, any other is linked (or offered as an embed) as before. A promise lets the paste land as it would without the option, linked or not, and swaps it for the page link when it answers, if the text is still there; undo gives back what the paste wrote. A readonly view pastes nothing.
It is not a default plugin: list it in plugins, in every view of a document that holds page links.
Try it
Type [[ at the end of the last line, then a few letters, and pick a page with Enter or a click. Or copy page:design-system or https://app.example.com/p/p4 and paste it there: the demo’s fromPaste reads either as a page, and the paste becomes its link. The editor is local to this page: a reload resets it.
Customize
This one draws the menu as a grid of tiles, each with the page’s icon large above its title, through the menu snippet.
<script lang="ts">
import {
Edytor,
createPageLinkPlugin,
type JSONDoc,
type PageLinkItem,
type Plugin,
type TriggerMenuController
} from 'edytor';
// The page's document and the rest of your editor's plugins.
let { value, plugins = [] }: { value?: JSONDoc; plugins?: Plugin[] } = $props();
const pages: PageLinkItem[] = [
{ id: 'p1', title: 'Roadmap', icon: '🗺️' },
{ id: 'p2', title: 'Meeting notes', icon: '📝' },
{ id: 'p3', title: 'Design system', icon: '🎨' },
{ id: 'p4', title: 'Release checklist', icon: '✅' },
{ id: 'p5', title: 'Onboarding guide', icon: '👋' }
];
// Pages as tiles in a grid under the caret.
const pageLinks = createPageLinkPlugin({
search: (query) =>
pages.filter((page) => page.title.toLowerCase().includes(query.trim().toLowerCase())),
menu
});
</script>
<!-- A tile grid: the page's icon large, its title underneath. -->
{#snippet menu(menu: TriggerMenuController<PageLinkItem>)}
<div class="tiles" onmousedown={(event) => event.preventDefault()} role="presentation">
<div class="title">Link to a page</div>
<div class="grid" aria-label="Pages" {@attach menu.popup}>
{#each menu.items as page, index (page.id)}
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
<div
class="tile"
data-selected={menu.index === index}
onmousemove={() => (menu.index = index)}
onclick={() => void menu.pick(page)}
{@attach menu.option(index)}
>
<span class="icon" aria-hidden="true">{page.icon}</span>
<span class="name">{page.title}</span>
</div>
{:else}
<div class="empty" role="presentation">
{menu.loading ? 'Searching…' : 'No page by that name'}
</div>
{/each}
</div>
</div>
{/snippet}
<Edytor {value} plugins={[pageLinks, ...plugins]} aria-label="Page link demo" />
<style>
.tiles {
width: 300px;
padding: 10px;
border: 1px solid #dfe3ea;
border-radius: 14px;
background: #f8faff;
box-shadow: 0 12px 30px rgb(30 50 100 / 0.18);
font: 13px/1.3 system-ui, sans-serif;
color: #1f2a44;
}
.title {
margin: 0 4px 8px;
color: #6b7a99;
font-size: 11px;
font-weight: 600;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 6px;
}
.tile {
display: grid;
justify-items: center;
gap: 4px;
padding: 8px 4px;
border: 1px solid transparent;
border-radius: 10px;
text-align: center;
cursor: pointer;
}
.tile[data-selected='true'] {
border-color: #8fa8e8;
background: #e6edff;
}
.icon {
font-size: 22px;
}
.name {
font-size: 12px;
}
.empty {
grid-column: 1 / -1;
padding: 6px 8px;
color: #6b7a99;
}
</style>
Options
search(query, ctx) => PageLinkItem[] | Promise<PageLinkItem[]>
The pages matching the text typed after [[. A promise is a search: the newest query's answer wins, and the menu reads Searching… meanwhile.
(query, ctx) => PageLinkItem[] | Promise<PageLinkItem[]>href?(page) => string | undefined
A page's URL when search answers none, such as /pages/<id>.
(page) => string | undefinedfromPaste?(text) => PageLinkItem | null | Promise<PageLinkItem | null>
The page a pasted text names (your app's URL for it, page:roadmap…), or null. Asked for a one-line paste at a caret; a page turns the pasted text into its link, one undo step that gives the text back. See Pasted references.
(text) => PageLinkItem | null | Promise<PageLinkItem | null>char?string
The text that opens the menu. Default [[.
stringmenu?Snippet<[TriggerMenuController<PageLinkItem>]>
The menu, at the caret while it is open (without it, nothing shows). Keep its ARIA with {@attach controller.popup} on the list and {@attach controller.option(index)} on each row. See Menus and handles.
Snippet<[TriggerMenuController<PageLinkItem>]>A PageLinkItem is { id, title, href?, icon? }: id is the page’s id in your app, title the text shown, href where the link goes (absolute, or relative to your app) and icon a short icon such as an emoji (default 📄).
The page link atom
A page link is an inline block of type pageLink whose data is { id, title, href?, icon? }:
{ "type": "pageLink", "data": { "id": "p1", "title": "Roadmap", "href": "/pages/p1", "icon": "🗺️" } }
It renders an <a class="edytor-page-link" data-edytor-page-link href="…"> holding the icon (.icon) and the title (.title), with data-selected="true" while the atom is selected (the Notion theme draws the underlined title and the selection), and copies as plain text as its title. The href goes through the link sanitizer when it is stored and when it is rendered: a javascript: or other script URL is dropped, and the atom renders without one. The title is the one stored when it was picked: to show a page’s current title, render your own snippet (pageLinkInlineBlock) that looks it up by data.id.
Behavior
The menu is a trigger of two characters: it opens when the second [ completes [[ at the start of a text or after whitespace. A single [ ([] for a to-do with the markdown shortcuts) opens nothing. ↑/↓ move the highlight, Enter or a click picks, Escape closes it and keeps the text. The pick is one undo step; a refusal keeps [[query. The menu’s listbox is named “Pages”.
For people, see Mention.
Placement. The package places no popup: your markup places itself by controller.anchor (the caret), a virtual element read live. With Floating UI, computePosition(anchor, element, { strategy: 'fixed', middleware: [offset(8), flip(), shift({ padding: 8 })] }) under autoUpdate; with bits-ui, customAnchor={anchor}. See Menus and handles.