rustdoc_katex_demo/
lib.rs

1//! This crate demonstrates an approach to including KaTeX in Rust docs. It tries to balance
2//! readable source code, attractive rendered output, and ease of use.
3//!
4//! Docs with KaTeX can be generated locally and [on docs.rs](https://docs.rs/rustdoc-katex-demo).
5//!
6//! Setup
7//! =====
8//!
9//! You'll only need one file: just grab `katex-header.html` from this project and put it into the
10//! root of your project.
11//!
12//! ## Rendering Locally
13//!
14//! This project can be documented locally with the following commands. Dependencies are
15//! documented separately because you probably don't want your dependencies' docs to use KaTeX.
16//! Also, dependencies would not build correctly because of relative paths.
17//!
18//! ```bash
19//! cargo doc
20//! RUSTDOCFLAGS="--html-in-header katex-header.html" cargo doc --no-deps --open
21//! ```
22//!
23//! ## Rendering on Docs.rs
24//!
25//! Include the following snippet in your `Cargo.toml`:
26//!
27//! ```toml
28//! [package.metadata.docs.rs]
29//! rustdoc-args = [ "--html-in-header", "katex-header.html" ]
30//! ```
31//!
32//! How to Write KaTeX
33//! ==================
34//!
35//! Here is some inline $\KaTeX$.
36//!
37//! And now for a fancy math expression:
38//!
39//! $$
40//! f(x) = \int_{-\infty}^\infty
41//!   \hat f(\xi)e^{2 \pi i \xi x}
42//!   d\xi
43//! $$
44//!
45//! If your math expression has lots of Markdown special characters like _ or *, it may not render
46//! correctly because those characters will be processed by Markdown:
47//!
48//! $$
49//! \sum^{T-1}_{t=1}x_{ij}
50//! $$
51//!
52//! To fix this, you can escape each special character by preceding it with a backslash:
53//!
54//! $$
55//! \sum^{T-1}\_{t=1}x\_{ij}
56//! $$
57//!
58//!
59//!
60//! How to Not Write KaTeX
61//! ======================
62//!
63//! Somehow the section on how to *not* write KaTeX is longer and more complex than that on how to
64//! write KaTeX! I think it's worth it, because being able to use a single dollar sign for inline
65//! math expressions makes the source code a lot more readable.
66//!
67//! If you try to write two dollar signs in one paragraph, your text will render as KaTeX! An
68//! example: $1.00 + $2.00 = $3.00
69//!
70//! Dollar signs inside of code blocks are safe: `$1.00 + $2.00 = $3.00`.
71//!
72//! It's safe to write a single $ as long as there are no other dollar signs in the same HTML
73//! element. This translates to approximately one Markdown paragraph or bullet item.
74//!
75//! If you're the kind of person who likes to write many dollar signs all in the same paragraph,
76//! you can surround each one in a `<span>` element: <span>$1.00</span> + <span>$2.00</span> =
77//! <span>$3.00</span>.
78//!
79//! More
80//! ====
81//!
82//! If you want to use your own set of delimiters, you can change that in `katex-header.html`. The
83//! order of the delimiters is important, so you may wish to consult the KaTeX auto-render
84//! [docs](https://katex.org/docs/autorender.html) and
85//! [source code](https://github.com/Khan/KaTeX/blob/master/contrib/auto-render/auto-render.js).
86//!
87//! This project currently depends on KaTeX's official CDN, jsDelivr. I would also like to describe
88//! a way to bundle the JS and CSS resources into the project.
89//!
90//! Resources
91//! =========
92//!
93//! - [xss-probe on docs.rs](https://docs.rs/xss-probe): a Rust crate that injects JS and CSS into
94//!   rendered docs.rs documentation by using `build.rs` to rewrite some files on the docs.rs build
95//!   machine.
96//! - The curve25519-dalek crate includes
97//!   [lovely rendered KaTeX](https://doc-internal.dalek.rs/curve25519_dalek/curve_models/index.html)
98//!   on their self-hosted docs site.
99//! - [pwnies on docs.rs](https://docs.rs/pwnies): a Rust crate that injects JS and CSS into
100//!   rendered docs.rs documentation using the `--html-in-header` argument.
101//! - [GitHub issue for pwnies](https://github.com/rust-lang-nursery/docs.rs/issues/167)
102//! - [rust-num PR #226](https://github.com/rust-num/num/pull/226): a nice example of KaTeX
103//!   injection with `--html-in-header`. In that thread, cuviper highlights a few problems with
104//!   KaTeX in rustdoc:
105//!   * I would prefer all resources stay local, especially for offline use. KaTeX advertises that
106//!     it is self-contained to allow bundling, so that should be possible.
107//!   * The injection is a bit fragile, leaning on an environment variable, and RUSTDOCFLAGS isn't
108//!     even supported in stable cargo. This also injects into all crates at the time, not just
109//!     num's.
110//!   * This won't work (as-is) for the sub-crates when generated independently. That's important
111//!     for docs.rs where everything is independent.
112//!   * Even from the main crate, it won't work if generated as a dependency in someone else's docs.
113//! - [katex-doc](https://docs.rs/katex-doc), a crate that uses a similar technique but with a few
114//!   differences in implementation.