#mdbook-preprocessor #preprocessor #markdown #gitbook

bin+lib mdbook-gitbook

mdBook preprocessor to properly render GitBook specific syntax

11 releases (4 stable)

1.0.3 Feb 5, 2024
1.0.2 Feb 3, 2024
0.4.1 Feb 3, 2024
0.3.1 Feb 2, 2024
0.2.3 Feb 1, 2024

#639 in Text processing

Download history 56/week @ 2024-03-29 11/week @ 2024-04-05

100 downloads per month

MPL-2.0 license

35KB
300 lines

mdbook-gitbook

crates.io MPL 2.0 LICENSE docs.rs Build Test

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.

Usage

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]

Development

How it works

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>

Expanding the preprocessor

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.

Dependencies

~18–33MB
~543K SLoC