Skip to content

fix: editor node converters - #1647

Merged
Philipinho merged 1 commit into
mainfrom
fix/editor-html
Sep 30, 2025
Merged

Philipinho merged 1 commit into
mainfrom
fix/editor-html

Conversation

@Philipinho

@Philipinho Philipinho commented Sep 30, 2025 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of rich-text HTML/JSON conversion.
    • Prevents memory leaks during server-side parsing and serialization.
    • Enforces server-only execution to avoid browser runtime issues and clearer errors.
  • Refactor

    • Consolidated content conversion logic into local utilities for more stable, performant processing.
    • Streamlined serialization flow to reduce external dependencies during conversion.
  • Chores

    • Updated a DOM emulation dependency to a newer major version for improved compatibility and stability.

@coderabbitai

coderabbitai Bot commented Sep 30, 2025 •

Copy link
Copy Markdown

Walkthrough

Shifts server-side ProseMirror HTML/JSON conversion to local helpers. Adds getHTMLFromFragment, refactors generateHTML to use it and enforce Node-only execution, hardens generateJSON with isolated happy-dom parsing and browser guards, updates collaboration util to use local imports, and bumps happy-dom to ^18.0.1.

Changes

Cohort / File(s) Summary
Dependency bump
apps/server/package.json
Update dependency: happy-dom ^15.11.6 → ^18.0.1.
Collaboration util imports
apps/server/src/collaboration/collaboration.util.ts
Replace @tiptap/html import of generateJSON with local ../common/helpers/prosemirror/html import; co-locate generateHTML and generateJSON imports. No API changes.
HTML generation refactor
apps/server/src/common/helpers/prosemirror/html/generateHTML.ts
Replace DOMSerializer/happy-dom logic with call to local getHTMLFromFragment; add Node-only runtime guard; adjust imports to type-only where applicable. Signature unchanged.
JSON generation hardening
apps/server/src/common/helpers/prosemirror/html/generateJSON.ts
Add browser guard; use isolated happy-dom Window and DOMParser; parse full HTML via PMDOMParser.fromSchema; ensure cleanup in finally; improved docs; no exported API changes.
New fragment serializer
apps/server/src/common/helpers/prosemirror/html/getHTMLFromFragment.ts
Add exported getHTMLFromFragment to serialize ProseMirror fragments to HTML using DOMSerializer; supports provided Document or local happy-dom Window; ensures resource cleanup.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant GenHTML as generateHTML
  participant Frag as getHTMLFromFragment
  participant PM as ProseMirror Schema/DOMSerializer
  participant HDom as happy-dom (local)

  Caller->>GenHTML: generateHTML(content, extensions, options)
  GenHTML->>GenHTML: Assert Node environment
  GenHTML->>Frag: getHTMLFromFragment(doc, schema, {document?})
  alt with provided Document
    Frag->>PM: DOMSerializer.fromSchema(schema)
    Frag->>PM: Serialize fragment into wrapper
    Frag-->>GenHTML: innerHTML
  else without Document
    Frag->>HDom: create Window/document
    Frag->>PM: DOMSerializer.fromSchema(schema)
    Frag->>PM: Serialize fragment
    Frag->>HDom: XMLSerializer.serializeToString()
    Frag-->>GenHTML: HTML string
    Frag->>HDom: cleanup
  end
  GenHTML-->>Caller: HTML string
Loading
sequenceDiagram
  autonumber
  actor Caller
  participant GenJSON as generateJSON
  participant HDom as happy-dom (local)
  participant PM as PMDOMParser.fromSchema(schema)

  Caller->>GenJSON: generateJSON(html, extensions, options)
  GenJSON->>GenJSON: Assert not in browser
  GenJSON->>HDom: create Window + DOMParser
  GenJSON->>HDom: Build full HTML doc string
  GenJSON->>HDom: DOMParser.parseFromString(...)
  GenJSON->>PM: parse(document, options)
  PM-->>GenJSON: PM Node
  GenJSON-->>Caller: JSONContent
  GenJSON->>HDom: cleanup (abort/close)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I nudge my whiskers at DOM’s bright glow,
With happy windows spun below.
From shards to scrolls, I weave the thread—
JSON crumbs and HTML bread.
New paths are mapped, old streams run tight;
I thump, then hop—serialization right! 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “fix: editor node converters” clearly summarizes the primary intent of the pull request—correcting the editor’s node conversion utilities—and uses terminology that matches the updated ProseMirror HTML/JSON conversion functions without extraneous detail. It is concise, specific to the main change, and easily understood by teammates reviewing the history.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/editor-html

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🧪 Early access (Sonnet 4.5): enabled

We are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience.

Note:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/server/src/collaboration/collaboration.util.ts (1)

39-41: Update outdated comments.

The comments reference the @tiptap/html library and its issues, but the code now uses local helpers. Update the comments to reflect that these local implementations address the serialization concerns mentioned, or clarify that the issues motivated the move to local helpers.

Apply this diff to clarify the comments:

-// @tiptap/html library works best for generating prosemirror json state but not HTML
-// see: https://github.com/ueberdosis/tiptap/issues/5352
-// see:https://github.com/ueberdosis/tiptap/issues/4089
+// Local HTML/JSON helpers address serialization issues found in @tiptap/html
+// Context: https://github.com/ueberdosis/tiptap/issues/5352
+// Context: https://github.com/ueberdosis/tiptap/issues/4089
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b50e256 and 143947b.

📒 Files selected for processing (5)
  • apps/server/package.json (1 hunks)
  • apps/server/src/collaboration/collaboration.util.ts (1 hunks)
  • apps/server/src/common/helpers/prosemirror/html/generateHTML.ts (1 hunks)
  • apps/server/src/common/helpers/prosemirror/html/generateJSON.ts (1 hunks)
  • apps/server/src/common/helpers/prosemirror/html/getHTMLFromFragment.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
apps/server/src/common/helpers/prosemirror/html/generateHTML.ts (1)
apps/server/src/common/helpers/prosemirror/html/getHTMLFromFragment.ts (1)
  • getHTMLFromFragment (18-54)
🔇 Additional comments (7)
apps/server/src/common/helpers/prosemirror/html/getHTMLFromFragment.ts (1)

18-54: LGTM with minor observation on type casts.

The function structure is solid:

  • Proper fallback to local happy-dom when no document is provided
  • Resource cleanup in finally block prevents memory leaks
  • Clear separation of concerns between provided document and local instance paths

The type casts on lines 41 (as unknown as Document) and 46 (as any) are necessary due to happy-dom/ProseMirror type incompatibilities but are safe in this context since happy-dom implements the required DOM interfaces.

One consideration: the XMLSerializer path (lines 45-46) produces slightly different output than innerHTML (line 31) – XMLSerializer includes XML namespaces and self-closing tags. This is expected behavior, but ensure consumers can handle both formats if the function is called with and without a provided document.

apps/server/src/collaboration/collaboration.util.ts (1)

38-38: LGTM – successfully migrated to local helpers.

The import change from external @tiptap/html to local helpers aligns with the PR objective of using server-side ProseMirror conversion utilities.

apps/server/src/common/helpers/prosemirror/html/generateHTML.ts (2)

19-23: LGTM – excellent environment guard.

The runtime check prevents browser usage and provides clear guidance to use @tiptap/html for client-side scenarios. This is a sensible safeguard for a function that depends on server-specific happy-dom functionality.


25-28: LGTM – clean refactor to reuse getHTMLFromFragment.

Delegating to getHTMLFromFragment eliminates code duplication and centralizes the serialization logic. The function signature remains unchanged, maintaining backward compatibility.

apps/server/src/common/helpers/prosemirror/html/generateJSON.ts (2)

24-28: LGTM – consistent environment guard.

The browser guard matches the pattern in generateHTML.ts, providing consistent behavior across the local helpers module and clear guidance for client-side usage.


30-55: LGTM – robust isolated parsing with proper cleanup.

The implementation is solid:

  • Local Window and DOMParser instances ensure isolation
  • Full HTML document structure (line 38) ensures proper DOM parsing context
  • Null-check on parsed document (lines 41-43) provides clear error messaging
  • Resource cleanup in finally block (lines 49-51) prevents memory leaks
  • Type cast on line 46 (as unknown as Node) is necessary due to happy-dom/ProseMirror interface mismatches but is safe since happy-dom implements the required DOM Node interface

The error handling is appropriate: parse failures are caught explicitly, and the finally block ensures cleanup even if PMDOMParser.fromSchema().parse().toJSON() throws.

apps/server/package.json (1)

65-65: No BrowserContext.close usage detected; run strict TS checks & tests
No direct calls to BrowserContext.close() were found in the codebase—please ensure your strict TypeScript type checks and full test suite pass under happy-dom v18.

@Philipinho
Philipinho merged commit 3135030 into main Sep 30, 2025
github-actions Bot added a commit to antiagingrecord/docmost that referenced this pull request Oct 9, 2025
4231ba9 Merge commit '042836cb6d7cc1f71af3ce44c7957f8abbbf8f25' into feat/aar_edition
042836c sync
4f1f0ba fix
3164b69 feat: api keys management (EE) (docmost#1665)
16c1e86 fix comment space
c9b1cad sync
bf8cf62 feat: Typesense search driver (EE) (docmost#1664)
5a2e16a Merge commit '31350303760163384113bf49d07127e886f94907' into feat/aar_edition
3bc710d Adds CLAUDE.md documentation
16914f1 Block search for non-indexed shares
ca75e29 Set default page mode to Read
3135030 fix editor converter (docmost#1647)
3fae41a fix: editor performance improvements (docmost#1648)
b50e256 sync
1f3b0c7 cloud fix
3c4cab0 v0.23.2
4de25a8 invalidate queries on space deletion
cf5bbb1 fix import html processing
ac17521 sync
9ac180f fix: enhance page import (docmost#1570)
46669fe (cloud) disable page sharing in trial mode
fe6ecdf fix: update combobox props in SpaceSelect component (docmost#1564)
04ae1d7 Allow lastColumnResizable in table
1280f96 feat: implement space and workspace icons (docmost#1558)
61d1cf8 fix: reset file inputs after import
f413720 - sync - reinstantiate S3 client to fix file upload errors during import - delete import zip file after use
8e16ad9 v0.23.1
7ada3cb fix: page import task (docmost#1551)
47c5417 sync
dc06502 sync
091e790 fix attachment search in cloud
ae24ea2 v0.23.0
9df6061 lock file
31053e2 update mermaid
eb8e850 use debug
c99bfb8 make print better
26ea04e sync
6cc58c5 sync
7d2ff34 UI fixes
b08d37f fix
d43ee77 remove debug log
5d91eb4 feat: queue imported attachments for indexing
3e9f6b1 Remove version from docker-compose.yml [deprecated] (docmost#1011)
db55de9 feat: progressive web app (docmost#614)
1919eba New Crowdin updates (docmost#1522)
7951b2e New Crowdin updates (docmost#1509)
73b78f6 more translations
cf7534d fix version display
adec36d fix: adjust margins - use default browser highlight background
f9e1080 sync
00e499b Fixing extra page bug on print (docmost#1478)
5ee6e46 checkbox aligned to text (docmost#1486)
1f797c3 fix: confluence drawio import (docmost#1518)
f12866c feat(EE): full-text search in attachments (docmost#1502)
dcbb65d feat(EE): LDAP integration (docmost#1515)
5968764 feat: emoji callout icon (docmost#1323)
242fb6b fix: set mermaid theme based on computed color scheme (docmost#1438)
74cd890 feat(EE): implement SSO group sync for SAML and OIDC (docmost#1452)
509622a ignore type error
937386e fix: hide table handles in readonly mode
60a373f fix: readonly editor table responsiveness
73ee6ee feat: subpages (child pages) list node (docmost#1462)
7d1e5bc feat: table row/column drag and drop (docmost#1467)
aa58e27 fix: exclude deleted pages (docmost#1494)
08135a2 sync
d92a942 sync
5012a68 sync
5a33777 feat: debug mode env variable (docmost#1450)
3b85f4b fix: enforce C collation for page position ordering to ensure consistent behavior in Postgres 17+ (docmost#1446)
cb2a039 fix: invalidate trashed page from tree state
95b7be6 fix: hide trash from can view permission (docmost#1445)
b0c5572 fix nested taskList in markdown export (docmost#1443)
dddfd48 feat: add attachments support for single page exports (docmost#1440)
aa6eec7 fix: exclude trashed pages from position generation
97a7701 fix local storage copy function (docmost#1442)
b97eb85 sync

git-subtree-dir: vendor/app
git-subtree-split: 4231ba9
@Philipinho
Philipinho deleted the fix/editor-html branch December 14, 2025 19:07
nulluserid pushed a commit to N2con-Inc/docmost that referenced this pull request Dec 17, 2025
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