An example preprocessor for mdbook
.
Add the configuration below to book.toml
.
[preprocessor.ynn]
command = "<path to executable>"
patterns = [
["<old>", "<new>"],
...
]
timestamp = "<format>"
-
patterns
(optional)patterns
provides a simple replace functionality with user-defined regular expressions.-
"<old>"
is a regular expression. -
"<new>"
is generally a string literal, but$0
,$1
, ... can be used to represent capture groups.
-
-
timestamp
(optional)When
timestamp
is specified, the last commit date of the source file is prepended to each page.The prepended timestamp is enclosed in
<div id='mdbook_preprocessor_last_modified'></div>
, making it customizable via CSS.Commit dates are retrieved this command:
git log -1 --pretty='format:<format>' <file>
[preprocessor.ynn]
command = "/Users/user/.cargo/bin/mdbook_preprocessor"
patterns = [
[":warning:", "⚠️"],
[":check:", "✅"],
["==(.*?)==", "<font color=Red>$1</font>"],
]
timestamp = "🕒 last modified: %cs"
The last pattern converts ==[string]==
into <font color=Red>[string]</font>
.
$ cargo install --locked --path .
$ cargo install --locked --git 'https://github.com/your-diary/mdbook_preprocessor'
$ cargo build --release
$ cargo test
Basically, the only part you may want to edit is the body of the replace()
function in src/replacer.rs
.
pub fn replace(filepath: &Option<PathBuf>, s: &str, config: &Table) -> String {
/* ... */
}
Optionally, you may want to fix or add its tests.
For a more advanced but yet simple example, the official example preprocessor would help.