Crates.io | mdbook-gitbook |
lib.rs | mdbook-gitbook |
version | 1.0.3 |
source | src |
created_at | 2024-02-01 01:54:06.877469 |
updated_at | 2024-02-05 16:05:12.162955 |
description | mdBook preprocessor to properly render GitBook specific syntax |
homepage | |
repository | https://github.com/GeckoEidechse/mdbook-gitbook |
max_upload_size | |
id | 1122543 |
size | 118,442 |
mdBook preprocessor to properly render GitBook specific syntax in mdBook.
You can compare this preprocessor to GitBook by looking at the same sample book on:
To see the list of existing and supported syntax, see this GitHub issue.
Note:
This mdBook preprocessor was written for local rendering of the NorthstarWiki where it covers the majority of the used GitBook-specific syntax. As such I'm not planning to add support for more GitBook widgets etc, however contributions to expand the supported syntax are more than welcome <3
Similarly, if you'd like to take over maintainership or ownership of this crate, please get in touch via an issue in the GitHub repo of this crate.
First, install the preprocessor:
cargo install mdbook-gitbook
Then, add the preprocessor to your book.toml
:
[book]
authors = ["Jill Doe"]
language = "en"
multilingual = false
src = "src"
title = "My awesome Book"
# ADD THIS
[preprocessor.gitbook]
The way this preprocessor works is primarily by using regexes to search for specific patterns like
{% embed url="URL_HERE" %}
and then replacing it with the corresponding HTML code like
<div style="border: 1px solid #ccc; padding: 10px; max-width: 500px; margin: 10px">
<a href="URL_HERE">
<div style="display: flex; align-items: center;">
<div style="margin-right: 10px;">
<img alt="" src="{icon_link}" width="100%" height="auto" decoding="async"
style="width: 32px; height: 32px; border-radius: 4px;">
</div>
<div style="flex-grow: 1;">
<div style="font-weight: bold; margin-bottom: 5px;">
EXTRACTED_PAGE_TITLE
</div>
<div style="color: #666;">
EXTRACTED_PAGE_NAME
</div>
</div>
</div>
</a>
</div>
The currently supported syntax is tracked in this GitHub issue
To add support for some currently unsupported syntax, expand the existing existing main render loop in lib.rs
/// Apply to all chapters
fn handle_chapter(chapter: &mut Chapter) -> Result<(), Error> {
chapter.content = hints::render(&chapter.content)?;
chapter.content = embeds::render(&chapter.content)?;
chapter.content = content_refs::render(&chapter.content)?;
// Add your additional syntax parsing here
Ok(())
}
with a function that calls the corresponding parsing logic.
In your parsing logic, use regex or any other methods to scan for the specific pattern of the syntax you want to support and replace it with the corresponding HTML code.