forked from phuocng/csslayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
163 lines (147 loc) · 5.68 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
const fs = require('fs');
const markdownIt = require('markdown-it');
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const htmlmin = require('html-minifier');
const randomInteger = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
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);
// Shortcodes
eleventyConfig.addPairedShortcode('demo', function(content) {
return `<div class="example example--border">
<div class="example__ribbon example__ribbon--tr">
<span class="example__title">Demo</span>
</div>
<div class="example__content example__content--medium">${content}</div>
</div>`;
});
eleventyConfig.addPairedShortcode('pattern', function(content, name) {
const href = `/${name.toLowerCase().split(' ').join('-')}/`;
return `<div class="pattern__item">
<a class="pattern__link" href="${href}">
<div class="pattern__cover">${content}</div>
<div class="pattern__title">${name}</div>
</a>
</div>`;
});
// `size` can be `sm`, `md`, `lg`
eleventyConfig.addShortcode('circle', function(size) {
const s = size || 'sm';
return `<div class="circle circle--${s}"></div>`;
});
// `direction` can be `hor` or `ver`
eleventyConfig.addShortcode('line', function(dir) {
const w = randomInteger(1, 4) * 20;
return `<div class="line line--${dir} line--${w}"></div>`;
});
eleventyConfig.addShortcode('lines', function(dir, numLines) {
const content = Array(numLines).fill('').map(_ => {
const w = randomInteger(1, 4) * 20;
return `<div class="line line--${dir} line--${w}"></div>`
}).join('');
return `<div class="lines">${content}</div>`;
});
// `direction` can be `hor` or `ver`
eleventyConfig.addShortcode('rectangle', function(dir, size, width) {
const direction = dir || 'hor';
const s = size || 'sm';
const w = width || randomInteger(2, 4) * 20;
return `<div class="rectangle rectangle--${direction} rectangle--${s} rectangle--${w}"></div>`;
});
eleventyConfig.addShortcode('square', function(size) {
const s = size || 'sm';
return `<div class="square square--${s}"></div>`;
});
// `corner` can be one of `t`, `r`, `b`, `l`, `tr`, `br`, `tl`, `bl`
eleventyConfig.addShortcode('triangle', function(corner, size) {
const s = size || 'sm';
return `<div class="triangle triangle--${corner} triangle--${s}"></div>`;
});
// 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();
});
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;
});
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'
}
};
};