Skip to content

Feature request: resolve relative links against a base URL #287

Description

@jaredlynskey

Use case

I convert server-rendered pages to Markdown so that agents and LLM clients can read them (served behind Accept: text/markdown). The consumer has no page context, so a link like /login or /en/post/ is not resolvable on their side — it needs to be https://example.com/login.

At the moment the rendered output preserves whatever was in the href, so a page full of root-relative links converts into Markdown whose links cannot be followed.

What I'd like

A config option that resolves relative href/src values against a base URL while rendering, roughly:

let config = html2text::config::plain()
    .base_url("https://example.com/some/page");

Semantics I'd expect: standard URL resolution (RFC 3986 style), so /loginhttps://example.com/login, ../x → resolved against the page path, and already-absolute URLs and non-HTTP schemes (mailto:, data:, #fragment) left alone.

Prior art

Python's html2text has exactly this as a constructor argument:

html2text.HTML2Text(baseurl="https://example.com")

It is the one thing keeping me on that library — I came here via html2text_rs and the Rust conversion was ~5x faster on my pages, but the relative links made the output unusable for my case.

Notes

  • I appreciate this may be out of scope if you consider URL resolution the caller's job. A documented "resolve hrefs yourself before rendering" would be a reasonable answer too, though doing it correctly outside the renderer means parsing the HTML twice.
  • If you're open to it in principle I'm happy to attempt a PR — I'd want a steer on where it belongs (during DOM→RenderTree, or in the decorators) before writing anything.
  • Happy to provide sample HTML if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions