paperclip/ui/src/lib/normalize-markdown.test.ts
Devin Foley d202631016
fix: autoformat pasted markdown in inline editor (#2673)
## Thinking Path

> - Paperclip orchestrates AI agents for zero-human companies
> - The inline markdown editor (MarkdownEditor / MDXEditor) is used to
edit agent instructions, issue descriptions, and other content
> - When users paste agent instructions copied from terminals or
consoles, extra leading whitespace is uniformly added to every line
> - PR #2572 fixed markdown structure preservation on paste but did not
address the leading whitespace (dedent) problem
> - This pull request adds a Lexical paste normalization plugin that
strips common leading whitespace and normalizes line endings before
MDXEditor processes pasted content
> - The benefit is that pasted content from terminals/consoles renders
correctly without manual cleanup

## What Changed

- **`ui/src/lib/normalize-markdown.ts`** — Pure utility that computes
minimum common indentation across non-empty lines and strips it
(dedent), plus CRLF → LF normalization
- **`ui/src/lib/paste-normalization.ts`** — Lexical `PASTE_COMMAND`
plugin at `CRITICAL` priority that intercepts plain-text pastes,
normalizes the markdown, and re-dispatches cleaned content for MDXEditor
to process. Skips HTML-rich pastes.
- **`ui/src/components/MarkdownEditor.tsx`** — Registers the new plugin;
updates PR #2572's `handlePasteCapture` to use `normalizeMarkdown()`
(dedent + CRLF) instead of `normalizePastedMarkdown()` (CRLF only) for
the markdown-routing path
- **`ui/src/lib/paste-normalization.test.ts`** — 9 unit tests covering
dedent, CRLF normalization, mixed indent, empty lines, single-line
passthrough, and edge cases

## Verification

- `pnpm --dir ui exec vitest run src/lib/paste-normalization.test.ts` —
9 tests pass
- Manual: paste indented agent instructions from a terminal into any
inline markdown editor and confirm leading whitespace is stripped

## Risks

- Low risk. The plugin only activates for plain-text pastes (no HTML
clipboard data). HTML/rich pastes pass through unchanged. Single-line
pastes are not modified. The dedent logic is conservative — it only
strips whitespace common to all non-empty lines.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-04-04 11:21:27 -07:00

59 lines
2.1 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { normalizeMarkdown } from "./normalize-markdown";
describe("normalizeMarkdown", () => {
it("strips common leading whitespace (dedent)", () => {
const input = " # Title\n \n Some text\n - Item 1\n - Item 2";
const expected = "# Title\n\nSome text\n- Item 1\n- Item 2";
expect(normalizeMarkdown(input)).toBe(expected);
});
it("preserves relative indentation within dedented content", () => {
const input = " # Title\n \n Some text\n code block\n More text";
const expected = "# Title\n\nSome text\n code block\nMore text";
expect(normalizeMarkdown(input)).toBe(expected);
});
it("normalizes CRLF to LF", () => {
const input = "line one\r\nline two\r\nline three";
const expected = "line one\nline two\nline three";
expect(normalizeMarkdown(input)).toBe(expected);
});
it("normalizes bare CR to LF", () => {
const input = "line one\rline two\rline three";
const expected = "line one\nline two\nline three";
expect(normalizeMarkdown(input)).toBe(expected);
});
it("returns single-line input unchanged", () => {
const input = " just one line";
expect(normalizeMarkdown(input)).toBe(" just one line");
});
it("returns text unchanged when no common indent", () => {
const input = "# Title\n\nNo indent here\n- list item";
expect(normalizeMarkdown(input)).toBe(input);
});
it("handles empty lines in indented content", () => {
const input = " line one\n\n line two\n \n line three";
const expected = "line one\n\nline two\n\nline three";
expect(normalizeMarkdown(input)).toBe(expected);
});
it("returns empty string unchanged", () => {
expect(normalizeMarkdown("")).toBe("");
});
it("handles mixed indent levels correctly", () => {
const input = " base\n nested\n back\n deep";
const expected = "base\n nested\nback\n deep";
expect(normalizeMarkdown(input)).toBe(expected);
});
it("leaves mixed tab and space indentation unchanged", () => {
const input = "\t# Title\n body\n\t- item";
expect(normalizeMarkdown(input)).toBe(input);
});
});