-
Notifications
You must be signed in to change notification settings - Fork 3
/
.eleventy.js
186 lines (167 loc) · 6.46 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
const fs = require('fs');
const prism = require('prismjs');
const prismLoader = require('prismjs/components/index.js');
const slugify = require('slugify');
const markdownIt = require('markdown-it');
const markdownItAttrs = require('markdown-it-attrs');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItLinkAttributes = require('markdown-it-link-attributes');
const nestingToc = require('eleventy-plugin-nesting-toc');
const { EleventyRenderPlugin } = require("@11ty/eleventy");
module.exports = function(eleventyConfig) {
eleventyConfig.setDataDeepMerge(true);
// Plugins
eleventyConfig.addPlugin(EleventyRenderPlugin);
eleventyConfig.addPlugin(nestingToc,
{
tags: ['.cf-content > h2', '.cf-content > h3', '.cf-content > h4'],
wrapperClass: 'cf-toc'
}
);
// Filters
eleventyConfig.addFilter('sitemapDateString', (dateObj) => {
return new Date(dateObj).toISOString();
});
eleventyConfig.addFilter('pathLast', (str) => {
let array = str.replace(/\/$/, '').split('/');
let val = array[array.length - 1];
return (typeof val === undefined) ? '' : val;
});
eleventyConfig.addFilter('valueIfEmpty', function() {
let len = arguments.length;
for (let i = 0; i < len; i++) {
if (typeof arguments[i] !== 'undefined') {
return arguments[i];
}
}
return false;
});
// Fake a `.cf-content` wrapper for TOC tag selectors
eleventyConfig.addFilter('contentWrap', (content) => {
return '<div class="cf-content">' + content + '</div>';
});
// Markdown plugins
const slugifyHeading = s => slugify(s, {lower: true, remove: /[*+~.()'"!?:@\[\]\/]/g});
const markdownItOptions = {
html: true,
breaks: true,
linkify: true
};
const markdownItAnchorOptions = {
slugify: slugifyHeading,
permalink: markdownItAnchor.permalink.ariaHidden({
class: 'direct-link',
symbol: '',
style: 'aria-describedby',
placement: 'after'
})
};
const markdownItLinkAttributesOptions = {
matcher(href) {
return href.match(/^https?:\/\//);
},
attrs: {
target: "_blank",
rel: "noopener"
}
};
const md = new markdownIt();
eleventyConfig.setLibrary('md', markdownIt(markdownItOptions)
.use(markdownItAttrs)
.use(markdownItAnchor, markdownItAnchorOptions)
.use(markdownItLinkAttributes, markdownItLinkAttributesOptions)
);
// Use singular short codes for syntax highlights and examples
// Work around issue with no parsing between paired shortcodes.
eleventyConfig.addShortcode('renderHighlight', function(content, language) {
let highlightedContent = content.trim();
let lines = {};
if(language !== 'text') {
if(!prism.languages[language]) {
prismLoader([language]);
}
highlightedContent = prism.highlight(highlightedContent, prism.languages[language]);
}
lines = highlightedContent.split('\n');
return `<pre class="language-${language}"><code class="language-${language}">` + lines.join('<br>') + '</code></pre>';
});
eleventyConfig.addShortcode('renderExample', function(content, addClass, id) {
let highlightedContent = content.trim();
let lines = {};
let output = '';
let outputId = '';
let parsedContent = '';
addClass = (typeof addClass !== 'undefined') ? ' ' + addClass : '';
// Strip out `holder.js` reference
if (highlightedContent.includes('data-src="holder.js')) {
parsedContent = highlightedContent.replace('data-src="holder.js', 'src="✂️holder.js');
parsedContent = parsedContent.replace(/\" /g, '✂️" ');
parsedContent = parsedContent.split('✂️');
highlightedContent = '';
parsedContent.forEach(function(str) {
highlightedContent += str.includes('holder.js') ? '...' : str;
});
}
// Send through PrismJS
if(!prism.languages['html']) {
prismLoader(['html']);
}
highlightedContent = prism.highlight(highlightedContent, prism.languages['html']);
lines = highlightedContent.split('\n');
content = content.replace(/\n\n/g, '\n');
if (typeof id !== 'undefined') {
outputId = ' id="' + id + '"';
}
output += `<div class="cf-example${addClass}"${outputId}>` + content + '</div>\n';
output += '<pre class="language-html"><code class="language-html">' + lines.join('<br>') + '</code></pre>';
return output;
});
eleventyConfig.addShortcode('renderCallout', function(content, level, addClass) {
addClass = (typeof addClass !== 'undefined') ? ' ' + addClass : '';
// Add preceding newline improve markup output
content = md.renderInline('\n' + content);
return `<div class="cf-callout cf-callout-${level}${addClass}">${content}</div>`;
});
// BrowserSync configuration and 404 page
eleventyConfig.setBrowserSyncConfig({
ghostMode: false,
notify: true,
callbacks: {
ready: function(err, browserSync) {
const content_404 = fs.readFileSync('_siteout/404.html');
browserSync.addMiddleware('*', (req, res) => {
// Provides the 404 content without redirect.
res.writeHead(404);
res.write(content_404);
res.end();
});
}
}
});
// Layout aliases
eleventyConfig.addLayoutAlias('home', 'layouts/home.html');
eleventyConfig.addLayoutAlias('doc', 'layouts/doc.html');
eleventyConfig.addLayoutAlias('alias', 'layouts/alias.html');
// Static assets
eleventyConfig.addPassthroughCopy({
'site/assets': '/assets',
'LICENSE': 'LICENSE'
});
eleventyConfig.setLiquidOptions({
dynamicPartials: true
});
return {
templateFormats: ['md', 'html', 'liquid'],
markdownTemplateEngine: 'liquid',
htmlTemplateEngine: 'liquid',
dataTemplateEngine: 'liquid',
passthroughFileCopy: true,
pathPrefix: '/',
dir: {
input: 'site',
output: '_siteout',
includes: '_includes',
data: '_data'
}
};
};