Giter Site home page Giter Site logo

kevingimbel / mktoc Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 3.0 1.98 MB

🦀 Markdown Table of Content generator

Home Page: https://kevingimbel.de/oss/mktoc

License: MIT License

Rust 95.84% Makefile 2.13% Shell 2.02%
rust markdown table-of-contents toc generator rust-library command-line command-line-tool cli

mktoc's Introduction

mktoc

Graphic design is my passion ✨

Coverage

Table of Contents

About

⬆️ Back to Top

mktoc parses markdown files and generates a Table Of Content linking all headlines up to heading level 6 deep, or as specified by command line arguments, environment variables, or inline JSON config (see Usage).

Installation

⬆️ Back to Top

mktoc can be installed using Cargo, the Rust package manager.

Cargo

Installation

$ cargo install mktoc

Update

$ cargo install --force mktoc

Binary

Binaries for Linux and MacOS can be downloaded from the release page.

Windows Binaries are not yet build automatically, see #7.

Usage

Update Markdown file

⬆️ Back to Top

Add the following HTML comment into the Markdown file where the Table of Contents should be rendered.

<!-- BEGIN mktoc -->
<!-- END mktoc -->

Everything between those comments will be replaced!

Inline config

⬆️ Back to Top

Starting with version 3.0.0 it's possible to set values in-line which is great when collaborating with teams who may have individual settings.

<!-- BEGIN mktoc {"min_depth": 2, "max_depth": 4, "wrap_in_details": false} -->
<!-- END mktoc -- >

this is equal to running mktoc -m 2 -M 4 or setting these environment variables MKTOC_MIN_DEPTH=2 and MKTOC_MAX_DEPTH=4.

Inline config takes priority over environment or CLI arguments.

min_depth

The minumum depth to look for, defaults to 1 which is equal to <h1> or #.

max_depth

The maximum depth to look for, defaults to 6 which is equal to <h6> or ######.

wrap_in_details

If set to true, the ToC will be wrapped in a <details> element.

Command line

⬆️ Back to Top

Specify --stdout or -s to output generated content to stdout instead of overwriting file. By default the specified file will be overwritten.

$ mktoc -h
Generate Table of Contents from Markdown files

Usage: mktoc [OPTIONS] [FILE]

Arguments:
  [FILE]  [default: README.md]

Options:
  -s, --stdout                 If set will output to stdout instead of replacing content in file
  -m, --min-depth <MIN_DEPTH>  Minimum heading level [env: MKTOC_MIN_DEPTH=] [default: 1]
  -M, --max-depth <MAX_DEPTH>  Maximum heading level [env: MKTOC_MAX_DEPTH=] [default: 6]
  -w, --wrap-in-details        Wrap ToC in details html element [env: MKTOC_WRAP_IN_DETAILS=]
  -h, --help                   Print help
  -V, --version                Print version

$ mktoc -s README.md
$ mktoc -m 2 -M 4 README.md
$ mktoc

If no arguments are given the default or configured (via environment) values are used.

See mktoc --help for list of all arguments and flags.

mktoc

USAGE:
mktoc [FLAGS] [OPTIONS] [file]

FLAGS:
-h, --help       Prints help information
-s, --stdout     If set will output to stdout instead of replacing content in file
-V, --version    Prints version information

OPTIONS:
-M, --max-depth <max-depth>    Maximum heading level [env: MKTOC_MAX_DEPTH=]  [default: 6]
-m, --min-depth <min-depth>    Minimum heading level [env: MKTOC_MIN_DEPTH=2]  [default: 1]

ARGS:
<file>     [default: README.md]

Environment configuration

⬆️ Back to Top

mktoc can be configured with environment variables, namely:

  • MKTOC_MIN_DEPTH equal to -m
  • MKTOC_MAX_DEPTH equal to -M

Place these variables in a shell environment file such as ~/.bashrc or ~/.zshrc, then just run mktoc without -m and -M

# The following in ~/.bashrc|~/.zshrc configures mktoc to render headings from level 2 to
# level 4
# 
# MKTOC_MIN_DEPTH=2
# MKTOC_MAX_DEPTH=4
# MKTOC_WRAP_IN_DETAILS=true

$ mktoc README.md

Auto-run with VSCode

⬆️ Back to Top

For VSCode the Run on save extension can be used to trigger mktoc.

Install the extension and then add the following config to workspace or user settings.json.

"emeraldwalk.runonsave": {
    "commands": [
        {
            "match": "\\.md$",
            "cmd": "mktoc ${file}"
        }
    ]
}

This will run the command for every markdown file on safe. If there is no mktoc comment in the Markdown file nothing happens.

Performance

⬆️ Back to Top

mktoc is fast but can probably be even faster! Pull Requests and bug reports are appreciated!

Contributing

⬆️ Back to Top

We love and welcome every form of contribution.

Where to start?

Here are some good places to start:

Tooling

  • mktoc is used for table of content generation in the README.md (neat!)
  • criterion for benchmarking

Install dev build

Sometimes it's nice to install a specific version of mktoc, this can be done with the following command:

# install specific commit
cargo install --git https://github.com/KevinGimbel/mktoc --force --rev $COMMIT_ID
# install branch
cargo install --git https://github.com/KevinGimbel/mktoc --force --branch $BRANCH_NAME

License

⬆️ Back to Top

MIT, see LICENSE file.

mktoc's People

Contributors

dependabot[bot] avatar kevingimbel avatar matthiasbeyer avatar oylenshpeegul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mktoc's Issues

Extract input manipulation

The code responsible for adding the ToC is inside the make_toc function. This means a third-party user cannot get a full Markdown file generated without specifying an actual file (make_toc takes a file path).

https://github.com/KevinGimbel/mktoc/blob/main/src/lib.rs#L227-L237

Todo

Extract code into function

fn add_toc(original_file_content: String, new_toc: String) -> String

Write tests

  • Adding the ToC is implicitly tested, should be explicitly tested

Feature: wrap ToC in details block

Add an option to wrap the generated ToC in a <details> element.

Example:

Table of Contents

Code:

<!-- BEGIN mktoc -->
<details>
  <summary>Table of Contents</summary>

- [Installation 🤖](#installation)
  - [Build from source](#build-from-source)
    - [Prerequisites](#prerequisites)
    - [Instructions](#instructions)
  - [Binary releases](#binary-releases)
  - [Docker](#docker)
    - [Images](#images)
    - [Usage](#usage)
    - [Building](#building)
</details>
<!-- END mktoc -->

Important:

  • Newline after <summary> is required, otherwise the markdown is not parsed.
  • Should be configurable via ENV, cli argument, and inline config

Strip special characters

Special characters like ( must be removed from the link.

The headline

## MongoDB (Atlas)

must be linked as

- [MongoDB (Atlas)](#mongodb-atlas)

but currently mktoc produces a link like

- [MongoDB (Atlas)](#mongodb-(atlas))

Links in Headlines produce wrong output

The rendered output is wrong

Code:

<!-- BEGIN mktoc -->

  - [Links in [Headlines](/headlines/)](#links-in-headlinesheadlines)
<!-- END mktoc -->

### Links in [Headlines](/headlines/)

Rendered like:

  • [Links in Headlines](#links-in-headlinesheadlines)

Desired Output:

<!-- BEGIN mktoc -->

  - [Links in Headlines](#links-in-headlines)
<!-- END mktoc -->

### Links in [Headlines](/headlines/)

Link inside a Headline must be stripped out.

Parsing issues

Since we're using a "simple" Regex based parser to find headlines we also get things that are not a headline, e.g. comment blocks inside of code snippets:

# my comment
pub fn is_issue() -> bool {
  true
}

Here # my comment would be added to the Table of Contents. So far I cannot think of a way to fix this without using a markdown parser to process the markdown before getting the headlines.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.