Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

3.5 KB
Raw Blame
import { Editor } from "@tiptap/core";
import TaskItem from "@tiptap/extension-task-item";
import TaskList from "@tiptap/extension-task-list";
import { Markdown } from "@tiptap/markdown";
import StarterKit from "@tiptap/starter-kit";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import { pasteMarkdown } from "./paste-markdown";

let editor: Editor;
afterEach(() => editor?.destroy());
function setup(content = "") {
  editor = new Editor({
    extensions: [
      StarterKit,
      TaskList,
      TaskItem.configure({ nested: true }),
      Markdown,
    ],
    content,
    contentType: "markdown",
  });
}
function clipboard(text: string, html = "") {
  return {
    clipboardData: {
      getData: (type: string) => (type === "text/html" ? html : text),
    },
    preventDefault: vi.fn(),
  } as unknown as ClipboardEvent;
}
describe("Markdown paste and edit round trips", () => {
  it.each([
    "**Provide a staff-only interface.**",
    "- [ ] **Bold task**\n- [x] *Done*",
    "# Heading\n\n**Bold** and [link](https://example.com)",
  ])("preserves formatting in %s", (markdown) => {
    setup();
    expect(pasteMarkdown(editor.view, clipboard(markdown))).toBe(true);
    const saved = editor.getMarkdown();
    expect(saved).not.toContain("\\*");
    expect(editor.getHTML()).toContain("<strong>");
    editor.commands.setContent(saved, { contentType: "markdown" });
    expect(editor.getMarkdown()).toBe(saved);
    expect(editor.getHTML()).toContain("<strong>");
  });
  it("inserts inline formatting inside the existing paragraph", () => {
    setup("Hello world");
    editor.commands.setTextSelection(7);
    expect(pasteMarkdown(editor.view, clipboard("**bold** "))).toBe(true);
    expect(editor.getHTML()).toBe("<p>Hello <strong>bold</strong> world</p>");
  });
  it.each([
    "www.youtube.com/watch?v=video",
    "youtube.com/watch?v=video",
    "www.example.com/issue/TEST-1",
  ])("leaves %s to URL handlers", (url) => {
    setup();
    const event = clipboard(url);
    expect(pasteMarkdown(editor.view, event)).toBe(false);
    expect(event.preventDefault).not.toHaveBeenCalled();
  });
  it("leaves rich text, plain text and code-block pastes alone", () => {
    setup();
    expect(
      pasteMarkdown(
        editor.view,
        clipboard("**literal**", "<p>**literal**</p>"),
      ),
    ).toBe(false);
    expect(pasteMarkdown(editor.view, clipboard("plain text"))).toBe(false);
    expect(
      pasteMarkdown(
        editor.view,
        clipboard("https://youtube.com/watch?v=video"),
      ),
    ).toBe(false);
    editor.commands.setCodeBlock();
    expect(pasteMarkdown(editor.view, clipboard("**literal**"))).toBe(false);
  });
  it("leaves formatted text literal inside inline code", () => {
    setup("`literal`");
    editor.commands.setTextSelection(4);
    expect(editor.isActive("code")).toBe(true);
    const event = clipboard("**bold**");
    expect(pasteMarkdown(editor.view, event)).toBe(false);
    expect(event.preventDefault).not.toHaveBeenCalled();
    expect(editor.getHTML()).toBe("<p><code>literal</code></p>");
  });
  it.each(["line  \nbreak", "line\\\nbreak"])(
    "preserves explicit hard breaks in %s",
    (markdown) => {
      setup();
      expect(pasteMarkdown(editor.view, clipboard(markdown))).toBe(true);
      expect(editor.getHTML()).toBe("<p>line<br>break</p>");
    },
  );
  it("does not unescape deliberately literal Markdown on load", () => {
    setup("\\*\\*literal\\*\\*");
    expect(editor.getHTML()).not.toContain("<strong>");
    expect(editor.getMarkdown()).toContain("\\*");
  });
});