-
Notifications
You must be signed in to change notification settings - Fork 129
/
.eleventy.js
142 lines (123 loc) · 4.18 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
require("dotenv").config();
// const pluginSass = require("eleventy-plugin-sass");
const sass = require("sass");
const fs = require("fs-extra");
const pluginSEO = require("eleventy-plugin-seo");
const pluginSchema = require("@quasibit/eleventy-plugin-schema");
const pluginSitemap = require("@quasibit/eleventy-plugin-sitemap");
const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const siteConfig = require("./src/_data/site.js");
const marked = require("marked");
const prismjs = require("prismjs");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
// Enable linked headings in md files
const markdownLib = markdownIt({ html: true }).use(markdownItAnchor, {
permalink: true,
level: [2],
permalinkSymbol: `
<svg width="16" height="16" class="icon icon--link" role="img" aria-labelledby="link--title">
<title id="link--title">Anchor link</title>
<use xlink:href="#link" fill="CurrentColor"></use>
</svg>
`,
});
// Markdown filter: Making the syntax highlighting consistent between md files and imported Readmes
marked.setOptions({
highlight: function (code, lang) {
if (prismjs.languages[lang]) {
return prismjs.highlight(code, prismjs.languages[lang], lang);
} else {
return code;
}
},
});
module.exports = function (eleventyConfig) {
// Merge default an theme specific tags together
eleventyConfig.setDataDeepMerge(true);
// Enable Sass usage
// eleventyConfig.addPlugin(pluginSass, {
// watch: "src/assets/css/*",
// outputDir: "dist/assets/css",
// });
// Compile Sass before a build
eleventyConfig.on("beforeBuild", () => {
let result = sass.renderSync({
file: "src/assets/css/styles.scss",
sourceMap: false,
outputStyle: "compressed",
});
fs.ensureDirSync('dist/assets/css');
fs.writeFile("dist/assets/css/styles.css", result.css, (err) => {
if (err) throw err;
console.log("CSS generated");
});
});
// Enable core SEO features
eleventyConfig.addPlugin(pluginSEO, {
...siteConfig,
image: "/assets/images/logos/netlify-functions-og-card.png",
options: {
twitterCardType: "summary_large_image",
imageWithBaseUrl: true,
},
});
// Add schema data
eleventyConfig.addPlugin(pluginSchema);
// Add a sitemap
eleventyConfig.addPlugin(pluginSitemap, {
sitemap: {
hostname: siteConfig.url,
},
});
// Add syntax highlighting
eleventyConfig.addPlugin(pluginSyntaxHighlight);
// Date formatting
eleventyConfig.addFilter("readableDate", (str) =>
new Date(str).toLocaleString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
})
);
// Display array keys and remove undefined tags
eleventyConfig.addFilter("keys", (obj) => Object.keys(obj));
// Exclude items from an array
eleventyConfig.addFilter("except", (arr = [], ...values) => {
const data = new Set(arr);
for (const item of values) {
data.delete(item);
}
return [...data].sort();
});
// Filter a collection based on items flagged as "featured"
eleventyConfig.addFilter("featured", (items) => {
return items.filter((item) => item.data.featured);
});
// If string contains a certain string
eleventyConfig.addFilter(
"contains",
(str, parameter) => str && str.includes(parameter)
);
// Markdown filter
eleventyConfig.addFilter("markdownify", (str) => marked(str));
// Copy JavaScript, images, functions and Netlify CMS config into dist
eleventyConfig.addPassthroughCopy("src/assets/images");
eleventyConfig.addPassthroughCopy("src/assets/js");
eleventyConfig.addPassthroughCopy("src/functions");
eleventyConfig.addPassthroughCopy("src/admin/config.yml");
eleventyConfig.addPassthroughCopy({
"node_modules/@yaireo/tagify/dist/tagify.min.js": "assets/js/tagify.min.js",
"node_modules/@yaireo/tagify/dist/tagify.css": "assets/css/tagify.css",
});
// Redefined the markdown library to modify behaviour, to add linkable headings
eleventyConfig.setLibrary("md", markdownLib);
return {
dir: {
input: "src",
output: "dist",
},
markdownTemplateEngine: "njk",
passthroughFileCopy: true,
};
};