-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.eleventy.js
188 lines (153 loc) · 6.5 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
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
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItAttrs = require("markdown-it-attrs");
const moment = require("moment");
const slugify = require("slugify");
const Image = require("@11ty/eleventy-img");
const fetch_image = require("./src/fetch_image.js");
const CleanCSS = require("clean-css");
const extractExcerpt = require("./src/shortcodes/extract_excerpt.js");
const groupByYear = require("./src/filters/group_by_year.js");
const thumbnails = require("./src/thumbnails.js");
const tab_gallery = require("./src/shortcodes/make_tab_gallery.js");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const install_media_objects = require("./src/media_object.js");
const {
fortawesomeBrandsPlugin,
} = require('@vidhill/fortawesome-brands-11ty-shortcode');
module.exports = function(eleventyConfig) {
// Configure markdown parser
const markdownLib = markdownIt({html: true, typographer: true});
markdownLib.use(markdownItAnchor);
markdownLib.use(markdownItAttrs);
eleventyConfig.setLibrary("md", markdownLib);
eleventyConfig.addPlugin(fortawesomeBrandsPlugin);
eleventyConfig.addPlugin(syntaxHighlight);
install_media_objects(eleventyConfig);
//copy through assets
eleventyConfig.addPassthroughCopy("admin");
eleventyConfig.addPassthroughCopy("assets");
eleventyConfig.addPassthroughCopy("dist");
eleventyConfig.addPassthroughCopy("favicon.png");
eleventyConfig.addPassthroughCopy("galleries/**/*.{jpg,jpeg,JPG,png,gif,svg}");
eleventyConfig.addPassthroughCopy("google5458abc1104b04dd.html");
eleventyConfig.addPassthroughCopy({"htaccess": ".htaccess"});
eleventyConfig.addPassthroughCopy({"content": "."}, {
filter: ["**/*.{jpg,jpeg,JPG,png,gif,svg}"],
rename: function (path) {
return path.replace("content/", "");
}
});
eleventyConfig.addWatchTarget("src/**/*");
let $collectionApi = null;
eleventyConfig.addCollection("posts", function(collectionApi) {
$collectionApi = collectionApi;
return collectionApi.getFilteredByGlob("_posts/*.md");
});
eleventyConfig.setLiquidOptions({
dynamicPartials: false,
});
eleventyConfig.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
eleventyConfig.addShortcode("image", async function(src, alt, sizes) {
let metadata = await fetch_image(src);
let imageAttributes = {
alt,
sizes,
loading: "lazy",
decoding: "async",
};
// You bet we throw an error on a missing alt (alt="" works okay)
return Image.generateHTML(metadata, imageAttributes);
});
eleventyConfig.addShortcode("img_responsive", async function(src, alt, sizes="100vw, 720, 820, 940, 1140, 1280") {
let metadata = await fetch_image(src);
let imageAttributes = {
alt,
sizes,
class: "img-responsive",
loading: "lazy",
decoding: "async",
};
return Image.generateHTML(metadata, imageAttributes);
});
eleventyConfig.addShortcode("image_with_class", async function(src, alt, sizes, class_names) {
let metadata = await fetch_image(src);
if (sizes==undefined || sizes=="_") {
sizes="720, 940, 1140, 1280, 2048";
}
let imageAttributes = {
alt,
sizes,
class: class_names,
loading: "lazy",
decoding: "async",
};
// You bet we throw an error on a missing alt (alt="" works okay)
return Image.generateHTML(metadata, imageAttributes);
});
// Thumbnails
eleventyConfig.addShortcode("thumbnail_for_post", thumbnails.thumbnail_for_post);
eleventyConfig.addFilter("has_thumbnail", thumbnails.has_thumbnail);
eleventyConfig.addShortcode("thumbnail_from_path", thumbnails.thumbnail_from_path);
eleventyConfig.addShortcode("tab_gallery", tab_gallery);
// Defines shortcode for generating post excerpts
eleventyConfig.addShortcode('excerpt', extractExcerpt);
eleventyConfig.addFilter('excerpt', extractExcerpt);
eleventyConfig.addFilter("with_titles", items => {
return items.filter(item => "title" in item.data);
});
// Liquid filter to convert a date to a string
eleventyConfig.addLiquidFilter("to_utc_string", date => date.toUTCString() );
// Liquid filter for long date string
eleventyConfig.addLiquidFilter("date_to_long_string", function(date) {
return date.toLocaleDateString("en-GB", { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
});
// Universal filter for jsonify
eleventyConfig.addFilter("jsonify", JSON.stringify );
eleventyConfig.addFilter("with_explicit_date", items => items.filter(item => "date" in item.data));
eleventyConfig.addFilter("group_by_year", groupByYear);
eleventyConfig.addShortcode("log", console.log);
// Read the menu data from _config.yml and add it to the global data
eleventyConfig.addGlobalData("menu", () => getDataFromConfigYaml("menu"));
eleventyConfig.addGlobalData("site_title", () => getDataFromConfigYaml("title"));
eleventyConfig.addGlobalData("site_tagline", () => getDataFromConfigYaml("tagline"));
eleventyConfig.addGlobalData("production_url", () => getDataFromConfigYaml("production_url"));
eleventyConfig.addGlobalData("JB", () => getDataFromConfigYaml("JB"));
eleventyConfig.addGlobalData("author", () => getDataFromConfigYaml("author"));
eleventyConfig.addGlobalData("now" , () => new Date());
eleventyConfig.addNunjucksFilter("date", function(date, format) {
return moment(date).format(format);
});
eleventyConfig.addCollection("tagList", (collection) => {
const tagsSet = new Set();
const tagsDict = {};
// Iterate over all content files and collect tags
collection.getAll().forEach((item) => {
if (item.data.tags) {
item.data.tags.forEach((tag) => {
const slugTag = slugify(tag);
if (!(slugTag in tagsDict)) {
tagsDict[slugTag] = [];
}
tagsDict[slugTag].push(item);
});
}
});
return tagsDict;
});
return {
dir: {
layouts: "_includes/layouts"
}
}
};
function getDataFromConfigYaml(key) {
// Load the _config.yml file
const yaml = require("js-yaml");
const fs = require("fs");
const config = yaml.load(fs.readFileSync("_config.yml", "utf-8"));
// Return the menu data
return config[key];
}