Newer
Older
const slugify = require("@sindresorhus/slugify")// import slugify from '@sindresorhus/slugify';
const Cache = require("@11ty/eleventy-cache-assets");
const pluginTOC = require("eleventy-plugin-nesting-toc");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const { DateTime } = require("luxon");
const cheerio = require('cheerio');
module.exports = function (eleventyConfig) {
const markdown = require("markdown-it")({
html: true,
breaks: true,
linkify: true,
});
eleventyConfig.addFilter("markdownify", function (rawString) {
return markdown.render(rawString);
});
eleventyConfig.addFilter("markdowninline", function (rawString) {
return markdown.renderInline(rawString);
});
eleventyConfig.addCollection("sortedByOrder", function (collectionApi) {
return collectionApi.getAll()
.filter(item => {
return item.data.menu;
})
.sort((a, b) => {
if (a.data.menu) {
if (a.data.order > b.data.order) return 1;
else if (a.data.order < b.data.order) return -1;
else return 0;
eleventyConfig.addCollection("blog", collectionApi => {
return collectionApi.getFilteredByGlob("src/blog/posts/*.md").sort((a, b) => a.data.date - b.data.date);
});
eleventyConfig.addCollection("articles", collectionApi => {
return collectionApi.getFilteredByGlob("src/articles/*.md").sort((a, b) => a.data.date - b.data.date);
});
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
// eleventyConfig.addFilter("search", searchFilter);
// eleventyConfig.addFilter("searchSingle", searchFilterSingle);
eleventyConfig.addCollection("allSearch", collection => {
return [...collection.getFilteredByTag("chapter")];
});
eleventyConfig.addPassthroughCopy({ "static/css": "/css" });
eleventyConfig.addPassthroughCopy({ "static/fonts": "/fonts" });
eleventyConfig.addPassthroughCopy({ "static/js": "/js" });
eleventyConfig.addPassthroughCopy({ "static/images": "/images" });
eleventyConfig.addPassthroughCopy({ "static/outputs": "/outputs" });
// plugin TOC
eleventyConfig.addPlugin(pluginTOC);
eleventyConfig.setLibrary(
"md",
markdownIt({
html: true,
linkify: true,
typographer: true,
}).use(markdownItAnchor, {})
);
// useful to use the toc somewhere else
eleventyConfig.addFilter("prependLinks", function (value, prepend) {
return value.replace(/<a href="/g, `<a href="${prepend}`)
});
eleventyConfig.addFilter("replaceWithRegex", function (replaceThat, replaceWith) {
let regex = new RegExp(replaceThat);
return value.replace(regex, replaceWith)
});
// add latin number plugin
eleventyConfig.addFilter("romanize", function (value) {
return romanize(value);
});
// \get the date with luxon (for all date)
eleventyConfig.addFilter("postDate", (dateObj) => {
let date = new Date(dateObj)
return DateTime.fromJSDate(date).toLocaleString(DateTime.DATE_MED);
});
// limit the amount of items
eleventyConfig.addFilter("limit", function (arr, limit) {
return arr.slice(0, limit);
});
eleventyConfig.addFilter("filterContent", function (value, el) {
// console.log(value);
const $ = cheerio.load(value);
if ($.html(el)) {
return value = $.html(el);
}
else {
return value;
}
});
eleventyConfig.addPlugin(pluginTOC, {
tags: ["h2", "h3", "h4"], // which heading tags are selected headings must each have an ID attribute
wrapper: "nav", // element to put around the root `ol`/`ul`
wrapperClass: "toc", // class for the element around the root `ol`/`ul`
ul: false, // if to use `ul` instead of `ol`
flat: false,
});
eleventyConfig.addFilter("slugify", function (str) {
return slugify(str, {
lower: true,
replacement: "-",
remove: /[*+~.·,()'"`´%!?¿:@]/g
});
});
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
// folder structures
// -----------------------------------------------------------------------------
// content, data and layouts comes from the src folders
// output goes to public (for gitlab ci/cd)
// -----------------------------------------------------------------------------
return {
dir: {
input: "src",
output: "public",
includes: "layouts",
data: "data",
},
};
};
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function romanize(num) {
// taken from Steven Levithan
// https://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
if (isNaN(num))
return NaN;
var digits = String(+num).split(""),
key = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"],
roman = "",
i = 3;
while (i--)
roman = (key[+digits.pop() + (i * 10)] || "") + roman;
return Array(+digits.join("") + 1).join("M") + roman;
}