Skip to content

Commit 8e0b7b7

Browse files
committed
TOC For blog posts
1 parent 90b94b2 commit 8e0b7b7

8 files changed

Lines changed: 633 additions & 11 deletions

File tree

eleventy.config.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
// eleventy.config.js
22
const { DateTime } = require("luxon"); // Import Luxon library for dates
33
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
4+
const pluginTOC = require('eleventy-plugin-toc');
5+
const markdownIt = require('markdown-it');
6+
const markdownItAnchor = require('markdown-it-anchor');
47

58
module.exports = function(eleventyConfig) {
69

710
eleventyConfig.addPlugin(syntaxHighlight);
11+
12+
// --- TOC & Anchor Plugin Configuration ---
13+
eleventyConfig.setLibrary(
14+
'md',
15+
markdownIt({
16+
html: true,
17+
linkify: true,
18+
typographer: true,
19+
}).use(markdownItAnchor, {
20+
slugify: s => s.trim().toLowerCase().replace(/[\s+,.']/g, '-').replace(/[()]/g, ''), // Ensure consistent slugification
21+
permalink: markdownItAnchor.permalink.ariaHidden({
22+
placement: 'before', // Or 'after'
23+
class: 'heading-anchor-link', // Optional: A class for styling the anchor link
24+
symbol: '#', // Optional: The symbol for the anchor link
25+
level: [2,3,4] // Optional: Only add permalinks to h2, h3, h4
26+
})
27+
})
28+
);
29+
30+
eleventyConfig.addPlugin(pluginTOC, {
31+
tags: ['h2', 'h3'], // Which heading tags to include in the TOC, h4 can be added if needed
32+
wrapper: 'nav', // What element to wrap the TOC in, e.g., 'nav'
33+
wrapperClass: 'toc', // Class for the wrapper element
34+
ul: true, // Whether to use an unordered list (ul) or ordered list (ol)
35+
flat: false // Whether to generate a flat list or nested list
36+
});
37+
38+
839
// --- Passthrough Copy ---
940
// (Keep your existing passthrough copy lines)
1041
eleventyConfig.addPassthroughCopy("src/css");
@@ -52,4 +83,4 @@ module.exports = function(eleventyConfig) {
5283
markdownTemplateEngine: "njk",
5384
htmlTemplateEngine: "njk"
5485
};
55-
};
86+
};

0 commit comments

Comments
 (0)