-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
149 lines (122 loc) · 4.63 KB
/
.eleventy.js
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
const { DateTime } = require("luxon");
const markdownItAttrs = require('markdown-it-attrs');
const markdownItFootnote = require('markdown-it-footnote');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const installPrismLanguages = require('./prism-languages.js');
const pluginRss = require("@11ty/eleventy-plugin-rss");
const fs = require("fs");
const path = require("path");
const pluginTOC = require('eleventy-plugin-nesting-toc');
const pluginNavigation = require("@11ty/eleventy-navigation");
const codepenNjk = require('./codepen')
const metagen = require('eleventy-plugin-metagen');
const Path = require('path')
const manifestPath = path.resolve(__dirname, "dist", "assets", "manifest.json");
const manifest = JSON.parse(
fs.readFileSync(manifestPath, { encoding: "utf8" })
);
const getSimilarCategories = function(categoriesA, categoriesB) {
return categoriesA.filter(Set.prototype.has, new Set(categoriesB)).length;
}
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
init: function({ Prism }) {
installPrismLanguages(Prism)
},
});
eleventyConfig.addPlugin(metagen);
eleventyConfig.addPlugin(pluginNavigation);
eleventyConfig.addPlugin(pluginTOC, {
tags: ['h2', 'h3']
});
eleventyConfig.addShortcode("myCustomImage", function (url, alt) {
return `<div class="cimage fw pt-8 pb-8"><img class="shadow" src="${eleventyConfig.getFilter("url")(url)}" alt="${alt}"></div>`;
});
eleventyConfig.addNunjucksShortcode("codepen", codepenNjk);
eleventyConfig.addLayoutAlias("post", "layouts/post.njk");
eleventyConfig.addLayoutAlias("default", "layouts/default.njk");
eleventyConfig.setDataDeepMerge(true);
// Adds a universal shortcode to return the URL to a webpack asset. In Nunjack templates:
// {% webpackAsset 'main.js' %} or {% webpackAsset 'main.css' %}
eleventyConfig.addShortcode("webpackAsset", function(name) {
if (!manifest[name]) {
throw new Error(`The asset ${name} does not exist in ${manifestPath}`);
}
return manifest[name];
});
eleventyConfig.addFilter("titlef", url => {
return url !== '/' ? ' | Kevin Regenrek' : ''
});
eleventyConfig.addFilter("absolute", image => {
if (!image) {
return false
}
const prefixedUrl = 'https://regenrek.com/assets/images/'+ image
return prefixedUrl + '.png';
})
// Date formatting (human readable)
eleventyConfig.addFilter("readableDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy");
});
// Date formatting (machine readable)
eleventyConfig.addFilter("machineDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("yyyy-MM-dd");
});
// only content in the `posts/` directory
eleventyConfig.addCollection("posts", function(collection) {
return collection.getAllSorted().filter(function(item) {
return item.inputPath.match(/^\.\/posts\//) !== null;
});
});
eleventyConfig.addPlugin(pluginRss);
/* Markdown Plugins */
let markdownIt = require("markdown-it");
// add anchor links to heading elements like <h1>,<h2>, ...
let markdownItAnchor = require("markdown-it-anchor");
let options = {
html: true,
breaks: true,
linkify: true // apply auto links
};
let opts = {
permalink: false
};
eleventyConfig.setLibrary("md", markdownIt(options)
.use(markdownItFootnote)
.use(markdownItAttrs)
.use(markdownItAnchor, opts)
);
// Copy all images directly to dist.
eleventyConfig.addPassthroughCopy({ "assets/images": "assets/images" });
// Copy external dependencies to dist.
eleventyConfig.addPassthroughCopy({ "assets/vendor": "assets/vendor" });
// Reload the page every time the JS/CSS are changed.
eleventyConfig.setBrowserSyncConfig({ files: [manifestPath] });
// A debug utility.
eleventyConfig.addFilter("log", obj => {
console.log(obj)
});
eleventyConfig.addFilter("similarPosts", function(collection, path, tags){
return collection.filter((post) => {
console.log("TAGS", post.data.tags)
return getSimilarCategories(post.data.tags, tags) >= 1 && post.data.page.inputPath !== path;
})
// .sort((a,b) => {
// return getSimilarCategories(b.data.categories, categories) - getSimilarCategories(a.data.categories, categories);
// });
});
eleventyConfig.addFilter('limit', function(arr, limit) {
return arr.slice(0, limit);
});
return {
dir: {
input: ".",
includes: "_includes", // relative to dir.input
data: "_data",
output: "dist",
},
htmlTemplateEngine: "njk",
markdownTemplateEngine: "njk",
passthroughFileCopy: true,
};
};