Files
outline/shared/utils/ProsemirrorDataHelper.test.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

146 lines
4.2 KiB
TypeScript

import type { ProsemirrorData } from "../types";
import { ProsemirrorDataHelper } from "./ProsemirrorDataHelper";
describe("ProsemirrorDataHelper", () => {
describe("getEmpty", () => {
it("returns a new empty document each call", () => {
const a = ProsemirrorDataHelper.getEmpty();
const b = ProsemirrorDataHelper.getEmpty();
expect(a).toEqual({
type: "doc",
content: [{ content: [], type: "paragraph" }],
});
expect(a).not.toBe(b);
});
it("produces data considered empty", () => {
expect(
ProsemirrorDataHelper.isEmpty(ProsemirrorDataHelper.getEmpty())
).toBe(true);
});
});
describe("isEmpty", () => {
it("returns false when the root is not a doc", () => {
const data: ProsemirrorData = { type: "paragraph" };
expect(ProsemirrorDataHelper.isEmpty(data)).toBe(false);
});
it("returns true for a doc with no content", () => {
expect(ProsemirrorDataHelper.isEmpty({ type: "doc" })).toBe(true);
expect(ProsemirrorDataHelper.isEmpty({ type: "doc", content: [] })).toBe(
true
);
});
it("returns true for a doc with a single empty paragraph", () => {
const data: ProsemirrorData = {
type: "doc",
content: [{ type: "paragraph", content: [] }],
};
expect(ProsemirrorDataHelper.isEmpty(data)).toBe(true);
});
it("returns false when the single paragraph has content", () => {
const data: ProsemirrorData = {
type: "doc",
content: [
{ type: "paragraph", content: [{ type: "text", text: "hi" }] },
],
};
expect(ProsemirrorDataHelper.isEmpty(data)).toBe(false);
});
it("returns false when there are multiple nodes", () => {
const data: ProsemirrorData = {
type: "doc",
content: [
{ type: "paragraph", content: [] },
{ type: "paragraph", content: [] },
],
};
expect(ProsemirrorDataHelper.isEmpty(data)).toBe(false);
});
});
describe("getFirstText", () => {
it("returns the text of the first block with text", () => {
const data: ProsemirrorData = {
type: "doc",
content: [
{ type: "paragraph", content: [] },
{
type: "paragraph",
content: [
{ type: "text", text: "Hello " },
{ type: "text", text: "world", marks: [{ type: "strong" }] },
],
},
{
type: "paragraph",
content: [{ type: "text", text: "Second" }],
},
],
};
expect(ProsemirrorDataHelper.getFirstText(data)).toBe("Hello world");
});
it("reads text nested in other blocks", () => {
const data: ProsemirrorData = {
type: "doc",
content: [
{
type: "blockquote",
content: [
{ type: "paragraph", content: [{ type: "text", text: "Quote" }] },
],
},
],
};
expect(ProsemirrorDataHelper.getFirstText(data)).toBe("Quote");
});
it("stops at a hard break", () => {
const data: ProsemirrorData = {
type: "doc",
content: [
{
type: "paragraph",
content: [
{ type: "text", text: "First line" },
{ type: "br" },
{ type: "text", text: "Second line" },
],
},
],
};
expect(ProsemirrorDataHelper.getFirstText(data)).toBe("First line");
});
it("stops at the end of a nested block", () => {
const data: ProsemirrorData = {
type: "doc",
content: [
{
type: "blockquote",
content: [
{ type: "paragraph", content: [{ type: "text", text: "First" }] },
{
type: "paragraph",
content: [{ type: "text", text: "Second" }],
},
],
},
],
};
expect(ProsemirrorDataHelper.getFirstText(data)).toBe("First");
});
it("returns undefined for an empty document", () => {
expect(
ProsemirrorDataHelper.getFirstText(ProsemirrorDataHelper.getEmpty())
).toBeUndefined();
});
});
});