Crates.io | mdbook-tocjs |
lib.rs | mdbook-tocjs |
version | 0.1.1 |
source | src |
created_at | 2024-04-08 17:30:41.908553 |
updated_at | 2024-04-23 04:29:15.863064 |
description | A mdbook preprocessor which adds extra js and css file for ToC hydration. |
homepage | |
repository | https://github.com/acheul/mdbook-collectors |
max_upload_size | |
id | 1200716 |
size | 73,660 |
A mdbook preprocessor which adds extra js and css file for ToC hydration.
This preprocessor does not actually preprocess contents of each chapters. Rather, it adds additional toc.js
& toc.css
file to be used to hydrate ToC(Table of Contents) block and wing.
Two formats of ToC are available: block and wing.
The block format ToC will be generated at a position where you want to place it. On the above image, "Table of Contents" below the main title is the block ToC.
The wing format ToC will be generated at a left side of a post. It will be floating with scroll. Also when you place mouse over the wing, horizontal bars are converting into literals:
cargo install mdbook-tocjs
In a markdown file of a post where you want to make a ToC, include lines like:
<div id="tock"></div>
<div id="tocw"></div>
With node whose id is "tock", the preprocessor formats toc.js
to be able to make a ToC block under the node.
With node whose id is "tocw", the preprocessor formats toc.js
to be able to make a ToC wing of the post.
You can change actual ids with configuration settings in book.toml
.
Inside the toc.js
, toc building methods have some optional arguments. You can pass these options using html's dataset property.
<div id="tock" data-use_number=true data-block_title="Table of Contents"></div>
Above code will make ToC block with number prefix for each title and with a top title of "Table of Contents".
Here are available options: (option: type(default) / details)
use_number: bool(false) / Use number prefix? / for both of block
and wing
block_title: str("Titles") / top title of block
ToC
use_title_name: bool(false) / Use post's title for top title of wing
ToC
root_title: str("Title") / top title of wing
ToC
bar_unit_size: Int(8) / unit size of bar in wing
ToC
wing_left_margin: Int(4) / left margin of wing
ToC
Default setting (in book.toml
):
[preprocessor.tocjs]
save_dir = "lib"
theme_dir = "theme"
block_marker_id = "tock"
wing_marker_id = "tocw"
base_url = "/"
The directory where toc.js
and toc.css
file will be added to.
This directory is relative to the book's src
directory.
🪧 Mind that the
mdbook serve
watchessrc
directory. Once you startmdbook serve
command then change any contents undersrc
directory, the preprocessor may recreate the files and this would lead to a repeat loop of watch and serve. To prevent this, make a .gitignore file at the book's root directory and add thesave_dir
which is joined aftersrc
directory.
The book's theme directory. The preprocessor automatically creates/revise head.hbs
file under this directory. Please make sure to match this setting with your [output.html]
's theme
setting.
The created head.hbs
would look like this:
<link rel="stylesheet" href="/lib/toc.css">
<script src="/lib/toc.js"></script>
Literals to be used as nodes' ids in the raw markdown file. The preprocessor formats toc.js
file to be matched with them.
Sync base url. It's used in head.hbs
.
You can also use additional-js
and additional-css
for more customization. In this case, manually clone and reivse the toc.js
and toc.css
file.
0.1.0
to 0.1.1
base_url
to configuration.