Skip to content

sbstjn/predown

Repository files navigation

predown

Current Release MIT License CircleCI Build Status

Preprocess Markdown files using Go templates and TOML configuration.

Usage

# Show generated Markdown content in stdout

$ > predown template.md
$ > predown template.md --data data.toml
$ > predown template.md --data data.toml --wrap wrapper.frontmatter

# Pipe generated Markdown to file

$ > predown template.md > output.md

# Save generated Markdown in file

$ > predown template.md output.md
$ > predown template.md output.md --data data.toml
$ > predown template.md output.md --data data.toml --wrap wrapper.frontmatter

# Transform generated Markdown to HTML

$ > predown template.md output.html --data data.toml

Example

Let's assume you have example.md, data.toml, and wrapper.frontmatter files like these:

example.md
# Headline

Variable Example from `data.toml` is **{{ .Example }}**.

## Table

| Name | Age |
| ---- | --- |
{{- range .People }}
| {{ .Name }} | {{ .Age }} |
{{- end }}

Nice, isn't it? ;)
data.toml
Example = "Foo"

[[People]]

Name = "Frank"
Age = 21

[[People]]

Name = "Claude"
Age = 45

[[People]]

Name = "Natascha"
Age = 37
wrapper.frontmatter
---
Example: {{ .Data.Example }}
---

{{ .Content }}

Command

Using predown, you can merge the data from data.toml with example.md, and wrap everything in wrapper.frontmatter afterwords.

$ > predown template.md output.md \
    --data data.toml \
    --wrap wrapper.frontmatter

This will create a file called output.md with the following content:

---
Example: Foo
---

Headline
========

Variable Example from `data.toml` is **Foo**.

Table
-----

| Name     | Age |
|----------|-----|
| Frank    | 21  |
| Claude   | 45  |
| Natascha | 37  |

Nice, isn't it? ;)

As you can see, the Markdown file will be tidied up. This can be very handy if you work with tables in your Markdown files. After running predown, they nicely align …

Install

Bash

To download the latest binary for your system (linux or darwin), just use the following curl request:

$ > curl -sSLfo predown \
    ` \
      curl -sSLf \
      https://api.github.com/repos/sbstjn/predown/releases/latest \
      | grep browser_download_url \
      | cut -d '"' -f 4 \
      | grep \`uname -s | tr A-Z a-z\` \
    `
$ > chmod +x predown
$ > ./predown --version

Makefile

To add predown to your existing setup, you can update your Makefile to download the binary file.

PREDOWN= ./predown

$(PREDOWN):
  @ curl -sSLfo $(PREDOWN) \
    ` \
      curl -sSLf https://api.github.com/repos/sbstjn/predown/releases/latest \
      | grep browser_download_url \
      | cut -d '"' -f 4 \
      | grep $(shell uname -s | tr A-Z a-z) \
    `
    @ chmod +x $(PREDOWN)
    @ $(PREDOWN) --version

License

Feel free to use the code, it's released using the MIT license.

Contribution

You are welcome to contribute to this project! 😘

To make sure you have a pleasant experience, please read the code of conduct. It outlines core values and beliefs and will make working together a happier experience.