forked from brob/bryanlrobinson.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
134 lines (111 loc) · 4.59 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
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const dateFilter = require('nunjucks-date-filter');
const markdownIt = require("markdown-it");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const blogTools = require("eleventy-plugin-blog-tools");
const svgContents = require('eleventy-plugin-svg-contents');
const sanitizeHTML = require('sanitize-html')
// const pluginRespimg = require( "../eleventy-respimg" );
const htmlMinTransform = require('./src/transforms/html-min-transform.js');
const slugify = require('slugify');
const { DateTime } = require("luxon");
require('dotenv').config()
module.exports = function(config) {
let mdOptions = {
html: true,
breaks: true,
linkify: true
};
config.setLibrary("md", markdownIt(mdOptions).use(require('markdown-it-anchor'), {}));
config.addPlugin(svgContents);
config.addPlugin(syntaxHighlight);
config.addPlugin(pluginRss);
config.addPlugin(blogTools);
// Update the plugin
// Have class option
// Have option for automatic domain prepend
// Import prior to `module.exports` within `.eleventy.js`
config.addFilter("postDate", (dateObj) => {
return DateTime.fromJSDate(dateObj).toLocaleString(DateTime.DATE_MED);
});
config.addTransform('htmlmin', htmlMinTransform);
config.cloudinaryCloudName = 'brob';
config.srcsetWidths = [ 320, 640, 960, 1280 ];
config.fallbackWidth = 640;
config.lazyLoad = true;
// config.addPlugin(pluginRespimg);
config.addShortcode('respimg', (path, alt, sizes, className, srcsetWidths=config.srcsetWidths) => {
const fetchBase = `https://res.cloudinary.com/${config.cloudinaryCloudName}/image/fetch/`;
const src = `${fetchBase}q_auto,f_auto,w_${config.fallbackWidth}/${path}`;
if (!Array.isArray(srcsetWidths)) {
srcsetWidths = srcsetWidths.split(',');
}
const srcset = srcsetWidths.map(w => {
return `${fetchBase}q_auto:eco,f_auto,w_${w}/${path} ${w}w`;
}).join(', ');
return `<img loading="lazy" src="${src}" srcset="${srcset}" class="${className ? className : "respimg"}" sizes="${sizes ? sizes : '100vw'}" alt="${alt ? alt : ''}">`;
});
// Webmentions Filter
config.addFilter('webmentionsForUrl', (webmentions, url) => {
const allowedTypes = ['mention-of', 'in-reply-to', 'like-of']
const clean = content =>
sanitizeHTML(content, {
allowedTags: ['b', 'i', 'em', 'strong', 'a'],
allowedAttributes: {
a: ['href']
}
})
return webmentions
.filter(entry => entry['wm-target'] === url)
.filter(entry => allowedTypes.includes(entry['wm-property']))
.filter(entry => !!entry.content)
.map(entry => {
const { html, text } = entry.content
entry.content.value = html ? clean(html) : clean(text)
return entry
})
})
config.addCollection('posts', collection => {
const posts = collection.getFilteredByTag('posts');
const postsWithUpdatedDates = posts.map(item => {
item.date = item.data.post ? new Date(item.data.post.date) : item.date
item.featuredLarge = item.data.post ? item.data.post.featuredLarge : item.featuredLarge
item.featuredImg = item.data.post ? item.data.post.featuredImg : item.featuredImg
return item
})
const sortedPosts = postsWithUpdatedDates.sort((a, b) => b.date - a.date)
return sortedPosts;
});
config.addCollection('categories', collection => {
let categories = [];
let sortedPosts = [];
const posts = collection.getFilteredByTag('posts').reverse()
posts.forEach(post => {
categories = [...new Set([...categories, ...post.data.categories])];
});
categories.forEach(category => {
let filteredPosts = posts.filter(post => post.data.categories.includes(category));
let categoryDetails = {
'title': category,
'slug': slugify(category),
'posts': [ ...filteredPosts ]
};
sortedPosts.push(categoryDetails);
});
return sortedPosts;
});
config.addFilter("limit", (array, limit) => array.slice(0, limit));
config.addPassthroughCopy("_redirects");
config.addPassthroughCopy("src/images");
config.addPassthroughCopy("src/style.css");
config.addPassthroughCopy("src/service-worker.js");
config.addPassthroughCopy("src/manifest.json");
config.addPassthroughCopy("src/js");
return {
dir: {
input: 'src',
output: 'dist'
},
passthroughFileCopy: true
};
};