Skip to content

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Sep 17, 2025

This rewrites the HTML rendering pipeline to use a tree data structure, and implements a custom HTML serializer. The intent is to make it easier to make changes and to manipulate the output. This should make some future changes much easier.

This is a large change, but I'll try to briefly summarize what's changing:

  • All of the HTML rendering support has been moved out of mdbook-markdown into mdbook-html. For now, all of the API surface is private, though we may consider ways to safely expose it in the future.
  • Instead of using pulldown-cmark's html serializer, this takes the pulldown-cmark events and translates them into a tree data structure (using the ego-tree crate to define the tree). See tree.rs.
  • HTML in the markdown document is parsed using html5ever, and then lives inside the same tree data structure. See tokenizer.rs.
  • Transformations are then applied to the tree data structure. For example, adding header links or hiding code lines.
  • Serialization is a simple process of writing out the nodes to a string. See serialize.rs.
  • The search indexer works on the tree structure instead of re-rendering every chapter twice. See html_handlebars/search.rs.
  • The print page now takes a very different approach of taking the same tree structure built for rendering the chapters, and applies transformations to it. This avoid re-parsing everything again. See print.rs.
    • I changed the linking behavior so that links on the print page link to items on the print page instead of outside the print page.
  • There are a variety of small changes to how it serializes as can be seen in the changes to the tests. Some highlights:
    • Code blocks no longer have a second layer of <pre> tags wrapping it.
    • Fixed a minor issue where a rust code block with a specific edition was having the wrong classes when there was a default edition.
  • Drops the ammonia dependency, which significantly reduces the number of dependencies. It was only being used for a very minor task, and we can handle it much more easily now.
  • Drops pretty_assertions, they are no longer used (mostly being migrated to the testsuite).

There's obviously a lot of risk trying to parse everything to such a low level, but I think the benefits are worth it. Also, the API isn't super ergonomic compared to say javascript (there are no selectors), but it works well enough so far.

I have not run this through rigorous benchmarking, but it does have a very noticeable performance improvement, especially in a debug build.

I expect in the future that we'll want to expose some kind of integration with extensions so they have access to this tree structure (or some kind of tree structure).

Closes #1736

@rustbot rustbot added the S-waiting-on-review Status: waiting on a review label Sep 17, 2025
@ehuss ehuss added the Breaking Change This would require a SemVer breaking change label Sep 17, 2025
This rewrites the HTML rendering pipeline to use a tree data structure,
and implements a custom HTML serializer. The intent is to make it easier
to make changes and to manipulate the output. This should make some
future changes much easier.

This is a large change, but I'll try to briefly summarize what's
changing:

- All of the HTML rendering support has been moved out of
  mdbook-markdown into mdbook-html. For now, all of the API surface is
  private, though we may consider ways to safely expose it in the
  future.
- Instead of using pulldown-cmark's html serializer, this takes the
  pulldown-cmark events and translates them into a tree data structure
  (using the ego-tree crate to define the tree). See `tree.rs`.
- HTML in the markdown document is parsed using html5ever, and then
  lives inside the same tree data structure. See `tokenizer.rs`.
- Transformations are then applied to the tree data structure. For
  example, adding header links or hiding code lines.
- Serialization is a simple process of writing out the nodes to a
  string. See `serialize.rs`.
- The search indexer works on the tree structure instead of re-rendering
  every chapter twice. See `html_handlebars/search.rs`.
- The print page now takes a very different approach of taking the
  same tree structure built for rendering the chapters, and applies
  transformations to it. This avoid re-parsing everything again. See
  `print.rs`.
    - I changed the linking behavior so that links on the print page
      link to items on the print page instead of outside the print page.
- There are a variety of small changes to how it serializes as can be
  seen in the changes to the tests. Some highlights:
	- Code blocks no longer have a second layer of `<pre>` tags wrapping
      it.
    - Fixed a minor issue where a rust code block with a specific
      edition was having the wrong classes when there was a default
      edition.
- Drops the ammonia dependency, which significantly reduces the number
  of dependencies. It was only being used for a very minor task, and
  we can handle it much more easily now.
- Drops `pretty_assertions`, they are no longer used (mostly being
  migrated to the testsuite).

There's obviously a lot of risk trying to parse everything to such a low
level, but I think the benefits are worth it. Also, the API isn't super
ergonomic compared to say javascript (there are no selectors), but it
works well enough so far.

I have not run this through rigorous benchmarking, but it does have a
very noticeable performance improvement, especially in a debug build.

I expect in the future that we'll want to expose some kind of
integration with extensions so they have access to this tree structure
(or some kind of tree structure).

Closes rust-lang#1736
@ehuss ehuss added this pull request to the merge queue Sep 17, 2025
Merged via the queue into rust-lang:master with commit 1c034bd Sep 17, 2025
14 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: waiting on a review label Sep 17, 2025
ehuss added a commit to ehuss/mdBook that referenced this pull request Sep 20, 2025
@ehuss ehuss mentioned this pull request Sep 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking Change This would require a SemVer breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert all the links in the generated print.html for linking inside the book into URL fragment form

2 participants