-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.eleventy.js
More file actions
129 lines (116 loc) · 5.42 KB
/
.eleventy.js
File metadata and controls
129 lines (116 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const markdownIt = require('markdown-it');
const utils = require('./src/utils');
const anchor = require('markdown-it-anchor');
const extractVersionPrefix = slug => {
const prefixMatch = slug.match(new RegExp(/^\d{1,3}(?=-)/));
return prefixMatch ? Number(prefixMatch[0]) : null;
};
const stripVersionPrefix = slug => slug.replace(new RegExp(/^\d{1,3}-/), '')
module.exports = eleventyConfig => {
// custom markdown library with automatic anchors for h2 headings
const markdownLib = markdownIt({
html: true,
typographer: true,
}).use(anchor, {
level: [2],
slugify: utils.readableSlug,
permalink: anchor.permalink.linkInsideHeader({
ariaHidden: true,
class: 'section-anchor',
symbol: '#',
}),
});
eleventyConfig.setLibrary('md', markdownLib);
// additional filters & shortcodes
eleventyConfig.addFilter('stripVersionPrefix', stripVersionPrefix);
eleventyConfig.addFilter('findSections', utils.findSections);
eleventyConfig.addShortcode('fontawesome', utils.fontawesome);
eleventyConfig.addPairedShortcode('alert', (content, level = 'info') => {
return `<small class="sidenote sidenote--${level}">${"\n\n"}${content}${"\n\n"}</small>`;
});
// syntax highlighting
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
eleventyConfig.addPlugin(syntaxHighlight, {
templateFormats: ["md"],
init: ({Prism}) => {},
});
// copy js uncompiled
eleventyConfig.addPassthroughCopy({'site/_js': 'js'});
// headers (for caching)
eleventyConfig.addPassthroughCopy('site/_headers');
// uncompiled assets
eleventyConfig.addPassthroughCopy('site/assets/');
// favicons
eleventyConfig.addPassthroughCopy({'site/favicons/': '/'})
// preview images
eleventyConfig.addPassthroughCopy({'site/preview-images/': '/'})
// build a tree of post sections (folders) and posts inside them
// this assumes that each section number exists only once, same for post
// numbers within sections
eleventyConfig.addCollection('posts', collections => collections.getFilteredByTag('post').filter(post => Boolean(post.data.permalink)));
eleventyConfig.addCollection('postTree', collections => {
return collections.getFilteredByTag('post').reduce((coll, post) => {
// don't include drafts or unpublished pages
if (!post.data.permalink) return coll;
// information on the directory which acts as a content section
const sectionDir = post.filePathStem.split('/')[1];
const sectionName = stripVersionPrefix(sectionDir);
const sectionNumber = extractVersionPrefix(sectionDir);
// we'll build an array of sections, using the section number as the
// index, so the sections are properly ordered
// this assumes there are no two sections sharing one section number
const sectionIndex = sectionNumber - 1;
// initialize the section object if it doesn't exists yet
if (typeof coll[sectionIndex] === 'undefined') {
coll[sectionIndex] = {
number: sectionNumber,
name: sectionName,
dir: sectionDir,
title: post.data.section.title,
posts: [],
}
}
// add the current post to the correct section
const postIndex = extractVersionPrefix(post.fileSlug) - 1;
coll[sectionIndex].posts[postIndex] = post;
return coll;
}, []);
});
// get previous / next post from the above post tree
eleventyConfig.addFilter('postTreePrevious', (page, collection) => {
const posts = collection.map(c => c.posts).flat(1);
const pageIndex = posts.findIndex(p => p.url === page.url);
if (pageIndex === -1) return null;
const nextIndex = pageIndex - 1;
return nextIndex < 0 ? null : posts[nextIndex];
});
eleventyConfig.addFilter('postTreeNext', (page, collection) => {
const posts = collection.map(c => c.posts).flat(1);
const pageIndex = posts.findIndex(p => p.url === page.url);
if (pageIndex === -1) return null;
const nextIndex = pageIndex + 1;
return nextIndex >= posts.length ? null : posts[nextIndex];
});
eleventyConfig.addFilter('postTreePreviousInSection', (page, collection) => {
const currentSection = collection.find(section => section.posts.some(current => current.url === page.url));
const pageIndex = currentSection.posts.findIndex(current => current.url === page.url);
return pageIndex === 0 ? null : currentSection.posts[pageIndex - 1];
});
eleventyConfig.addFilter('postTreeNextInSection', (page, collection) => {
const currentSection = collection.find(section => section.posts.some(current => current.url === page.url));
const pageIndex = currentSection.posts.findIndex(current => current.url === page.url);
return pageIndex + 1 >= currentSection.length ? null : currentSection.posts[pageIndex + 1];
});
return {
templateFormats: ['html', 'md', 'njk', '11ty.js'],
markdownTemplateEngine: 'njk',
pathPrefix: '/',
dir: {
input: "site",
output: "dist",
includes: "_includes",
layouts: "_includes/_layouts",
data: "_data",
}
}
}