Skip to content

Instantly share code, notes, and snippets.

@caseywatts
Created October 1, 2026 16:24
Show Gist options
  • Select an option

  • Save caseywatts/3dd175c1dfe217116aba7b4571dba6ae to your computer and use it in GitHub Desktop.

Select an option

Save caseywatts/3dd175c1dfe217116aba7b4571dba6ae to your computer and use it in GitHub Desktop.
Eleventy Custom Markdown Parser

Using Eleventy for USWDS projects

This eleventy config does several things to make it easier to work with USWDS:

  • Exposes a filter asMarkdown that can be used in hard-coded (non-markdown) HTML files. This uses the same markdown parser that Markdown uses, to prevent issues with having multiple instances or libraries.
  • External links get formatted as usa-link--external, and open in a new tab
  • Lists are formatted with usa-list
  • The typographer setting is enabled, giving us features like smart quotes.
export default function (eleventyConfig) {
// ...
// other eleventyConfig configuration might be here
// ...
let mdLib;
eleventyConfig.amendLibrary("md", (lib) => {
mdLib = lib;
mdLib.set({ html: true, typographer: true });
mdLib.renderer.rules.bullet_list_open = () => `<ul class="usa-list">`;
mdLib.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const token = tokens[idx];
const href = token.attrGet("href");
if (isExternalLink(href)) {
token.attrSet("target", "_blank");
token.attrSet("rel", "noreferrer");
token.attrSet("class", "usa-link usa-link--external");
} else {
token.attrSet("class", "usa-link");
}
return self.renderToken(tokens, idx, options);
};
});
eleventyConfig.addFilter("asMarkdown", (content, renderInline = false) => {
if (!mdLib) {
throw new Error("mdLib is undefined");
}
return renderInline ? mdLib.renderInline(content) : mdLib.render(content);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment