A mdbook preprocessor that allows the re-usability of template files with dynamic arguments
I'm still a beginner in terms of my Rust skills, so I'm definitely... probably sure that there are edge cases within this preprocessor.
Install Through Cargo
shell
$ cargo install mdbook-template
Add the following line into your book.toml
toml
[preprocessor.template]
You're good to go :D Continue building your mdbook normally!
shell
$ mdbook build
Given the following directory structure
text
book.toml
src
├── rust.md
├── go.md
├── friends
│ └── hazel.md
├── images
│ ├── ferris.png
│ └── corro.png
└── SUMMARY.md
If we wanted to include the images ferris.png
and corro.png
within all the files through a footer, we'd have to copy
the same piece of markdown/code in every file and set a unique path back to the images/
directory.
This is where mdbook-template
can help with the introduction of {{#template ...}
.
Being based on the {{#include ... }}
feature of mdbook, mdbook-template allows you to use familiar syntax to include
files while passing in arguments to allow for dynamic generation of text.
Please view the given example which demonstrates it in action.
The format is as follows
text
1 2 3
{{#template <file> <args>}}
relative path
to the template filekey=value
format.Arguments to be replaced within the template files should be wrapped in {}
```markdown
{{#template file.txt path=../images author=Goudham}}
{{#template file.txt path=../images author=Goudham }}
// Not recommended but valid {{#template file.txt path=../images author=Goudham}}
// Not recommended but valid {{#template file.txt path=../images author=Goudham }}
// Use {{#include}} for simply including files {{#template file.txt}}
{{#template file.txt path=../images author=Goudham}}
{{#template file.txt path=../images author=Goudham }} ```
Given the following directory
text
book.toml
src
├── rust.md
├── go.md
├── friends
│ └── hazel.md
├── images
│ ├── ferris.png
│ └── corro.png
├── templates
│ └── footer.md
└── SUMMARY.md
and the following content
templates/footer.md
markdown
-- Designed By {authors} --


rust.md
```markdown
Some Content...
{{#template templates/footer.md authors=Goudham, Hazel path=images}} ```
go.md
```markdown
Some Content...
{{#template templates/footer.md path=images authors=Goudham, Hazel}} ```
friends/hazel.md
```markdown
Some Content...
{{#template ../templates/footer.md path=../images authors=Goudham, Hazel }} ```
After running mdbook build
with the mdbook-template preprocessor enabled, the files will have dynamic paths to the
images and contain identical content.
rust.md
```markdown
Some Content...
-- Designed By Goudham, Hazel --
```
go.md
```markdown
Some Content...
-- Designed By Goudham, Hazel --
```
friends/hazel.md
```markdown
Some Content...
-- Designed By Goudham, Hazel --
```
Further examples are included within the examples directory which demonstrate a variety of usages.
First, thanks for your interest in contributing to this project! Please read the CONTRIBUTING.md before contributing!
This preprocessor is heavily based off the
links.rs
file within
mdbook itself. I definitely wouldn't have been able to mock up something like
this without the strong foundations that mdbook already implemented.