Skip to content

fix(markdown): refuse block HTML that would drop its text; create writes nothing on refusal - #65

Open
kklimuk wants to merge 1 commit into
mainfrom
claude/triage-issue-61-html-input-guard
Open

kklimuk wants to merge 1 commit into
mainfrom
claude/triage-issue-61-html-input-guard

Conversation

@kklimuk

@kklimuk kklimuk commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

🤖 docx-cli triage

What was wrong

printf '<h1>Title</h1><p>Body</p>' > p.html; docx create h.docx --from p.html printed create h.docx, exited 0, and read showed no paragraphs. CommonMark treats a line that opens with a block tag (<h1>, <p>, <div>, <table>, …) as one raw HTML block running to the next blank line. The importer then dropped that block along with the text inside it. The same thing happened to <div>/<table> blocks inside ordinary Markdown.

What changed

  • core/markdown/walker.tsx: an HTML block that contains visible text is now refused with USAGE (exit 2). The error names the line and shows a preview of the text that would be lost. The hint tells the agent to rewrite the content as Markdown. <!-- … --> comments (every read hint) and tag-only wrappers (a README's <div align="center"> around real Markdown) still drop silently. Inline tags inside a paragraph are unaffected. The guard applies to every Markdown ingress: create --from, insert/edit --markdown, and note bodies.
  • cli/create: create now runs content, geometry and header/footer changes on one in-memory document (Document.fromPackage) and saves once. Before, it saved a blank file and then reopened it for each step. Now any refused source (block HTML, a bad image) writes nothing, and an existing FILE overwritten with --force survives a failed create.
  • Docs: REFERENCE.md (Markdown dialect), create --help, core/markdown/CLAUDE.md. Tests are in create.test.ts and insert.test.ts. One HEIC test in images.test.ts asserted that a blank file was left behind; it now asserts that no file is written.

Verification

  • Before: exit 0, empty document. After:
    $ docx create h.docx --from p.html
    {"code":"USAGE","error":"Block-level HTML at line 1 would be dropped along with its text (\"Title Body\") — markdown input doesn't convert HTML blocks","hint":"Rewrite it as markdown: …"}   (exit 2, h.docx not created)
    
  • A read of a document whose text contains literal <div>hi</div> emits \<div\>…, so a round trip of read output back through create --from still exits 0.
  • Running create --from ok.md --page-numbers --margins 1.5 --header Hi --size a4 through the single-save path, then read + validate: the output is valid and the header, footer, page settings and footnote are all present.
  • bun run check ✅ bun run test:unit ✅ (2104 tests)
  • /simplify made no changes. /code-review xhigh ran against the wrong checkout and reviewed the previous commit (feat(images)!: refuse HEIC/HEIF input and drop the LGPL libheif dependency #56) instead of this diff; I reverted what it changed there and reviewed this diff by hand.

Decisions for you

  • Refuse or convert. I chose to refuse, as the conservative option. The alternative is to convert common block HTML (h1–h6, p, ul/ol/li, table, br) into OOXML, so an HTML page imports instead of failing. That means more surface to maintain, and anything we can't convert would still need this guard.
  • Scope. The refusal applies to insert/edit --markdown and note bodies as well as create. Content that used to be dropped silently now fails with exit 2. I think this is the right behaviour under "a mutation that changes nothing is an error", but it is a behaviour change.

Fixes #61

Action needed: approve/merge — review PR #65 and tick the two decisions above (refuse vs. convert; refusal on insert/edit too).


Generated by Claude Code

…tes nothing on refusal

`create --from page.html` exited 0 with an empty document: CommonMark
swallows a line opening with a block tag into one opaque html node, and
the importer dropped it with its prose. The walker now refuses a block
html node whose markup carries visible text (USAGE, exit 2) — comments
(every read hint) and tag-only wrappers still drop silently. Applies to
every markdown ingress (create --from, insert/edit --markdown, notes).

`create` now builds in memory over Document.fromPackage and saves once,
so any refused source writes nothing and an existing FILE under --force
survives.

Fixes #61

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create --from an HTML file writes an empty document without a warning

2 participants