GemBook Documentation

website·Indexed 31 days ago

https://book.gemjs.org

A tool that renders Markdown content into a website and generates pages based on directory structure. Features include a CLI for previewing and building, support for custom themes, internationalization, document metadata via FrontMatter, and a plugin system with components like <gbp-code-group> and <gbp-raw>.

Tokens
4.8K
Snippets
36
Records
41
Agent score
48%

What's inside GemBook

  1. Initialize and preview documentation with GemBook

    To start a GemBook project, create a documentation directory with a readme.md file. Use the gem-book command followed by the directory path to write and preview your documentation.
    # Create docs directory and initial file
    mkdir docs && echo '# Hello GemBook!' > docs/readme.md
    
    # Write and preview the docs
    gem-book docs
  2. Install and use GemBook plugins

    GemBook uses custom elements as a plugin system to customize Markdown rendering or enhance functionality. Plugins can be imported via the CLI or a script tag. When using a plugin in Markdown, ensure attributes do not contain line breaks to avoid being wrapped in <p> tags.
    # Import built-in plugin
    gem-book docs --plugin raw
    
    # Import local plugin
    gem-book docs --plugin ./my-plugin.ts
    
    # Import remote plugin
    gem-book docs --plugin https://example.com/plugin.js
    <!-- Alternative HTML import -->
    <script type="module" src="https://unpkg.com/gem-book/plugins/raw.js"></script>
    
    <!-- Usage in Markdown -->
    <gbp-raw src="/src/plugins/raw.ts"></gbp-raw>
  3. Initialize and preview a GemBook project

    To get started with GemBook, create a documentation directory and a readme file, then use the gem-book CLI to preview your content. You can optionally specify a project title, a logo image, and enable home-mode to render the readme or index file as the project homepage.
    # Create docs directory and initial content
    mkdir docs && echo '# Hello GemBook!' > docs/readme.md
    
    # Write and preview documentation
    gem-book docs
    
    # Preview with a specific title
    gem-book docs -t MyApp
    
    # Preview with a title and logo
    gem-book docs -t MyApp -i logo.png
    
    # Render readme.md/index.md as the project homepage
    gem-book docs -t MyApp -i logo.png --home-mode
    
    # Build front-end resources for production
    gem-book docs -t MyApp -i logo.png --home-mode --build
  4. Build GemBook front-end resources for deployment

    To generate the static front-end resources (such as index.html and .js files) required for deployment, run the build command. Once the output directory is generated, you can host it using any static site hosting service. Note that because <gem-book> utilizes the History API, GitHub Pages is not supported by default without additional configuration.
    npx gem-book docs --build
  5. Enable internationalization in GemBook

    To support multiple languages on a GemBook documentation site, use the --i18n flag during build/execution. This enables automatic redirection based on browser settings if no language code is present in the URL. The site will serve content based on the language code in the URL path: /{lang}/path.
    gem-book build --i18n
  6. Localize directory names in the sidebar

    By default, GemBook renders the directory name directly in the sidebar. To localize these names for different languages, you can use one of two methods:

    1. Rename the directory to the desired localized name (e.g., rename docs/zh/guide to docs/zh/002-指南).
    2. Add a config.yml file within that specific directory to define localization settings.
  7. Generate a documentation website using GemBook CLI

    GemBook renders Markdown content into a website based on the directory structure. It requires Node.js v18+ to run. You can use the gem-book CLI to preview content, specify project metadata, and build frontend resources.
    # Create a docs directory and a sample file
    mkdir docs && echo '# Hello GemBook!' > docs/readme.md
    
    # Write and preview documentation
    gem-book docs
    
    # Preview with a specific title
    gem-book docs -t MyApp
    
    # Preview with a title and logo
    gem-book docs -t MyApp -i logo.png
    
    # Render readme.md or index.md as the project homepage
    gem-book docs -t MyApp -i logo.png --home-mode
    
    # Build the final front-end resources
    gem-book docs -t MyApp -i logo.png --home-mode --build
  8. Use gem-book in a Monorepo project

    In Monorepo structures where the documentation is not at the project root, use the --base flag to specify the location of the current working directory relative to the project root. This ensures correct path resolution for the repository.
    # Project structure: project/packages/module/docs
    cd packages/module && npx gem-book docs --base packages/module
  9. Manage document sorting in GemBook sidebar

    The <gem-book> sidebar follows specific default sorting rules to remain consistent with IDE and editor views. The default priority is:

    1. readme.md and index.md are always placed first.
    2. Folders are listed before files.
    3. Files are sorted ascending by filename ASCII encoding.

    To implement custom sorting, add a numerical prefix to the filename (e.g., 300-faq.md). Using gaps between numbers (e.g., 100, 200) allows for inserting new documents between existing ones later without renaming multiple files.

    src/docs/
    ├── guide/
    │   ├── README.md
    │   └── installation.md
    ├── 300-faq.md
    ├── 400-about.md
    └── README.md
  10. Develop a GemBook plugin

    To develop a plugin, extend the GemBookPluginElement class. This class provides access to internal methods and the <gem-book> configuration. Use customElements.whenDefined('gem-book') to ensure the base element is available before defining your plugin.
    customElements.whenDefined('gem-book').then(({ GemBookPluginElement }) => {
      customElements.define(
        'gbp-example',
        class extends GemBookPluginElement {
          constructor() {
            super();
            console.log(GemBookPluginElement.config);
          }
        },
      );
    });
  11. Organize documentation for multi-language support

    When internationalization is enabled, documents must be organized into folders named with their respective language codes (e.g., en for English, zh for Chinese). GemBook will then map these folders to the corresponding language versions of the site.
    docs/
    ├── en/
    │   ├── guide/
    │   │   ├── README.md
    │   │   └── installation.md
    │   ├── 003-about.md
    │   └── README.md
    └── zh/
        ├── guide/
        │   ├── README.md
        │   └── installation.md
        └── README.md
  12. Integrate search with <gbp-docsearch>

    The <gbp-docsearch> plugin integrates Algolia DocSearch. It is instantiated once and can be positioned using slots.

    Note: renderJavaScript must be enabled in the Algolia DocSearch Crawler configuration.

    Using docsearch?local enables a local search service powered by MiniSearch.