The text below is meant for typst/typst,
not for this repository.
It is written as it would be posted there, and it goes out once it has been read
and is posted by a person rather than from here.
Until then, encode in crates/typstdoc-core/src/fragment.rs carries the
workaround it describes.
Exporting an HTML fragment rather than a whole document
typst-html exposes two encoders, html and html_in_bundle.
Both take an element as their root and both write <!DOCTYPE html> before it,
so both produce a document.
There is no public way to encode the contents of an element.
For content embedded into a page that Typst did not generate, the document is
exactly what is not wanted.
I compile Typst fragments from Rust doc comments and splice the result into
rustdoc's pages, where an equation has to arrive as <math>...</math> and
nothing else.
html_block_fragment, html_inline_fragment and html_math_fragment in
typst-html/src/fragment.rs do produce nodes rather than a document, but the
module is private and they take an &mut Engine, which cannot be constructed
from outside the compiler.
What I do instead is compile a document, take its <body>, encode that with
html_in_bundle, and strip <!DOCTYPE html> and the body's own tags off the
resulting string.
That works, and the affixes are constants of the encoder, but it reads the
encoder's output rather than its API, so it breaks silently on any change to
how a document is written.
Two shapes would each solve it:
- an encoder over the children of an element, alongside the two that exist,
taking the document for its link resolution;
- a re-export of the fragment functions, together with a way to obtain an
Engine.
The first is the smaller change and is enough for my case.
I am happy to open a PR for whichever shape you prefer.
The text below is meant for typst/typst,
not for this repository.
It is written as it would be posted there, and it goes out once it has been read
and is posted by a person rather than from here.
Until then,
encodeincrates/typstdoc-core/src/fragment.rscarries theworkaround it describes.
Exporting an HTML fragment rather than a whole document
typst-htmlexposes two encoders,htmlandhtml_in_bundle.Both take an element as their root and both write
<!DOCTYPE html>before it,so both produce a document.
There is no public way to encode the contents of an element.
For content embedded into a page that Typst did not generate, the document is
exactly what is not wanted.
I compile Typst fragments from Rust doc comments and splice the result into
rustdoc's pages, where an equation has to arrive as
<math>...</math>andnothing else.
html_block_fragment,html_inline_fragmentandhtml_math_fragmentintypst-html/src/fragment.rsdo produce nodes rather than a document, but themodule is private and they take an
&mut Engine, which cannot be constructedfrom outside the compiler.
What I do instead is compile a document, take its
<body>, encode that withhtml_in_bundle, and strip<!DOCTYPE html>and the body's own tags off theresulting string.
That works, and the affixes are constants of the encoder, but it reads the
encoder's output rather than its API, so it breaks silently on any change to
how a document is written.
Two shapes would each solve it:
taking the document for its link resolution;
Engine.The first is the smaller change and is enough for my case.
I am happy to open a PR for whichever shape you prefer.