Skip to content

Add: provide an an interface to the DOM tree, in addition to a string of HTML. - #74

Merged
letmutex merged 2 commits into
letmutex:mainfrom
bjones1:dom-convert
Apr 3, 2026
Merged

letmutex merged 2 commits into
letmutex:mainfrom
bjones1:dom-convert

Conversation

@bjones1

@bjones1 bjones1 commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

This provides the tree_to_markdown(&html_tree) function, which converts from a DOM tree to markdown just as convert(&html_str) converts from HTML to markdown. It also provides the let tree = html_to_tree(&html_str) utility to convert between HTML and a DOM tree. This allows library users to modify the DOM tree after parsing but before before transforming it to Markdown. Without this, users needs to manipulate the HTML must parse, transform the DOM, render to a string, then pass the string to convert, which again parses the HTML.

Summary by CodeRabbit

  • Refactor
    • Internal HTML→Markdown conversion was modularized into separate parse and render steps for clearer structure and easier maintenance.
    • Conversion behavior and existing public convert API remain unchanged for end-users.
    • Improvements enable more predictable parsing and simplify future enhancements.

@coderabbitai

coderabbitai Bot commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: acf77efe-1415-4307-8ddd-d3375b9e54d3

📥 Commits

Reviewing files that changed from the base of the PR and between 2614211 and 9a1be65.

📒 Files selected for processing (1)
  • src/lib.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/lib.rs

📝 Walkthrough

Walkthrough

The HtmlToMarkdown::convert method was refactored to call two new public APIs: html_to_tree parses HTML into an Rc<Node> DOM, and tree_to_markdown converts a provided DOM into Markdown. Node is now publicly re-exported.

Changes

Cohort / File(s) Summary
API Refactoring
src/lib.rs
Added HtmlToMarkdown::html_to_tree(&self, html: &str) -> std::io::Result<Rc<Node>> and HtmlToMarkdown::tree_to_markdown(&self, tree: &Rc<Node>) -> String. Refactored convert to compose these methods. Publicly re-exported markup5ever_rcdom::Node; RcDom remains internal.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I parsed the leaves and found the root,
Hopped through nodes in a tidy suit,
Split my path, then joined the trail,
Markdown blooms where bytes set sail 🌿✨

🚥 Pre-merge checks | ✅ 3
✅ 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 accurately describes the main change: adding a DOM tree interface API. It clearly summarizes the primary objective of exposing tree-based conversion methods alongside the existing HTML string-based approach.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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

@bjones1
bjones1 marked this pull request as ready for review April 3, 2026 06:00

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
src/lib.rs (2)

105-121: Consider re-exporting Node (and related types) for API usability.

The new public method returns Rc<Node>, but Node is not re-exported from this crate. Users wanting to manipulate the DOM tree before conversion would need to add markup5ever_rcdom as a direct dependency to access Node, NodeData, etc.

Consider adding a pub use statement to re-export the necessary types:

pub use markup5ever_rcdom::Node;

Additionally, the doc comment could be expanded to guide users on DOM manipulation and mention the dependency requirement if re-exporting is not desired.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib.rs` around lines 105 - 121, The public method html_to_tree returns
Rc<Node> but Node (and related types like NodeData) are not re-exported, forcing
consumers to depend on markup5ever_rcdom; add a public re-export (e.g., pub use
markup5ever_rcdom::{Node, NodeData};) near the crate API surface so callers can
use Node directly, and update the html_to_tree doc comment to note the returned
type and how to manipulate the DOM (or, if you choose not to re-export, document
the external dependency requirement instead).

123-142: tree_to_markdown is infallible but returns Result.

This method always returns Ok(content) — there are no fallible operations. Returning Result when failure is impossible can mislead callers into adding unnecessary error handling.

However, keeping Result for API consistency with convert (and allowing for future fallible changes) is a reasonable trade-off. If this is intentional, consider adding a brief doc comment noting that the Result is for API consistency.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib.rs` around lines 123 - 142, The function tree_to_markdown currently
returns std::io::Result<String> but never fails; either change its signature to
return String (and update all callers / the convert API that expect Result) or,
if you want to keep Result for API consistency with convert/future fallible
behavior, add a brief doc comment to the tree_to_markdown method explaining that
it currently always returns Ok but uses Result for consistency and potential
future fallibility so callers need not expect real errors; update the function
signature or add the doc comment near pub fn tree_to_markdown(&self, tree:
&Rc<Node>) to reflect the chosen approach.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/lib.rs`:
- Around line 105-121: The public method html_to_tree returns Rc<Node> but Node
(and related types like NodeData) are not re-exported, forcing consumers to
depend on markup5ever_rcdom; add a public re-export (e.g., pub use
markup5ever_rcdom::{Node, NodeData};) near the crate API surface so callers can
use Node directly, and update the html_to_tree doc comment to note the returned
type and how to manipulate the DOM (or, if you choose not to re-export, document
the external dependency requirement instead).
- Around line 123-142: The function tree_to_markdown currently returns
std::io::Result<String> but never fails; either change its signature to return
String (and update all callers / the convert API that expect Result) or, if you
want to keep Result for API consistency with convert/future fallible behavior,
add a brief doc comment to the tree_to_markdown method explaining that it
currently always returns Ok but uses Result for consistency and potential future
fallibility so callers need not expect real errors; update the function
signature or add the doc comment near pub fn tree_to_markdown(&self, tree:
&Rc<Node>) to reflect the chosen approach.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4e05bf5c-bb2e-4a2f-ad4e-0faab4de3741

📥 Commits

Reviewing files that changed from the base of the PR and between ae6f752 and 2614211.

📒 Files selected for processing (1)
  • src/lib.rs

     Re-export `markup5ever_rcdom::RcDom`.
@letmutex
letmutex merged commit d46d112 into letmutex:main Apr 3, 2026
1 check passed
@letmutex

letmutex commented Apr 3, 2026

Copy link
Copy Markdown
Owner

Thanks!

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.

2 participants