forked from phuocng/html-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
127 lines (114 loc) · 3.92 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
const fs = require('fs');
const markdownIt = require('markdown-it');
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const htmlmin = require('html-minifier');
const CATEGORIES = {
'Basic': 0,
'Intermediate': 1,
'Advanced': 2,
'Tip': 3,
};
module.exports = function(eleventyConfig) {
// Copy the `img` and `css` folders to the output
eleventyConfig.addPassthroughCopy('assets');
eleventyConfig.addPlugin(syntaxHighlight);
let markdownLibrary = markdownIt({
html: true,
linkify: true
});
eleventyConfig.setLibrary('md', markdownLibrary);
// Get the first `n` elements of a collection.
eleventyConfig.addFilter("head", (array, n) => {
if (!Array.isArray(array) || array.length === 0) {
return [];
}
return (n < 0) ? array.slice(n) : array.slice(0, n);
});
eleventyConfig.addCollection('sortByTitle', function(collectionApi) {
return collectionApi.getAll()
.filter(function(item) {
let extension = item.inputPath.split('.').pop();
return extension === 'md';
})
.sort(function(a, b) {
return a.data.title - b.data.title;
});
});
eleventyConfig.addCollection('categories', function(collectionApi) {
const categories = [];
collectionApi.getAll()
.filter(function(item) {
let extension = item.inputPath.split('.').pop();
return extension === 'md';
})
.forEach((item) => {
const category = item.data.category;
if (category && !categories.includes(category)) {
categories.push(category);
}
});
return categories.sort(function(a, b) {
return CATEGORIES[a] - CATEGORIES[b];
});
});
eleventyConfig.addCollection('groupByCategories', function(collectionApi) {
const categories = {};
collectionApi.getAll()
.filter(function(item) {
let extension = item.inputPath.split('.').pop();
return extension === 'md';
})
.forEach((item) => {
const category = item.data.category;
if (!category) {
return;
}
Array.isArray(categories[category])
? categories[category].push(item)
: categories[category] = [item];
});
return categories;
});
// Shortcodes
eleventyConfig.addShortcode('demo', function(url) {
return `<div class="example example--border">
<div class="example__ribbon example__ribbon--tr">
<span class="example__title">Demo</span>
</div>
<div class="example__content">
<autofit-frame src="${url}"></autofit-frame>
</div>
</div>`;
});
eleventyConfig.addTransform('minify-html', function(content) {
if (this.outputPath && this.outputPath.endsWith('.html')) {
return htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
}
return content;
});
return {
// Control which files Eleventy will process
// e.g.: *.md, *.njk, *.html, *.liquid
templateFormats: [
'md',
'njk',
'html',
'liquid',
],
// Pre-process *.md files with: (default: `liquid`)
markdownTemplateEngine: 'njk',
// Pre-process *.html files with: (default: `liquid`)
htmlTemplateEngine: 'njk',
// These are all optional (defaults are shown):
dir: {
input: 'contents',
includes: '_includes',
data: '_data',
output: '_site'
}
};
};