Repository navigation
Expand file tree
/
Copy path.eleventy.js
More file actions
145 lines (119 loc) · 3.06 KB
/
Copy path.eleventy.js
File metadata and controls
145 lines (119 loc) · 3.06 KB
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
const { isValidElement } = require('preact');
const fs = require('fs');
const { render } = require('preact-render-to-string');
const markdown = require('markdown-it');
const markdownAnchor = require('markdown-it-anchor');
/* -----------------------------------
*
* 11ty
*
* -------------------------------- */
module.exports = function (config) {
config.addPassthroughCopy('./src/_js/assets');
config.addPassthroughCopy('./src/_js/articles/_images');
config.setBrowserSyncConfig({
files: ['dist/**/*'],
});
config.setUseGitIgnore(false);
config.setDataDeepMerge(true);
config.setWatchThrottleWaitTime(300);
config.addTransform('jsx', (content) => {
if (isValidElement(content)) {
return `<!doctype html>${render(content)}`;
}
return content;
});
config.addTransform('hash', (content, path) => {
if (path.endsWith('.html')) {
return transformFileHash(content);
}
return content;
});
config.addJavaScriptFunction('getAssetContents', getAssetContents);
config.setLibrary(
'md',
markdown({ html: true, breaks: true, linkify: true }).use(markdownAnchor, {
permalink: true,
permalinkBefore: true,
permalinkClass: 'title-anchor',
permalinkSymbol: '',
permalinkAttrs: (slug) => ({ 'aria-label': normaliseTitleAnchors(slug) }),
slugify: slugifyTitleAnchors,
})
);
return {
passthroughFileCopy: true,
jsDataFileSuffix: '.data',
dir: {
input: 'src/_js',
output: 'dist',
data: 'data',
layouts: 'layouts',
includes: '_includes',
},
};
};
/* -----------------------------------
*
* Hashes
*
* -------------------------------- */
function transformFileHash(content) {
let assets = {};
try {
assets = require('./src/_js/assets.json');
} catch {
// no-op
}
const keys = Object.keys(assets);
return keys.reduce(
(result, key) =>
result.replace(
new RegExp(`(script|link)(.*?)(src|href)="(.*?)${key}"`, 'g'),
`$1$2$3="$4${assets[key]}"`
),
content
);
}
/* -----------------------------------
*
* Contents
*
* -------------------------------- */
function getAssetContents(paths) {
const assets = require('./src/_js/assets.json');
if (!paths.some((path) => assets[path])) {
return;
}
return paths.reduce((result, path) => {
const value = fs.readFileSync(`./src/_js/assets/${assets[path]}`);
return result + value.toString();
}, '');
}
/* -----------------------------------
*
* Anchors
*
* -------------------------------- */
function slugifyTitleAnchors(value) {
return encodeURIComponent(
String(value)
.toString()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/&/g, '-and-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')
);
}
/* -----------------------------------
*
* Anchors
*
* -------------------------------- */
function normaliseTitleAnchors(value) {
const result = value.replace(/-/g, ' ');
return result.charAt(0).toUpperCase() + result.slice(1);
}