Files
outline/shared/utils/ProsemirrorDataHelper.ts
Tom MoorandClaude f680d5dec9 feat: Display links, documents and collections as preview cards (#14511)
* feat: Display links as preview cards

Adds a display attribute to mentions so a link can render either inline
or as a card with image, title and description from its unfurl. Pasting
a URL offers a "Card" option, and selecting a link mention shows a
toolbar to switch between the two displays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFgeTUqKhSTriUHfG3z

* feat: Arrow key selection, full width and Return for link cards

Cards fill the document width, hide the blank line below them, are selected
by the up and down arrow keys like images, and Return on a selected card
adds a paragraph below it.

* feat: Display document and collection mentions as cards

Document and collection mentions can be switched to a card from the
selection toolbar. Document cards show the summary, collection and the
first image larger than 100x100. Collection cards show the first line of
the description. On public shares the preview image is signed, as the
share page already signs the attachments of that document. Card images
are hidden until they load and removed if they fail.

* feat: Keep mention cards through Markdown

The card display is written as the link title, so URLs are not changed.
Document and collection cards are written as @ links to their id and
parsed back into mentions.

* refactor: Move mention cards to their own file

Document cards show the collection after the title instead of below it.

* fix: Address review feedback on mention cards

- Include the share id in the document card unfurl URL for root shares.
- Return on a card in a heading adds a paragraph.
- URL cards keep their URL in Markdown, and the server does not narrow
  their type.
- Unfurl results apply to the current node, not the node at first render.
- Collection card descriptions stop at the first line break.

* refactor: Reuse existing helpers for mention cards

- Sign the preview image with TextHelper.attachmentsToSignedUrls.
- Check document and collection ids with isUUID.
- Return lines from getFirstText without changing an input array.

* fix: Preserve JSON output for inline mentions

* refactor: Centralize mention selection classes

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-08 08:11:31 -05:00

79 lines
1.9 KiB
TypeScript

import type { ProsemirrorData } from "../types";
/**
* Helpers that operate on plain `ProsemirrorData` JSON.
*/
export class ProsemirrorDataHelper {
/**
* Get a new empty document.
*
* @returns a new empty document as JSON.
*/
static getEmpty(): ProsemirrorData {
return {
type: "doc",
content: [
{
content: [],
type: "paragraph",
},
],
};
}
/**
* Returns true if the data looks like an empty document.
*
* @param data The ProsemirrorData to check.
* @returns True if the document is empty.
*/
static isEmpty(data: ProsemirrorData): boolean {
if (data.type !== "doc") {
return false;
}
if (data.content?.length === 1) {
const node = data.content[0];
return (
node.type === "paragraph" &&
(node.content === null ||
node.content === undefined ||
node.content.length === 0)
);
}
return !data.content || data.content.length === 0;
}
/**
* Returns the first line of text in the document, useful for a short plain
* text preview. Text blocks and hard breaks start a new line.
*
* @param data the ProsemirrorData to read.
* @returns the trimmed first line with text, or undefined.
*/
static getFirstText(data: ProsemirrorData): string | undefined {
return ProsemirrorDataHelper.toLines(data)
.map((line) => line.trim())
.find(Boolean);
}
private static toLines(node: ProsemirrorData): string[] {
if (node.type === "text") {
return [node.text ?? ""];
}
// Text joins the current line. Any other node, such as a paragraph or a
// hard break, is on its own lines.
const lines = [""];
for (const child of node.content ?? []) {
if (child.type === "text") {
lines[lines.length - 1] += child.text ?? "";
} else {
lines.push(...ProsemirrorDataHelper.toLines(child), "");
}
}
return lines;
}
}