markdown-styles
Converts Markdown files to HTML, with over a dozen builtin themes.
Looking for something to generate a blog from Markdown files? Check out ghost-render.
Looking to automatically render your Markdown site on git push? Check out markdown-styles-lambda
Features
v3.2.0 drops support for Node versions older than v10.12.0, removes mkdirp in favor of the native recursive mkdir and upgrades all dependencies to recent versions.
v3.1.10 fixes an issue with single file inputs, thanks @josimard!
v3.1.9 adds support for Chinese characters in heading link ids, thanks @weihong1028! Also includes a fix by @csandor for metadata with Windows line endings.
v3.1.8 fixes an issue with links that consist of internal anchors, thanks @a7madgamal & @nazrhyn!
v3.1.7 fixes an issue with paths on Windows and a default font protocol in mixu-page, thanks @hakkanicko and wirmar!
v3.1.6 fixes a compatibility issue with Node 6, thanks @maximumstock!
v3.1.5 updates some dependencies to add YAML syntax highlighting, thanks @omnibs!
v3.1.4 added linkification for relative links to markdown files, e.g. [link](./foo.md) -> <a href="./foo.html">link</a>.
v3.1.3 added a few additional properties to the programmatic API.
v3.1.2 added default classes that allow you to style headings in the table of contents. See the changelog for changes made in older versions.
- Includes 15+ ready-made CSS stylesheets for Markdown, see the bottom of the readme for screenshots.
- Reuse the stylesheets or use the
generate-md tool to convert a folder of Markdown files to HTML using one of the built-in layouts or a custom layout.
- Completely static output is easy to host anywhere.
- Metadata support: Each file can include additional metadata in a header section, such as the page title and author name which can then be used in the layout.
Layout features
- Built in support for code syntax highlighting via highlight.js; all layouts include a Github-style code highlighting theme by default.
- Built in table of contents generation from Markdown headings, fully customizable by replacing the
{{> toc}} partial in custom layout.
- Built in header id and anchor generation for headings written in Markdown; all layouts support revealing the URL via header hover links.
- Support for custom logic for rendering code blocks via
--highlight-*; this can be used to implement custom blocks that render the content of the code block in some interesting way. For example, I used this in my CSS book to implement hidden spoiler texts.
- Automatically detects the document title from the first heading in the Markdown markup.
Features for creating your own layout
- To make it easier to get started, you can export an existing layout using
--exports and use that as a starting point for your layouts.
- Create your own layout based on an existing layout via
--layout with:
- Full Handlebars support for layouts, helpers and partials
- Fully customizable table of contents template via the
toc partial
- Support for relative path generation via the
{{asset 'path'}} helper
- API support:
markdown-styles now has a public API
For changes, see the changelog.
Quickstart
Install generate-md via npm (to get npm, just install Node.js):
sudo npm install -g markdown-styles
Create a markdown file and then convert it to html:
mkdir input/
echo "# Hello world\n YOLO" > input/index.md
generate-md --layout github --input ./input --output ./output
google-chrome ./output/index.html
Try out different layouts by changing the --layout parameter; screenshots are at the bottom of this page.

generate-md CLI options
--input <path> specifies the input directory (default: ./input/).
--output <path> specifies the output directory (default: ./output/).
--layout <path> specifies the layout. It can be:
- The name of a builtin layout, such as
github or mixu-page.
- A path to a layout folder (full path, or a path relative to
process.cwd).
- A layout folder consists of:
./page.html, the template to use in the layout
./assets, the assets folder to copy over to the output
./partials, the partials directory
./helpers, the helpers directory
--export <name>: Exports a built-in layout to a directory. Use --output <path> to specify the location to write the built-in layout. For example, --export github --output ./custom-layout will copy the github builtin layout to ./custom-layout.
--highlight-<language> <module>: Specifies a custom highlighter module to use for a specific language. For example, --highlight-csv mds-csv will highlight any csv code blocks using the mds-csv module.
--no-header-links: If this flag is passed, the HTML for header links will not be generated. The hover links are enabled by default.
The resulting output
The output HTML is fully static and uses relative paths to the asset files, which are also copied into the output folder. This means that you could, for example, point a HTTP server at the output folder and be done with it or push the output folder to Amazon S3.
For example, here is how I deploy one of my books: aws s3 sync ./output/ s3://some-s3-bucket/some-folder/ --delete --exclude "node_modules/*" --exclude ".git" (assuming credentials are in the necessary environment variables and that the AWS CLI is installed).
Syntax highlighting
v2.0 has syntax highlighting enabled by default. Every layout has also been updated to include a default highlight.js syntax highlighting theme, which means everything works out of the box. For more highlighter themes, check out this demo site - you can find the highlight.js CSS styles here.
To enable language-specific syntax highlighting, you need to specify the language of the code block, e.g.:
```js
var foo = 'bar';
```
v2.0 also supports additional language specific syntax highlighters - check out mds-csv for an example of a syntax highlighter for a specific language.
To enable additional language-specific syntax highlighters, install the module (e.g. mds-csv), then add --highlight-{languagename} {modulename} to the command line. For example, generate-md --highlight-csv mds-csv ... to enable the CSV highlighter for csv code blocks.
Table of contents
The following built in layouts include the {{~> toc}} partial:
- mixu-book
- mixu-bootstrap-2col
- mixu-gray
- mixu-radar
These are mostly templates that have a sensible place to put this table of contents, such as a sidebar. I didn't want to default to putting a table of contents into the layouts that had no sidebar, but you can add it quite easily.
The {{~> toc}} partial generates a table of contents list. The list contains links to every header in your Markdown file. In addition, every Markdown header is automatically converted to a linkable anchor (e.g. #table_of_contents) when the page is generated.
You can customize the table of contents markup by overriding the ./partials/toc.hbs partial in your custom layout. By default, it looks like this:
<ul class="nav nav-list">
{{#each headings}}
<li class="sidebar-header-{{depth}}"><a href="#{{id}}">{{text}}</a></li>
{{/each}}
</ul>
Note that by default (since v3.1.2), each heading list item has a class that depends on the level of the heading (.sidebar-header-1, .sidebar-header-2, ...). Thanks @mixinmax!
The headings metadata is an array of objects with:
- an
id field (the HTML anchor id),
- a
text field (the heading text) and
- a
depth field (the depth of the heading, e.g. the number of # characters in the heading).
Header hover links (v2.1)
If you are reading this on Github, hover over the header above. You'll see a link appear on the side of the header. The same feature is supported by all of the layouts. The feature is implemented purely with CSS, and you can find the details in pilcrow.css in each layout's assets folder. To disable the feature, pass the --no-header-links flag.
v2.4 added support for having unique links for duplicated header names (e.g. using the same header text multiple times in the same file). The header id for the first occurrence stays the same as earlier (#header-text), but the second and subsequent headers get a counter appended (e.g. #header-text-1, #header-text-2). Thanks @xcv58!
Metadata sections
Each markdown file can have metadata associated with it. To set the metadata, start your markdown file with a metadata block that looks like this:
title: Page title
---
# Hello world
YOLO
There must be at least three - characters that separate the header from the rest of the content (on a single line).
You can reference the metadata values in your template by name. The default layouts only make use of the {{title}} metadata value, but your custom layouts can refer to any additional fields you want.
{{title}} is used as the page title. If you do not set the value explicitly, it is automatically detected from the first heading in the markdown file.
The metadata can also be written using JSON syntax or YAML syntax. This makes it possible to add arrays and hashes in the metadata. Using handlebars.js you can go even further. For example, you can add a tags array into the metadata section:
title: Page title
tags: ["handlebars", "template"]
---
# Hello world
... which can then be iterated over using the standard Handlebars {{#each}} iterator:
<ul>
{{#each tags}}
<li>{{ this }}</li>
{{/each}}
</ul>
which will result in:
<ul>
<li>handlebars</li>
<li>template</li>
</ul>
If you take a look at the {{~> toc}} built in partial, you can see that it is actually iterating over a metadata field called headings using the same syntax.
Writing your own layout
v2.0 makes it easier to get started with a custom layout via --export, which exports a built in layout as a starting point. Just pick a reasonable built in layout and start customizing. For example:
generate-md --export github --output ./my-layout
will export the github layout to ./my-layout. To make use of your new layout:
generate-md --layout ./my-layout --input ./some-input --output ./output
If you look under ./my-layout, you'll see that a layout folder consists of:
./page.html, the template to use in the layout
./assets, the assets folder to copy over to the output
./partials, the partials directory
./helpers, the helpers directory
See the next few sections for more details for how these features work.
Template Evaluation (page.html)
The handlebars.js template language is used to evaluate both the template and the markdown.
Here is a list of all the built in features:
{{~> content}}: renders the markdown content
- `{{ass