Skip to navigation Skip to main content
Eleventy
Eleventy Documentation
Stable
3.0.0
Toggle Menu
Eleventy 5.81s
Remix 40.14s

Custom Front Matter Options

Contents

Eleventy uses the gray-matter npm package for parsing front matter. gray-matter allows additional options that aren’t available by default in Eleventy.

Check out the full list of available gray-matter options. By default, Eleventy uses gray-matter’s default options.

eleventy.config.js
export default function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
/* … */
});
};
module.exports = function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
/* … */
});
};

Example: using TOML for front matter parsing

While Eleventy does include support for JSON, YAML, and JS front matter out of the box, you may want to add additional formats too.

eleventy.config.js
// Don’t forget to `npm install @iarna/toml`
import toml from "@iarna/toml";

export default function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
engines: {
toml: toml.parse.bind(toml),
},
});
};
// Don’t forget to `npm install @iarna/toml`
const toml = require("@iarna/toml");

module.exports = function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
engines: {
toml: toml.parse.bind(toml),
},
});
};

For more information, read this example on the gray-matter documentation.

Now you can use TOML in your front matter like this:

Filename sample.md
---toml
title = "My page title using TOML"
---


<!doctype html>
<html>

Example: use JavaScript in your front matter

This section has moved to the Frontmatter Documentation.

Example: Parse excerpts from content

eleventy.config.js
export default function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
// Optional, default is "---"
excerpt_separator: "<!-- excerpt -->",
});
};
module.exports = function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
// Optional, default is "---"
excerpt_separator: "<!-- excerpt -->",
});
};

Now you can do things like this:

Filename sample.md
---
title: My page title
---


This is the start of my content and this will be shown as the excerpt.

<!-- excerpt -->

This is a continuation of my content…

Your template’s content will include the excerpt but remove the separator:

This is the start of my content and this will be shown as the excerpt.
This is a continuation of my content…

page.excerpt now holds This is the start of my content and this will be shown as the excerpt.

INFO:
Don’t want your excerpt included with your content? The unique feature of this configuration is that you can keep your excerpt right at the beginning of your content. You can add a delimiter where you want the excerpt to end and the rest of the content to begin. If you want the excerpt to be separate from the content, make a new key for this and store it separately in your front matter or a data file.

Changing where your excerpt is stored

If you don’t want to use page.excerpt to store your excerpt value, then use your own excerpt_alias option (any valid path to Lodash Set will work) like so:

eleventy.config.js
export default function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
// Eleventy custom option
// The variable where the excerpt will be stored.
excerpt_alias: "my_custom_excerpt",
});
};
module.exports = function (eleventyConfig) {
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
// Eleventy custom option
// The variable where the excerpt will be stored.
excerpt_alias: "my_custom_excerpt",
});
};

Using excerpt_alias: 'my_custom_excerpt' means that the excerpt will be available in your templates as the my_custom_excerpt variable instead of page.excerpt.