-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
130 lines (110 loc) · 3.46 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
const { DateTime } = require("luxon");
const pluginTOC = require("eleventy-plugin-toc");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItHighlightJS = require("markdown-it-highlightjs");
const readingTime = require("eleventy-plugin-reading-time");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const fs = require("fs");
const path = require("path");
const isDev = process.env.APP_ENV === "development";
const mdOptions = {
html: true,
//xhtmlOut: true,
breaks: true,
linkify: true,
typographer: true,
};
const mdAnchorOpts = {
permalink: true,
permalinkBefore: true,
permalinkSymbol: "",
permalinkClass: "anchor-link",
level: [1],
};
const manifestPath = path.resolve(__dirname, "docs", "assets", "manifest.json");
const manifest = isDev
? {
"main.js": "/assets/main.js",
"main.css": "/assets/main.css",
}
: JSON.parse(fs.readFileSync(manifestPath, { encoding: "utf8" }));
module.exports = function (eleventyConfig) {
eleventyConfig.setLibrary(
"md",
markdownIt(mdOptions)
.use(markdownItAnchor, mdAnchorOpts)
.use(markdownItHighlightJS)
);
eleventyConfig.addPlugin(pluginTOC, { tags: ["h1"] });
eleventyConfig.addPlugin(readingTime);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addPassthroughCopy({ "src/images": "images" });
eleventyConfig.setBrowserSyncConfig({ files: [manifestPath] });
eleventyConfig.addShortcode("bundledcss", function () {
return manifest["main.css"]
? `<link href="${manifest["main.css"]}" rel="stylesheet" />`
: "";
});
eleventyConfig.addShortcode("bundledjs", function () {
return manifest["main.js"]
? `<script src="${manifest["main.js"]}"></script>`
: "";
});
eleventyConfig.addFilter("excerpt", (post) => {
const content = post.replace(/(<([^>]+)>)/gi, "");
return content.substr(0, content.lastIndexOf(" ", 200)) + "...";
});
eleventyConfig.addFilter("readableDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat(
"dd LLL yyyy"
);
});
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat("yyyy-LL-dd");
});
eleventyConfig.addFilter("head", (array, n) => {
if (n < 0) {
return array.slice(n);
}
return array.slice(0, n);
});
const generalTags = ["all", "nav", "post", "posts", "post_cn"];
eleventyConfig.addCollection("tagList", function (collection) {
let tagSet = new Set();
collection.getAll().forEach(function (item) {
if ("tags" in item.data) {
let tags = item.data.tags;
tags = tags.filter((item) => !generalTags.includes(item));
for (const tag of tags) {
tagSet.add(tag);
}
}
});
return [...tagSet];
});
eleventyConfig.addFilter("pageTags", (tags) => {
return tags
.toString()
.split(",")
.filter((tag) => {
return !generalTags.includes(tag);
});
});
return {
dir: {
input: "src",
output: "docs",
includes: "includes",
data: "data",
layouts: "layouts",
passthroughFileCopy: true,
templateFormats: ["html", "njk", "md"],
htmlTemplateEngine: "njk",
markdownTemplateEngine: "njk",
},
};
};