Skip to content

[Prototype] Convert plain text/thin-wrapper paste to formatted HTML via markdown#3361

Closed
JiuqingSong wants to merge 1 commit into
masterfrom
paste-markdown-as-html
Closed

[Prototype] Convert plain text/thin-wrapper paste to formatted HTML via markdown#3361
JiuqingSong wants to merge 1 commit into
masterfrom
paste-markdown-as-html

Conversation

@JiuqingSong

@JiuqingSong JiuqingSong commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

@

Summary

Adds a feature to PastePlugin that detects plain-text / markdown-style paste content and converts it to formatted HTML by default.

When content is pasted with the default source (not Word/Excel/PowerPoint/OneNote/etc.) and as a normal paste, the plugin now:

  1. Detects whether the copied content is effectively plain text:
    • There must be non-empty plain text (clipboardData.text).
    • Either there is no HTML (rawHtml null/undefined/empty), or the HTML is a thin wrapper of the plain text — every element is a structural tag (DIV, P, BR, SPAN), no element carries attributes (no style, class, etc.), and the HTML text matches the plain text. Any real formatting element (<b>, <a>, <table>, styled spans…) disqualifies it.
  2. Converts the plain text through convertMarkdownToContentModel, turns the resulting Content Model into a DOM tree via contentModelToDom, and replaces the contents of BeforePasteEvent.fragment in place — so the rest of the paste pipeline sees the formatted HTML instead of raw markdown.

This makes pasting markdown text produce formatted HTML (headings, lists, links, etc.) by default instead of literal markdown.

Scope / safety

  • Gated to pasteType === normal, so "paste as plain text" (asPlainText) keeps raw text, and mergeFormat/asImage are untouched.
  • Detection is conservative: it only triggers when the HTML genuinely adds no formatting.

Files changed

  • lib/paste/Markdown/convertPastedTextToMarkdown.ts (new) — shouldConvertPastedTextToMarkdown (detection) and convertPastedTextToMarkdown (conversion + fragment replacement).
  • lib/paste/PastePlugin.ts — added a default case to the paste-source switch that runs detection/conversion.
  • package.json — added roosterjs-content-model-markdown dependency.
  • Tests — new test/paste/Markdown/convertPastedTextToMarkdownTest.ts plus integration tests in ContentModelPastePluginTest.ts.

Verification

  • yarn test:fast (filtered) — 29 passing
  • yarn eslint — clean
  • yarn b — build succeeds (confirms the new cross-package dependency resolves)
pastemarkdown2

🤖 Generated with Claude Code
@

Convert plain text/thin-wrapper paste to formatted HTML via markdown

When pasted content is plain text, or HTML that is only a thin wrapper of
plain text (e.g. each line wrapped in a DIV or P with no other formatting),
interpret it as markdown and replace the paste fragment with the converted
formatted HTML, so we paste formatted content by default instead of raw
markdown text.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-06-19 17:17 UTC

@JiuqingSong JiuqingSong changed the title Convert plain text/thin-wrapper paste to formatted HTML via markdown [Prototype] Convert plain text/thin-wrapper paste to formatted HTML via markdown Jun 8, 2026
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.

1 participant