-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path.eleventy.js
More file actions
235 lines (183 loc) · 9.4 KB
/
.eleventy.js
File metadata and controls
235 lines (183 loc) · 9.4 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const fs = require('fs');
const yaml = require("js-yaml");
const util = require("util");
const clip = require("text-clipper").default;
const { DateTime } = require("luxon");
const filters = require("./src/scripts/filters");
const customPagination = require("./src/scripts/custom-pagination");
const templateFormats = [
"html",
"njk",
"ejs",
"md"
];
//
// Custom plugins / filters / ...
// local assets
const localPostImagesPlugin = require("./src/plugins/eleventy-hugo-style-local-post-images");
//
// NuGet plugins / filters / ...
// Syntax-highlight for MD code-blocks
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
// TOC: the main (eleventy-plugin-nesting-toc) plugin generates TOC, the markdown-it-anchor plugin adds ID's to headings...
const pluginTOC = require('eleventy-plugin-nesting-toc');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItFootnote = require("markdown-it-footnote");
const markdownItToc = require("markdown-it-table-of-contents");
const markdownItEmoji = require('markdown-it-emoji');
const markdownItSup = require('markdown-it-sup');
const markdownItSub = require('markdown-it-sub');
const markdownItSmartArrows = require('markdown-it-smartarrows');
const markdownItTaskLists = require('markdown-it-task-lists');
// RSS
const pluginRss = require("@11ty/eleventy-plugin-rss");
// Reading time
const pluginReadingTime = require('eleventy-plugin-reading-time');
module.exports = function (eleventyConfig) {
const env = (process.env.ELEVENTY_ENV || "").trim();
const isProd = env.toUpperCase() == "PROD";
console.log("Environment: ", env);
console.log("Is Prod : ", isProd);
const globals = yaml.load(fs.readFileSync("src/data/globals.yaml"));
console.log("Loaded 'globals' from yaml", globals);
//
// Default config
eleventyConfig.setUseGitIgnore(false);
// NOTE:
// unlike in hugo, not the content of the assets folder, but the asset folder itself (with it's subfolders) will be copied...
// SCSS is also stored in assets, but we're interested just in the final CSS build (so ignoring SCSS)...
eleventyConfig.addPassthroughCopy("src/assets/**/!(*.scss)");
eleventyConfig.addLayoutAlias('default', 'page.njk')
eleventyConfig.setFrontMatterParsingOptions({ excerpt: true, excerpt_separator: '---' });
// NOTE: need to set config here to be accessible in the addTransforms via: eleventyConfig.templateFormats
// eleventyConfig.setTemplateFormats(["html", "njk", "ejs", "md"]);
// add YAML support
eleventyConfig.addDataExtension("yaml", contents => yaml.load(contents));
// add Markdown filter
eleventyConfig.addFilter("md", content => markdownIt({ html: true }).render(content ?? ""));
//
// Debugging
eleventyConfig.addFilter("json", function (value) { return JSON.stringify(value); });
eleventyConfig.addFilter("inspect", function (value) { return util.inspect(value); });
//
// Custom plugins / filters / ...
eleventyConfig.addFilter('mergePaths', function(value) { return filters.mergePaths(value); });
eleventyConfig.addFilter('normalize', function(value) { return filters.normalize(value); });
eleventyConfig.addFilter('truthy', function(value) { return !!value; });
eleventyConfig.addFilter('falsy', function(value) { return !value; });
eleventyConfig.addFilter('truncateHtml', function(value, charLimit, lineLimit) { return clip(value, charLimit, { html: true, maxLines: lineLimit }); });
// NOTE: 2019-08-11T00:34:00+0800 dates with offset are not accepted via 11ty v0.12.1
eleventyConfig.addFilter("readableDate", dateObj => { return dateObj ? DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat("LLL dd, yyyy") : 'N/A'; });
eleventyConfig.addFilter('firstPage', function(value, doPaging) { return customPagination.firstPage(value, doPaging); });
eleventyConfig.addFilter('getSeriesNavigationDetails', (...args) => require('./src/scripts/post-series.filter').getSeriesNavigationDetails(...args));
eleventyConfig.addFilter('getRelatedNavigationDetails', (...args) => require('./src/scripts/post-related.filter').getRelatedNavigationDetails(...args));
// using custom solution as the eleventy-plugin-page-assets has bugs, is too complex (uses even html parsing), ...
eleventyConfig.addPlugin(localPostImagesPlugin, { eleventyConfig: eleventyConfig, templateFormats: templateFormats, excludes: ["src/plugins/**"] });
//
// NuGet plugins / filters / ...
// syntax-highlight for MD code-blocks: https://prismjs.com/#supported-languages (also see Prism.languages.extend...)
eleventyConfig.addPlugin(syntaxHighlight, {
init: function({ Prism }) {
// Not working as expected, see: https://github.com/11ty/eleventy-plugin-syntaxhighlight/issues/47
Prism.languages.mermaid = Prism.languages.extend('markup', {});
Prism.languages.mathjax = Prism.languages.extend('markup', {});
}
});
// add support for ```mermaid and ```mathjax higlighting inside MD
//
// don't forget to allow them via setting the corresponding parameter inside the front matter:
// mermaid: true
// mathjax: true
var markdownHighlighter = eleventyConfig.markdownHighlighter;
eleventyConfig.addMarkdownHighlighter((str, language) => {
if (language === "mermaid")
return `<pre class="mermaid">${str}</pre>`;
else if (language === "mathjax")
// TODO: pre and code tags generated making problems
return `<pre class="mathjax">$$ ${str} $$</pre>`;
else
return markdownHighlighter(str, language);
});
// TOC generator with required auto ID anchors creation
eleventyConfig.addPlugin(pluginTOC);
// Example Markdown configuration (to add IDs to the headers)
eleventyConfig.setLibrary("md",
markdownIt({
html: true,
linkify: true,
typographer: true,
})
.use(markdownItAnchor, {})
.use(markdownItToc, {})
.use(markdownItFootnote, {})
.use(markdownItSup, {})
.use(markdownItSub, {})
.use(markdownItEmoji, {})
.use(markdownItSmartArrows, {})
.use(markdownItTaskLists, {})
);
// RSS
eleventyConfig.addPlugin(pluginRss, {
posthtmlRenderOptions: {
closingSingleTag: "default" // opt-out of <img/>-style XHTML single tags
}
});
// Reading time
eleventyConfig.addPlugin(pluginReadingTime);
//
// Collections (posts)
const postCollectionName = globals.posts.collectionName;
const addSamples = globals.posts.includeSamplesOnProd || !isProd;
const postCollectionGlob = [
"src/posts/**/*.md",
...(addSamples ? ["src/post-samples/**/*.md"] : [])
];
console.log("Adding samples : ", addSamples);
console.log("Loading posts from: ", postCollectionGlob);
eleventyConfig.addCollection(postCollectionName, require('./src/scripts/collections/posts').filterPagesByGlob(postCollectionGlob));
const pagedPostCollectionName = 'pagedPostCollection';
eleventyConfig.addCollection(pagedPostCollectionName, require('./src/scripts/collections/paged-posts').forCollection(postCollectionName));
const tagCollectionName = 'tagCollection';
eleventyConfig.addCollection(tagCollectionName, require('./src/scripts/collections/tags').forCollection(postCollectionName));
const pagedPostsByTagCollectionName = 'pagedPostsByTagCollection';
eleventyConfig.addCollection(pagedPostsByTagCollectionName, require('./src/scripts/collections/paged-posts-by-tag').for(tagCollectionName, postCollectionName));
const categoryCollectionName = 'categoryCollection';
eleventyConfig.addCollection(categoryCollectionName, require('./src/scripts/collections/categories').forCollection(postCollectionName));
const pagedPostsByCategoryCollectionName = 'pagedPostsByCategoryCollection';
eleventyConfig.addCollection(pagedPostsByCategoryCollectionName, require('./src/scripts/collections/paged-posts-by-category').for(categoryCollectionName));
const seriesCollectionName = 'seriesCollection';
eleventyConfig.addCollection(seriesCollectionName, require('./src/scripts/collections/series').forCollection(postCollectionName));
return {
passthroughFileCopy: true,
// NOTE:
// IMPORTANT - @deprecated https://www.11ty.dev/docs/data-preprocessing/ in future versions
// see workaround: src\www\_blog-related\_blog-related.11tydata.js
// affected files:
// - src\www\www.11tydata.json
// - src\posts\posts.11tydata.json
dataTemplateEngine: 'njk',
htmlTemplateEngine: "njk",
markdownTemplateEngine: "njk",
templateFormats: templateFormats,
dir: {
input: "src",
output: "_site",
includes: "includes",
layouts: "includes/layouts",
data: "data",
},
//
// NOTE:
//
// DEV:
// it's always a good idea to test custom scripts with sub-folder support!
//
// HOSTING:
//
// when hosting under github with no custom domain and inside a sub-folder, then use the repo name as the pathPrefix ()
// same can be achieved with http/head/base (tested localy with partial path ending with /)
//
pathPrefix: "/jamstack-eleventy-custom"
};
}