This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
.core-eleventy.js
83 lines (72 loc) · 2.54 KB
/
.core-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
const fs = require('fs');
const matter = require('gray-matter');
const MarkdownIt = require('markdown-it');
const cc = require('config-chain');
const globalLocaleStrings = require('./src/_data/core/core-locale-strings.json');
const appLocaleStrings = require('./src/_data/app/app-locale-strings.json');
const localeStrings = { ...globalLocaleStrings, ...appLocaleStrings };
const appConf = cc(cc.find('.jam-on/app/conf.json'));
const appVersionMetadata = cc(cc.find('.jam-on/app/versionMetadata.json'));
let toolkitVersion;
try {
toolkitVersion = appVersionMetadata.get('tagOrBranch');
} catch (e) {
try {
toolkitVersion = appConf.get('toolkitPackageJSONVersion');
} catch {
toolkitVersion = 'version undetermined';
/* eslint-disable no-console */
console.warn(
"Your version of the Ontario.ca Jamstack Toolkit can't be determined and some features may not work correctly; you can try performing an upgrade to the latest release using the `jam-on.mjs` CLI to resolve this issue, or contact the Ontario.ca Service Integration team for help.",
);
/* eslint-enable no-console */
}
}
function coreConfigFunc(eleventyConfig) {
const md = new MarkdownIt({
html: true,
});
eleventyConfig.addFilter('markdown', (content) => md.renderInline(content));
eleventyConfig.addShortcode('toolkitVersion', () => toolkitVersion);
eleventyConfig.addShortcode(
'currentYear',
() => `${String(new Date().getFullYear())}`,
);
eleventyConfig.addShortcode(
'currentShortYear',
() => `${String(new Date().getFullYear()).slice(-2)}`,
);
/* eslint-disable func-names */
eleventyConfig.addFilter('localeString', function (key) {
/* eslint-enable func-names */
// Solution for accessing page front matter from https://stackoverflow.com/a/67746326
const { page } = this.ctx;
const str = fs.readFileSync(page.inputPath, 'utf8');
const { data } = matter(str);
const lang = data.lang || 'en';
let localeString;
if (key.includes('.')) {
const keyArr = key.split('.');
localeString = localeStrings[keyArr.shift()];
if (keyArr.length > 0) {
keyArr.forEach((k) => {
localeString = localeString[k];
});
}
} else {
localeString = localeStrings[key];
}
if (Array.isArray(localeString)) {
localeString = localeString.map((string) => string[lang]);
return localeString;
}
return `${localeString[lang]}`;
});
}
const coreConfigObj = {
pathPrefix: '/',
};
module.exports = {
configFunc: coreConfigFunc,
configObj: coreConfigObj,
};