-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
207 lines (173 loc) · 5.47 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const { readFileSync } = require('fs');
const yaml = require('js-yaml');
const { URL } = require('url');
const htmlmin = require('html-minifier');
const xmlmin = require('minify-xml');
const { parse: htmlParse, HTMLElement } = require('node-html-parser');
const readingTime = require('reading-time');
const escapeHtml = require('escape-html');
const IntlPolyfill = require('intl');
require('./locale/udm');
const site = require('./src/data/site');
const relative = function (url, base) {
return new URL(url, site.url + base).href;
}
const optionalAbsolute = function (url, base) {
if (/^(https?:)?\/\//.test(url)) {
return url;
}
return base + url;
}
module.exports = (config) => {
const dictionary = yaml.load(readFileSync(__dirname + '/src/data/words.yml'));
/** @type {import('markdown-it').Options} */
const mdOptions = {
html: true,
};
const md = require('markdown-it')(mdOptions)
.use(require('markdown-it-anchor'))
.use(require('markdown-it-texmath'), {
engine: require('katex'),
katexOptions: {
strict: 'ignore',
trust: true,
},
})
.use(require('markdown-it-footnote'))
.use(require('./utils/word-parser'), {
dictionary: Object.fromEntries(
dictionary.map(({ initial,def }) => [initial, def])
),
});
md.renderer.rules.footnote_block_open = () => (
'<section class="main-margin">' +
'<h2 id="references">Валэктонъёс</h2>' +
'<ol class="footnotes-list">'
);
config.setLibrary('md', md);
config.addDataExtension('yml', contents => yaml.load(contents));
config.addPassthroughCopy('src/assets');
config.addPassthroughCopy('src/fonts');
config.addPassthroughCopy('src/sw.js');
config.addPassthroughCopy('src/**/*.(html|png|jpg|js)');
config.ignores.add('src/styles');
config.ignores.add('src/scripts');
config.addCollection('articles', api =>
api.getFilteredByGlob('src/articles/**/index.md')
);
config.addShortcode('relative', relative);
config.addShortcode('mkMeta', function (meta) {
if (!meta)
return '';
return Object.entries(meta).map(([key, value]) =>
`<meta name="${escapeHtml(key)}" content="${escapeHtml(value)}">`
).join('\n');
});
config.addShortcode('mkRdf', function (rdf) {
if (!rdf)
return '';
const pairs = [];
const addProperty = (prefix, prop, value) => {
// Transform properties marked with ! to an absolute URL
if (prop.endsWith('!')) {
prop = prop.slice(0, -1);
value = relative(value, this.page.url);
}
const qualified = (prefix ? (prefix + ':') : '') + prop;
return [qualified, value];
};
for (const key in rdf) {
if (typeof rdf[key] === "object")
pairs.push(...Object.entries(rdf[key])
.map(([prop, value]) => addProperty(key, prop, value))
);
else
pairs.push(addProperty('', key, rdf[key]));
}
return pairs.map(([key, value]) =>
`<meta property="${escapeHtml(key)}" content="${escapeHtml(value)}">`
).join('\n');
});
config.addShortcode('mkPrefix', prefix =>
Object.entries(prefix).map(([k, v]) => `${k}: ${v}`).join(' ')
);
config.addFilter('md', (content, inline) =>
inline
? md.renderInline(content)
: md.render(content)
);
config.addFilter('optionalAbsolute', optionalAbsolute);
config.addFilter('headings', (content, _level) => {
const level = _level || 3;
const document = htmlParse(content);
return document
.querySelectorAll('h1, h2, h3, h4, h5, h6')
.map((el) => ({
id: el.id,
level: parseInt(el.tagName[1]),
content: el.textContent,
}))
.filter((heading) => heading.level <= level);
});
config.addFilter('excludeJsSpecific', (content) => {
const document = htmlParse(content);
document.querySelectorAll('.init').forEach((node) =>
node.remove()
);
return document.toString();
});
config.addFilter('isoDate', date => date.toISOString());
config.addFilter('udmDate', date =>
new IntlPolyfill.DateTimeFormat('udm', {
day: 'numeric',
month: 'short',
year: 'numeric',
}).format(date)
);
config.addFilter('langname', lang =>
new Intl.DisplayNames(lang, { type: 'language' }).of(lang)
);
config.addFilter('readTime', content => {
const { minutes } = readingTime(content);
const minutes_ = Math.round(minutes);
return (minutes_ < 1)
? 'минутлэсь ичи лыдӟон'
: `${minutes_} минут лыдӟон`;
});
config.addFilter('limit', (val, n) => val.slice(0, n));
config.addFilter('byYear', (arr) => {
return arr.reduce((all, v) => {
const year = v.date.getFullYear();
if (!all.has(year))
all.set(year, []);
all.get(year).push(v);
return all;
}, new Map());
});
config.addTransform('htmlmin', function (content, outputPath) {
if (outputPath && outputPath.endsWith('.html')) {
return htmlmin.minify(content, {
collapseWhitespace: true,
removeComments: true,
});
}
return content;
});
config.addTransform('xmlmin', function (content, outputPath) {
if (outputPath && outputPath.endsWith('.xml')) {
return xmlmin.minify(content);
}
return content;
});
return {
dir: {
input: 'src',
output: 'dist',
includes: 'includes',
data: 'data',
},
dataTemplateEngine: 'njk',
markdownTemplateEngine: 'njk',
templateFormats: ['md', 'njk', '11ty.js'],
};
};