mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* 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>
1623 lines
49 KiB
TypeScript
1623 lines
49 KiB
TypeScript
import Revision from "@server/models/Revision";
|
||
import { buildCollection, buildDocument } from "@server/test/factories";
|
||
import { ChangesetHelper } from "@shared/editor/lib/ChangesetHelper";
|
||
import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper";
|
||
import type { ProsemirrorData } from "@shared/types";
|
||
import { HeadingPrefixStyle, TextEditMode } from "@shared/types";
|
||
import { DocumentHelper } from "./DocumentHelper";
|
||
import { ProsemirrorHelper } from "./ProsemirrorHelper";
|
||
|
||
describe("DocumentHelper", () => {
|
||
beforeAll(() => {
|
||
// Fake only Date (for a deterministic system time); leave real timers so
|
||
// toHTML's React effect flush, which awaits setTimeout, is not deadlocked.
|
||
vi.useFakeTimers({ toFake: ["Date"] });
|
||
vi.setSystemTime(Date.parse("2021-01-01T00:00:00.000Z"));
|
||
});
|
||
|
||
afterAll(() => {
|
||
vi.useRealTimers();
|
||
});
|
||
|
||
describe("replaceInternalUrls", () => {
|
||
it("should replace internal urls", async () => {
|
||
const document = await buildDocument({
|
||
text: `[link](/doc/internal-123)`,
|
||
});
|
||
const result = await DocumentHelper.toJSON(document, {
|
||
internalUrlBase: "/s/share-123",
|
||
});
|
||
expect(result).toEqual({
|
||
content: [
|
||
{
|
||
content: [
|
||
{
|
||
marks: [
|
||
{
|
||
attrs: {
|
||
href: "/s/share-123/doc/internal-123",
|
||
title: null,
|
||
},
|
||
type: "link",
|
||
},
|
||
],
|
||
text: "link",
|
||
type: "text",
|
||
},
|
||
],
|
||
type: "paragraph",
|
||
},
|
||
],
|
||
type: "doc",
|
||
});
|
||
});
|
||
|
||
it("should not duplicate share path for URLs that already contain it", async () => {
|
||
const document = await buildDocument({
|
||
text: `[link](/s/testbugpage001/doc/test-page-2-2xIDEXYlib)`,
|
||
});
|
||
const result = await DocumentHelper.toJSON(document, {
|
||
internalUrlBase: "/s/testbugpage001",
|
||
});
|
||
expect(result).toEqual({
|
||
content: [
|
||
{
|
||
content: [
|
||
{
|
||
marks: [
|
||
{
|
||
attrs: {
|
||
href: "/s/testbugpage001/doc/test-page-2-2xIDEXYlib",
|
||
title: null,
|
||
},
|
||
type: "link",
|
||
},
|
||
],
|
||
text: "link",
|
||
type: "text",
|
||
},
|
||
],
|
||
type: "paragraph",
|
||
},
|
||
],
|
||
type: "doc",
|
||
});
|
||
});
|
||
});
|
||
|
||
describe("toProsemirror", () => {
|
||
const linkContent = () => ({
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [
|
||
{
|
||
type: "text",
|
||
text: "link",
|
||
marks: [{ type: "link", attrs: { href: "/doc/internal-123" } }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
});
|
||
|
||
it("should memoize the node for the same content", async () => {
|
||
const document = await buildDocument({ content: linkContent() });
|
||
const node = DocumentHelper.toProsemirror(document);
|
||
|
||
expect(DocumentHelper.toProsemirror(document)).toBe(node);
|
||
});
|
||
|
||
it("should parse again when the content is replaced", async () => {
|
||
const document = await buildDocument({ content: linkContent() });
|
||
const node = DocumentHelper.toProsemirror(document);
|
||
document.content = linkContent();
|
||
|
||
expect(DocumentHelper.toProsemirror(document)).not.toBe(node);
|
||
});
|
||
|
||
it("should not be changed by transforms of its JSON", async () => {
|
||
const document = await buildDocument({ content: linkContent() });
|
||
const node = DocumentHelper.toProsemirror(document);
|
||
|
||
ProsemirrorHelper.replaceInternalUrls(node, "/s/share-123");
|
||
ProsemirrorHelper.replaceDocumentReferences(
|
||
node,
|
||
new Map([["internal-123", { id: "other", path: "/doc/other" }]])
|
||
);
|
||
|
||
const text =
|
||
DocumentHelper.toProsemirror(document).firstChild?.firstChild;
|
||
expect(text?.marks[0].attrs.href).toBe("/doc/internal-123");
|
||
});
|
||
});
|
||
|
||
describe("toJSON", () => {
|
||
it("should return content directly if no transformation required", async () => {
|
||
const document = await buildDocument();
|
||
const result = await DocumentHelper.toJSON(document);
|
||
expect(result === document.content).toBe(true);
|
||
});
|
||
|
||
it("should remove marks and replace internal urls together", async () => {
|
||
const document = await buildDocument({
|
||
text: "link",
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [
|
||
{
|
||
type: "text",
|
||
text: "link",
|
||
marks: [
|
||
{
|
||
type: "comment",
|
||
attrs: { id: "comment-123", userId: "user-123" },
|
||
},
|
||
{ type: "link", attrs: { href: "/doc/internal-123" } },
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
|
||
const result = await DocumentHelper.toJSON(document, {
|
||
removeMarks: ["comment"],
|
||
internalUrlBase: "/s/share-123",
|
||
});
|
||
|
||
const marks = result.content?.[0].content?.[0].marks;
|
||
expect(marks?.map((mark) => mark.type)).toEqual(["link"]);
|
||
expect(marks?.[0].attrs?.href).toBe("/s/share-123/doc/internal-123");
|
||
});
|
||
});
|
||
|
||
describe("toHTML", () => {
|
||
it("should return html", async () => {
|
||
const document = await buildDocument({
|
||
text: "This is a test paragraph",
|
||
});
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
expect(result).toContain('<p dir="auto">This is a test paragraph</p>');
|
||
});
|
||
|
||
it("should apply the cspNonce to the injected mermaid script", async () => {
|
||
const document = await buildDocument({
|
||
text: "```mermaid\ngraph TD;\nA-->B;\n```",
|
||
});
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
includeMermaid: true,
|
||
cspNonce: "test-nonce-123",
|
||
});
|
||
expect(result).toMatch(/<script[^>]*nonce="test-nonce-123"/);
|
||
expect(result).toContain('window.status = "ready"');
|
||
});
|
||
|
||
it("should not set a nonce attribute when cspNonce is not provided", async () => {
|
||
const document = await buildDocument({
|
||
text: "```mermaid\ngraph TD;\nA-->B;\n```",
|
||
});
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
includeMermaid: true,
|
||
});
|
||
expect(result).not.toMatch(/<script[^>]*nonce="/);
|
||
});
|
||
|
||
it("should render math and include the KaTeX stylesheet", async () => {
|
||
const document = await buildDocument({
|
||
text: "Inline $E=mc^2$ and block:\n\n$$\n\\frac{1}{2}\n$$\n",
|
||
});
|
||
const result = await DocumentHelper.toHTML(document);
|
||
expect(result).toContain('class="katex"');
|
||
expect(result).toContain("katex.min.css");
|
||
});
|
||
|
||
it("should not include the KaTeX stylesheet without math", async () => {
|
||
const document = await buildDocument({
|
||
text: "This is a test paragraph",
|
||
});
|
||
const result = await DocumentHelper.toHTML(document);
|
||
expect(result).not.toContain("katex.min.css");
|
||
});
|
||
|
||
it("should number headings according to the document preference", async () => {
|
||
const document = await buildDocument({
|
||
text: "# One\n\n## Two\n\n# Three",
|
||
preferences: { headingPrefix: HeadingPrefixStyle.Numeric },
|
||
});
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
|
||
expect(result).toContain('data-heading-prefix="1."');
|
||
expect(result).toContain('data-heading-prefix="1.1"');
|
||
expect(result).toContain('data-heading-prefix="2."');
|
||
});
|
||
|
||
it("should not number headings without the document preference", async () => {
|
||
const document = await buildDocument({
|
||
text: "# One\n\n## Two",
|
||
});
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
|
||
expect(result).not.toContain("data-heading-prefix");
|
||
});
|
||
|
||
it("should number headings of a revision from its document", async () => {
|
||
const document = await buildDocument({
|
||
text: "# One\n\n## Two",
|
||
preferences: { headingPrefix: HeadingPrefixStyle.Numeric },
|
||
});
|
||
const revision = new Revision({
|
||
documentId: document.id,
|
||
title: document.title,
|
||
text: document.text,
|
||
});
|
||
const result = await DocumentHelper.toHTML(revision, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
|
||
expect(result).toContain('data-heading-prefix="1."');
|
||
expect(result).toContain('data-heading-prefix="1.1"');
|
||
});
|
||
|
||
it("should render diff classes when changes provided", async () => {
|
||
const doc1 = await buildDocument({ text: "Hello world" });
|
||
const doc2 = await buildDocument({ text: "Hello modified world" });
|
||
|
||
const changeset = ChangesetHelper.getChangeset(
|
||
doc2.content,
|
||
doc1.content
|
||
);
|
||
|
||
expect(changeset).not.toBeNull();
|
||
|
||
const result = await DocumentHelper.toHTML(doc2, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
changes: changeset!.changes,
|
||
});
|
||
|
||
expect(result).toContain(EditorStyleHelper.diffInsertion);
|
||
});
|
||
|
||
it("should render a notice through its React component with icon", async () => {
|
||
const document = await buildDocument({
|
||
text: ":::info\nHeads up\n:::",
|
||
});
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
|
||
// Rendered via the React component NodeView, not the plain toDOM spec.
|
||
expect(result).toContain("component-container_notice");
|
||
// The icon moved into the React component (#13109); it must reappear.
|
||
expect(result).toMatch(/class="icon"[^>]*>\s*<svg/);
|
||
// The content hole is preserved and holds the child text.
|
||
expect(result).toMatch(/class="content"[\s\S]*Heads up/);
|
||
});
|
||
|
||
it("should render an embed through its React component as an iframe", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "embed",
|
||
attrs: {
|
||
href: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
||
},
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
|
||
expect(result).toContain("<iframe");
|
||
expect(result).toContain("youtube.com/embed/dQw4w9WgXcQ");
|
||
});
|
||
|
||
it("should render mentions with toDOM", async () => {
|
||
const document = await buildDocument({
|
||
text: `@[Alan Kay](mention://2767ba0e-ac5c-4533-b9cf-4f5fc456600e/user/34095ac1-c808-45c0-8c6e-6c554497de64)`,
|
||
});
|
||
|
||
const result = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
|
||
// Mention's component depends on stores/router, so it opts out of
|
||
// component rendering and emits its toDOM markup directly.
|
||
expect(result).toContain('data-type="user"');
|
||
expect(result).toContain("Alan Kay");
|
||
expect(result).not.toContain("component-mention");
|
||
});
|
||
|
||
it("should include component styles unless styles are disabled", async () => {
|
||
const document = await buildDocument({
|
||
text: ":::info\nStyled\n:::",
|
||
});
|
||
|
||
const withStyles = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
});
|
||
expect(withStyles).toMatch(/<style[^>]*data-styled/);
|
||
|
||
const withoutStyles = await DocumentHelper.toHTML(document, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
expect(withoutStyles).not.toContain("data-styled");
|
||
});
|
||
|
||
it("should isolate concurrent exports", async () => {
|
||
const first = await buildDocument({ text: "First unique alpha content" });
|
||
const second = await buildDocument({
|
||
text: "Second unique beta content",
|
||
});
|
||
|
||
const [firstResult, secondResult] = await Promise.all([
|
||
DocumentHelper.toHTML(first, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
}),
|
||
DocumentHelper.toHTML(second, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
}),
|
||
]);
|
||
|
||
expect(firstResult).toContain("First unique alpha content");
|
||
expect(firstResult).not.toContain("beta");
|
||
expect(secondResult).toContain("Second unique beta content");
|
||
expect(secondResult).not.toContain("alpha");
|
||
});
|
||
|
||
it("should render diff decorations on component nodes", async () => {
|
||
const doc1 = await buildDocument({ text: ":::info\nHello\n:::" });
|
||
const doc2 = await buildDocument({ text: ":::info\nHello world\n:::" });
|
||
|
||
const changeset = ChangesetHelper.getChangeset(
|
||
doc2.content,
|
||
doc1.content
|
||
);
|
||
expect(changeset).not.toBeNull();
|
||
|
||
const result = await DocumentHelper.toHTML(doc2, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
changes: changeset!.changes,
|
||
});
|
||
|
||
expect(result).toContain("component-container_notice");
|
||
expect(result).toContain(EditorStyleHelper.diffInsertion);
|
||
});
|
||
});
|
||
|
||
describe("diff", () => {
|
||
it("should return html with diff", async () => {
|
||
const doc1 = await buildDocument({ text: "Hello world" });
|
||
const doc2 = await buildDocument({ text: "Hello modified world" });
|
||
const revision = new Revision({
|
||
documentId: doc2.id,
|
||
title: doc2.title,
|
||
text: doc2.text,
|
||
});
|
||
|
||
const result = await DocumentHelper.diff(doc1, revision, {
|
||
includeTitle: false,
|
||
includeStyles: false,
|
||
});
|
||
|
||
expect(result).toContain(EditorStyleHelper.diffInsertion);
|
||
});
|
||
});
|
||
|
||
describe("toMarkdownDiff", () => {
|
||
it("should return unified diff hunks of the markdown", async () => {
|
||
const before = await buildDocument({
|
||
text: "Intro\n\n- [ ] Task one\n- [ ] Task two\n\nOutro",
|
||
});
|
||
const after = await buildDocument({
|
||
text: "Intro\n\n- [x] Task one\n- [ ] Task two\n- [ ] Task three\n\nOutro",
|
||
});
|
||
|
||
const result = DocumentHelper.toMarkdownDiff(before, after);
|
||
|
||
expect(result).toEqual(
|
||
[
|
||
"@@ -1,6 +1,7 @@",
|
||
" Intro",
|
||
" ",
|
||
"-- [ ] Task one",
|
||
"+- [x] Task one",
|
||
" - [ ] Task two",
|
||
"+- [ ] Task three",
|
||
" ",
|
||
" Outro",
|
||
].join("\n")
|
||
);
|
||
});
|
||
|
||
it("should limit the context around each change", async () => {
|
||
const lines = Array.from({ length: 20 }, (_, i) => `Line ${i}`);
|
||
const before = await buildDocument({ text: lines.join("\n\n") });
|
||
const after = await buildDocument({
|
||
text: lines.map((l, i) => (i === 10 ? "Changed" : l)).join("\n\n"),
|
||
});
|
||
|
||
const result = DocumentHelper.toMarkdownDiff(before, after, {
|
||
context: 1,
|
||
});
|
||
|
||
expect(result).toEqual(
|
||
["@@ -20,3 +20,3 @@", " ", "-Line 10", "+Changed", " "].join("\n")
|
||
);
|
||
});
|
||
|
||
it("should treat a null before document as empty", async () => {
|
||
const after = await buildDocument({ text: "Hello" });
|
||
|
||
const result = DocumentHelper.toMarkdownDiff(null, after);
|
||
|
||
expect(result).toEqual(["@@ -0,0 +1,1 @@", "+Hello"].join("\n"));
|
||
});
|
||
|
||
it("should return an empty string when nothing changed", async () => {
|
||
const before = await buildDocument({ text: "Same" });
|
||
const after = await buildDocument({ text: "Same" });
|
||
|
||
expect(DocumentHelper.toMarkdownDiff(before, after)).toEqual("");
|
||
});
|
||
|
||
it("should keep the raw serializer output that patch edits search", async () => {
|
||
const before = await buildDocument({ text: "One\n\n\\\n\nTwo" });
|
||
const after = await buildDocument({ text: "One\n\n\\\n\nThree" });
|
||
|
||
const result = DocumentHelper.toMarkdownDiff(before, after);
|
||
|
||
expect(result).toEqual(
|
||
["@@ -2,3 +2,3 @@", " ", " \\", "-Two", "+Three"].join("\n")
|
||
);
|
||
|
||
// Context and added lines together are a valid patch target.
|
||
const findText = (result ?? "")
|
||
.split("\n")
|
||
.filter((line) => line.startsWith(" ") || line.startsWith("+"))
|
||
.map((line) => line.slice(1))
|
||
.join("\n");
|
||
const patched = DocumentHelper.applyMarkdownToDocument(
|
||
after,
|
||
"\n\\\nFour",
|
||
TextEditMode.Patch,
|
||
findText
|
||
);
|
||
expect(await DocumentHelper.toMarkdown(patched)).toContain("Four");
|
||
});
|
||
|
||
it("should return undefined when the diff does not complete in time", async () => {
|
||
const lines = Array.from({ length: 100 }, (_, i) => `Line ${i}`);
|
||
const before = await buildDocument({ text: lines.join("\n\n") });
|
||
const after = await buildDocument({
|
||
text: [...lines].reverse().join("\n\n"),
|
||
});
|
||
|
||
expect(
|
||
DocumentHelper.toMarkdownDiff(before, after, { timeout: 0 })
|
||
).toBeUndefined();
|
||
expect(DocumentHelper.toMarkdownDiff(before, after)).toContain("+Line 0");
|
||
});
|
||
});
|
||
|
||
describe("applyMarkdownToDocument", () => {
|
||
it("should keep mention cards when the whole document is replaced", async () => {
|
||
const modelId = "6f4e9a32-5b1c-4d2e-9f3a-8b7c6d5e4f30";
|
||
const mention = (attrs: Record<string, string>) => ({
|
||
type: "paragraph",
|
||
content: [
|
||
{
|
||
type: "mention",
|
||
attrs: {
|
||
id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
|
||
modelId,
|
||
display: "card",
|
||
...attrs,
|
||
},
|
||
},
|
||
],
|
||
});
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
mention({
|
||
type: "url",
|
||
label: "Example",
|
||
href: "https://example.com/a?b=c",
|
||
}),
|
||
mention({ type: "document", label: "Roadmap" }),
|
||
mention({ type: "collection", label: "Product" }),
|
||
],
|
||
},
|
||
});
|
||
|
||
const markdown = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
const result = DocumentHelper.applyMarkdownToDocument(
|
||
document,
|
||
markdown,
|
||
TextEditMode.Replace
|
||
);
|
||
|
||
const mentions: Array<Record<string, unknown>> = [];
|
||
DocumentHelper.toProsemirror(result).descendants((node) => {
|
||
if (node.type.name === "mention") {
|
||
mentions.push(node.attrs);
|
||
}
|
||
});
|
||
expect(mentions[0].href).toBe("https://example.com/a?b=c");
|
||
expect(mentions.map((attrs) => [attrs.type, attrs.display])).toEqual([
|
||
["url", "card"],
|
||
["document", "card"],
|
||
["collection", "card"],
|
||
]);
|
||
});
|
||
|
||
it("should merge prepended text into the first paragraph", async () => {
|
||
const document = await buildDocument({ text: "Existing" });
|
||
const result = DocumentHelper.applyMarkdownToDocument(
|
||
document,
|
||
"x",
|
||
TextEditMode.Prepend
|
||
);
|
||
expect(result.text).toBe("xExisting");
|
||
});
|
||
|
||
it("should merge prepended text that ends with spaces", async () => {
|
||
const document = await buildDocument({ text: "Existing" });
|
||
const result = DocumentHelper.applyMarkdownToDocument(
|
||
document,
|
||
"x ",
|
||
TextEditMode.Prepend
|
||
);
|
||
expect(result.text).toBe("xExisting");
|
||
});
|
||
|
||
it("should not merge prepended text when trailing whitespace has a newline", async () => {
|
||
const document = await buildDocument({ text: "Existing" });
|
||
const result = DocumentHelper.applyMarkdownToDocument(
|
||
document,
|
||
"x \n ",
|
||
TextEditMode.Prepend
|
||
);
|
||
expect(result.text).toBe("x\n\nExisting");
|
||
});
|
||
});
|
||
|
||
describe("parseMentions", () => {
|
||
it("should not parse normal links as mentions", async () => {
|
||
const document = await buildDocument({
|
||
text: `# Header
|
||
|
||
[link not mention](http://google.com)`,
|
||
});
|
||
const result = DocumentHelper.parseMentions(document);
|
||
expect(result.length).toBe(0);
|
||
});
|
||
|
||
it("should return an array of mentions", async () => {
|
||
const document = await buildDocument({
|
||
text: `# Header
|
||
|
||
@[Alan Kay](mention://2767ba0e-ac5c-4533-b9cf-4f5fc456600e/user/34095ac1-c808-45c0-8c6e-6c554497de64) :wink:
|
||
|
||
More text
|
||
|
||
@[Bret Victor](mention://34095ac1-c808-45c0-8c6e-6c554497de64/user/2767ba0e-ac5c-4533-b9cf-4f5fc456600e) :fire:`,
|
||
});
|
||
const result = DocumentHelper.parseMentions(document);
|
||
expect(result.length).toBe(2);
|
||
expect(result[0].id).toBe("2767ba0e-ac5c-4533-b9cf-4f5fc456600e");
|
||
expect(result[1].id).toBe("34095ac1-c808-45c0-8c6e-6c554497de64");
|
||
expect(result[0].modelId).toBe("34095ac1-c808-45c0-8c6e-6c554497de64");
|
||
expect(result[1].modelId).toBe("2767ba0e-ac5c-4533-b9cf-4f5fc456600e");
|
||
});
|
||
});
|
||
|
||
describe("toMarkdown", () => {
|
||
it("should preserve smart quotes rather than flattening them", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "the cat’s “meow”" }],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
expect(result).toBe("the cat’s “meow”");
|
||
});
|
||
|
||
it("should not crash serializing a table with no rows", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{ type: "paragraph", content: [{ type: "text", text: "before" }] },
|
||
{ type: "table", content: [] },
|
||
{ type: "paragraph", content: [{ type: "text", text: "after" }] },
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
expect(result).toBe("before\n\nafter");
|
||
});
|
||
|
||
it("should not escape standalone square brackets", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "one [two] three" }],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
expect(result).toBe("one [two] three");
|
||
});
|
||
|
||
it("should escape literal inline-link syntax with nested brackets", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "nested [a[b]c](url) link" }],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Escaped so it is not re-parsed into a link on round-trip.
|
||
expect(result).toBe("nested \\[a\\[b]c](url) link");
|
||
});
|
||
|
||
it("should export bullet lists inside table cells with br tags", async () => {
|
||
// Create a document with a table containing a bullet list in a cell
|
||
// This tests the renderList inTable handling
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "table",
|
||
content: [
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Header" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "bullet_list",
|
||
content: [
|
||
{
|
||
type: "list_item",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "item 1" }],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "list_item",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "item 2" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Lists inside tables should use <br> tags instead of newlines
|
||
expect(result).toContain("<br>");
|
||
expect(result).toContain("* item 1");
|
||
expect(result).toContain("* item 2");
|
||
// Should not have newlines between list items within the table cell
|
||
expect(result).not.toMatch(/\* item 1\n\* item 2/);
|
||
});
|
||
|
||
it("should export ordered lists inside table cells with br tags", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "table",
|
||
content: [
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Header" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "ordered_list",
|
||
attrs: { order: 1 },
|
||
content: [
|
||
{
|
||
type: "list_item",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "first" }],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "list_item",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "second" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Ordered lists inside tables should use <br> tags
|
||
expect(result).toContain("<br>");
|
||
expect(result).toContain("1. first");
|
||
expect(result).toContain("2. second");
|
||
});
|
||
|
||
it("should pad table cells to match header width", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "table",
|
||
content: [
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Long Header" }],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Col 2" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "A" }],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "B" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Cells should be padded to match header width
|
||
// "A" padded to 11 chars (length of "Long Header")
|
||
// "B" padded to 5 chars (length of "Col 2")
|
||
expect(result).toContain("| A |"); // A + 10 spaces = 11 chars
|
||
expect(result).toContain("| B |"); // B + 4 spaces = 5 chars
|
||
});
|
||
|
||
it("should export checkbox lists inside table cells with br tags", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "table",
|
||
content: [
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Header" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "checkbox_list",
|
||
content: [
|
||
{
|
||
type: "checkbox_item",
|
||
attrs: { checked: false },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "todo" }],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "checkbox_item",
|
||
attrs: { checked: true },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "done" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Checkbox lists inside tables should use <br> tags
|
||
expect(result).toContain("<br>");
|
||
expect(result).toContain("[ ] todo");
|
||
expect(result).toContain("[x] done");
|
||
});
|
||
|
||
it("should export code fences inside table cells on a single line", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "table",
|
||
content: [
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Header" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "code_fence",
|
||
attrs: { language: "abap" },
|
||
content: [
|
||
{ type: "text", text: "a | b\nc \\ d\nline 2" },
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Code fences inside tables should use <br> tags rather than literal
|
||
// newlines that would break the table row structure, with pipes and
|
||
// backslashes escaped so the content cannot break out of the column.
|
||
expect(result).toContain(
|
||
"```abap<br>a \\| b<br>c \\\\ d<br>line 2<br>```"
|
||
);
|
||
// The fence content must not introduce raw newlines inside the table.
|
||
expect(result).not.toMatch(/```abap\n/);
|
||
});
|
||
|
||
it("should export math blocks inside table cells on a single line", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "table",
|
||
content: [
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Header" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "math_block",
|
||
content: [
|
||
{ type: "text", text: "a | b\n\\frac{1}{2}" },
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Math blocks inside tables should use <br> tags rather than literal
|
||
// newlines that would break the table row structure, with pipes and
|
||
// backslashes escaped so the content cannot break out of the column.
|
||
expect(result).toContain("$$<br>a \\| b<br>\\\\frac{1}{2}<br>$$");
|
||
// The block content must not introduce raw newlines inside the table.
|
||
expect(result).not.toMatch(/\$\$\n/);
|
||
});
|
||
|
||
it("should export notice blocks inside table cells on a single line", async () => {
|
||
const document = await buildDocument({
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "table",
|
||
content: [
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "th",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Header" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
type: "tr",
|
||
content: [
|
||
{
|
||
type: "td",
|
||
attrs: { colspan: 1, rowspan: 1 },
|
||
content: [
|
||
{
|
||
type: "container_notice",
|
||
attrs: { style: "warning" },
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "First | line" }],
|
||
},
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Second line" }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(document, {
|
||
includeTitle: false,
|
||
});
|
||
// Notice blocks inside tables should use <br> tags rather than literal
|
||
// newlines that would break the table row structure, with pipes escaped
|
||
// so the content cannot break out of the column.
|
||
expect(result).toContain(
|
||
":::warning<br>First \\| line<br><br>Second line<br><br>:::"
|
||
);
|
||
// The notice must not introduce raw newlines inside the table.
|
||
expect(result).not.toMatch(/:::warning\n/);
|
||
});
|
||
|
||
it("should include collection title by default", async () => {
|
||
const collection = await buildCollection({
|
||
name: "Test Collection",
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Collection description" }],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(collection);
|
||
expect(result).toContain("# Test Collection");
|
||
expect(result).toContain("Collection description");
|
||
});
|
||
|
||
it("should include collection emoji icon in title", async () => {
|
||
const collection = await buildCollection({
|
||
name: "Test Collection",
|
||
icon: "📚",
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Collection description" }],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(collection);
|
||
expect(result).toContain("# 📚 Test Collection");
|
||
});
|
||
|
||
it("should not include collection title when includeTitle is false", async () => {
|
||
const collection = await buildCollection({
|
||
name: "Test Collection",
|
||
icon: "📚",
|
||
content: {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [{ type: "text", text: "Collection description" }],
|
||
},
|
||
],
|
||
},
|
||
});
|
||
const result = await DocumentHelper.toMarkdown(collection, {
|
||
includeTitle: false,
|
||
});
|
||
expect(result).not.toContain("# ");
|
||
expect(result).not.toContain("Test Collection");
|
||
expect(result).toContain("Collection description");
|
||
});
|
||
});
|
||
|
||
describe("toPlainText", () => {
|
||
it("should return only plain text", async () => {
|
||
const revision = new Revision({
|
||
title: "Title",
|
||
text: `
|
||
This is a test paragraph
|
||
|
||
A new [link](https://www.google.com)
|
||
|
||
- list item 1
|
||
|
||
This is a new paragraph.
|
||
|
||
!!This is a placeholder!!
|
||
|
||
==this is a highlight==
|
||
|
||
- [x] checklist item 1
|
||
- [x] checklist item 2
|
||
- [ ] checklist item 3
|
||
- [ ] checklist item 4
|
||
- [x] checklist item 5
|
||
|
||
| This | Is | Table |
|
||
|----|----|----|
|
||
| Multiple \n Lines \n In a cell | | |
|
||
| | | |`,
|
||
});
|
||
|
||
const text = DocumentHelper.toPlainText(revision);
|
||
|
||
// Strip all formatting
|
||
expect(text).toEqual(`This is a test paragraph
|
||
A new link
|
||
list item 1
|
||
This is a new paragraph.
|
||
This is a placeholder
|
||
this is a highlight
|
||
checklist item 1
|
||
checklist item 2
|
||
checklist item 3
|
||
checklist item 4
|
||
checklist item 5
|
||
This
|
||
Is
|
||
Table
|
||
Multiple
|
||
|
||
|
||
Lines
|
||
|
||
|
||
In a cell
|
||
|
||
|
||
|
||
|
||
`);
|
||
});
|
||
});
|
||
|
||
describe("getPreviewImage", () => {
|
||
const image = (src: string, width?: number, height?: number) => ({
|
||
type: "paragraph",
|
||
content: [{ type: "image", attrs: { src, alt: "", width, height } }],
|
||
});
|
||
|
||
it("returns the first image larger than the minimum size", () => {
|
||
const data: ProsemirrorData = {
|
||
type: "doc",
|
||
content: [
|
||
image("/icon.png", 32, 32),
|
||
image("/narrow.png", 400, 80),
|
||
image("/large.png", 800, 600),
|
||
image("/second.png", 800, 600),
|
||
],
|
||
};
|
||
expect(DocumentHelper.getPreviewImage(data)).toBe("/large.png");
|
||
});
|
||
|
||
it("skips images without a stored size", () => {
|
||
const data: ProsemirrorData = {
|
||
type: "doc",
|
||
content: [image("/unknown.png")],
|
||
};
|
||
expect(DocumentHelper.getPreviewImage(data)).toBeUndefined();
|
||
});
|
||
|
||
it("finds images nested in other blocks", () => {
|
||
const data: ProsemirrorData = {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "blockquote",
|
||
content: [image("/quoted.png", 200, 200)],
|
||
},
|
||
],
|
||
};
|
||
expect(DocumentHelper.getPreviewImage(data)).toBe("/quoted.png");
|
||
});
|
||
});
|
||
|
||
describe("getAnchorContent", () => {
|
||
it("should return the section content for a matching heading anchor", async () => {
|
||
const document = await buildDocument({
|
||
text: `Intro paragraph.
|
||
|
||
## Installation
|
||
|
||
Install instructions here.
|
||
|
||
More install detail.
|
||
|
||
## Usage
|
||
|
||
Usage instructions here.`,
|
||
});
|
||
|
||
const result = DocumentHelper.getAnchorContent(
|
||
document,
|
||
"h-installation"
|
||
);
|
||
expect(result).toBe(
|
||
`## Installation
|
||
|
||
Install instructions here.
|
||
|
||
More install detail.`
|
||
);
|
||
});
|
||
|
||
it("should accept an anchor with a leading hash", async () => {
|
||
const document = await buildDocument({
|
||
text: `## Installation
|
||
|
||
Install instructions here.`,
|
||
});
|
||
|
||
const result = DocumentHelper.getAnchorContent(
|
||
document,
|
||
"#h-installation"
|
||
);
|
||
expect(result).toBe(
|
||
`## Installation
|
||
|
||
Install instructions here.`
|
||
);
|
||
});
|
||
|
||
it("should stop at the next heading of the same or higher level", async () => {
|
||
const document = await buildDocument({
|
||
text: `## Section
|
||
|
||
Section body.
|
||
|
||
### Subsection
|
||
|
||
Subsection body.
|
||
|
||
## Other
|
||
|
||
Other body.`,
|
||
});
|
||
|
||
const result = DocumentHelper.getAnchorContent(document, "h-section");
|
||
expect(result).toBe(
|
||
`## Section
|
||
|
||
Section body.
|
||
|
||
### Subsection
|
||
|
||
Subsection body.`
|
||
);
|
||
});
|
||
|
||
it("should resolve anchors for duplicate headings", async () => {
|
||
const document = await buildDocument({
|
||
text: `## Notes
|
||
|
||
First notes.
|
||
|
||
## Notes
|
||
|
||
Second notes.`,
|
||
});
|
||
|
||
const result = DocumentHelper.getAnchorContent(document, "h-notes-1");
|
||
expect(result).toBe(
|
||
`## Notes
|
||
|
||
Second notes.`
|
||
);
|
||
});
|
||
|
||
it("should resolve anchors for a heading that ends in a number", async () => {
|
||
const document = await buildDocument({
|
||
text: `## Setup
|
||
|
||
First setup.
|
||
|
||
## Setup
|
||
|
||
Second setup.
|
||
|
||
## Setup 1
|
||
|
||
Third setup.`,
|
||
});
|
||
|
||
expect(DocumentHelper.getAnchorContent(document, "h-setup-1")).toBe(
|
||
`## Setup
|
||
|
||
Second setup.`
|
||
);
|
||
expect(DocumentHelper.getAnchorContent(document, "h-setup-1-1")).toBe(
|
||
`## Setup 1
|
||
|
||
Third setup.`
|
||
);
|
||
});
|
||
|
||
it("should count nested headings when numbering duplicates", async () => {
|
||
const document = await buildDocument({
|
||
text: `> ## Notes
|
||
>
|
||
> Quoted notes.
|
||
|
||
## Notes
|
||
|
||
Top-level notes.`,
|
||
});
|
||
|
||
expect(
|
||
DocumentHelper.getAnchorContent(document, "h-notes")
|
||
).toBeUndefined();
|
||
expect(DocumentHelper.getAnchorContent(document, "h-notes-1")).toBe(
|
||
`## Notes
|
||
|
||
Top-level notes.`
|
||
);
|
||
});
|
||
|
||
it("should return undefined when no heading matches", async () => {
|
||
const document = await buildDocument({
|
||
text: `## Installation
|
||
|
||
Install instructions here.`,
|
||
});
|
||
|
||
expect(
|
||
DocumentHelper.getAnchorContent(document, "h-missing")
|
||
).toBeUndefined();
|
||
expect(DocumentHelper.getAnchorContent(document, "")).toBeUndefined();
|
||
});
|
||
});
|
||
|
||
describe("isContentEqual", () => {
|
||
const paragraph = (text: string, marks?: ProsemirrorData["marks"]) => ({
|
||
type: "doc",
|
||
content: [
|
||
{ type: "paragraph", content: [{ type: "text", text, marks }] },
|
||
],
|
||
});
|
||
const comment = {
|
||
type: "comment",
|
||
attrs: { id: "comment-id", userId: "user-id" },
|
||
};
|
||
|
||
it("should return true for identical content", async () => {
|
||
const document = await buildDocument({ content: paragraph("Hello") });
|
||
const revision = Revision.buildFromDocument(document);
|
||
|
||
expect(DocumentHelper.isContentEqual(document, revision)).toBe(true);
|
||
});
|
||
|
||
it("should return false when marks differ and none are ignored", async () => {
|
||
const document = await buildDocument({ content: paragraph("Hello") });
|
||
const revision = Revision.buildFromDocument(document);
|
||
document.content = paragraph("Hello", [comment]);
|
||
|
||
expect(DocumentHelper.isContentEqual(document, revision)).toBe(false);
|
||
});
|
||
|
||
it("should return true when only ignored marks differ", async () => {
|
||
const document = await buildDocument({ content: paragraph("Hello") });
|
||
const revision = Revision.buildFromDocument(document);
|
||
document.content = paragraph("Hello", [comment]);
|
||
|
||
expect(
|
||
DocumentHelper.isContentEqual(document, revision, {
|
||
ignoreMarks: ["comment"],
|
||
})
|
||
).toBe(true);
|
||
});
|
||
|
||
it("should return false when the text differs", async () => {
|
||
const document = await buildDocument({ content: paragraph("Hello") });
|
||
const revision = Revision.buildFromDocument(document);
|
||
document.content = paragraph("Hello world", [comment]);
|
||
|
||
expect(
|
||
DocumentHelper.isContentEqual(document, revision, {
|
||
ignoreMarks: ["comment"],
|
||
})
|
||
).toBe(false);
|
||
});
|
||
|
||
it("should return true when a comment covers part of a text node", async () => {
|
||
const document = await buildDocument({
|
||
content: paragraph("Hello world"),
|
||
});
|
||
const revision = Revision.buildFromDocument(document);
|
||
document.content = {
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [
|
||
{ type: "text", text: "Hello " },
|
||
{ type: "text", text: "world", marks: [comment] },
|
||
],
|
||
},
|
||
],
|
||
};
|
||
|
||
expect(
|
||
DocumentHelper.isContentEqual(document, revision, {
|
||
ignoreMarks: ["comment"],
|
||
})
|
||
).toBe(true);
|
||
});
|
||
|
||
it("should return true when only an image comment differs", async () => {
|
||
const image = (marks?: ProsemirrorData["marks"]) => ({
|
||
type: "doc",
|
||
content: [
|
||
{
|
||
type: "paragraph",
|
||
content: [
|
||
{
|
||
type: "image",
|
||
attrs: { src: "https://example.com/image.png", marks },
|
||
},
|
||
],
|
||
},
|
||
],
|
||
});
|
||
const document = await buildDocument({ content: image() });
|
||
const revision = Revision.buildFromDocument(document);
|
||
document.content = image([comment]);
|
||
|
||
expect(
|
||
DocumentHelper.isContentEqual(document, revision, {
|
||
ignoreMarks: ["comment"],
|
||
})
|
||
).toBe(true);
|
||
|
||
document.content = image([]);
|
||
expect(
|
||
DocumentHelper.isContentEqual(document, revision, {
|
||
ignoreMarks: ["comment"],
|
||
})
|
||
).toBe(true);
|
||
});
|
||
});
|
||
});
|