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 https://github.com/rust-lang/mdBook/issues/1736
|
||
|---|---|---|
| .. | ||
| build | ||
| cli | ||
| config/empty | ||
| includes/all_includes | ||
| index/basic_readme | ||
| init/init_from_summary/src | ||
| markdown | ||
| playground | ||
| preprocessor | ||
| redirects | ||
| renderer | ||
| rendering | ||
| search | ||
| test | ||
| theme | ||
| toc | ||
| book_test.rs | ||
| build.rs | ||
| cli.rs | ||
| config.rs | ||
| includes.rs | ||
| index.rs | ||
| init.rs | ||
| main.rs | ||
| markdown.rs | ||
| playground.rs | ||
| preprocessor.rs | ||
| print.rs | ||
| README.md | ||
| redirects.rs | ||
| renderer.rs | ||
| rendering.rs | ||
| search.rs | ||
| test.rs | ||
| theme.rs | ||
| toc.rs | ||
Testsuite
Introduction
This is the main testsuite for exercising all functionality of mdBook.
Tests should be organized into modules based around major features. Tests should use BookTest to drive the test. BookTest will set up a temp directory, and provides a variety of methods to help create a build books.
Basic structure of a test
Using BookTest, you typically use it to copy a directory into a temp directory, and then run mdbook commands in that temp directory. You can run the mdbook executable, or use the mdbook API to perform whatever tasks you need. Running the executable has the benefit of being able to validate the console output.
See build::basic_build for a simple test example. I recommend reviewing the methods on BookTest to learn more, and reviewing some of the existing tests to get a feel for how they are structured.
For example, let's say you are creating a new theme test. In the testsuite/theme directory, create a new directory with the book source that you want to exercise. At a minimum, this needs a src/SUMMARY.md, but often you'll also want book.toml. Then, in testsuite/theme.rs, add a test with BookTest::from_dir("theme/mytest"), and then use the methods to perform whatever actions you want.
BookTest is designed to be able to chain a series of actions. For example, you can do something like:
BookTest::from_dir("theme/mytest")
.build()
.check_main_file("book/index.html", str![["file contents"]])
.change_file("src/index.md", "new contents")
.build()
.check_main_file("book/index.html", str![["new contents"]]);
Snapbox
The testsuite uses snapbox to drive most of the tests. This library provides the ability to compare strings using a variety of methods. These strings are written in the source code using either the str! or file! macros.
The magic is that you can set the SNAPSHOTS=overwrite environment variable, and snapbox will automatically update the strings contents of str!, or the file contents of file!. This makes it easier to update tests. Snapbox provides nice diffing output, and quite a few other features.
Expected contents can have wildcards like ... (matches any lines) or [..] (matches any characters on a line). See snapbox filters for more info and other filters.
Typically when writing a test, I'll just start with an empty str! or file!, and let snapbox fill it in. Then I review the contents to make sure they are what I expect.
Note that there is some normalization applied to the strings. See book_test::assert for how some of these normalizations happen.