forked from cagov/covid19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
258 lines (217 loc) · 9.39 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const CleanCSS = require("clean-css");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const fs = require('fs')
const langData = JSON.parse(fs.readFileSync('pages/_data/langData.json','utf8'));
const pageNav = JSON.parse(fs.readFileSync('pages/_data/pageNav.json','utf8'));
module.exports = function(eleventyConfig) {
//Copy static assets
eleventyConfig.addPassthroughCopy({ "./src/css/fonts": "fonts" });
eleventyConfig.addPassthroughCopy({ "./src/img": "img" });
eleventyConfig.addPassthroughCopy({ "./src/pdf": "pdf" });
eleventyConfig.addPassthroughCopy({ "./pages/rootcopy": "/" });
//azure-pipelines-staging.yml
//Process manual content folder
eleventyConfig.addCollection("manualcontent", function(collection) {
const manualContentFolderName = 'manual-content';
let output = [];
collection.getAll().forEach(item => {
if(item.inputPath.includes(manualContentFolderName)) {
item.outputPath = item.outputPath.replace(`/${manualContentFolderName}`,'');
item.url = item.url.replace(`/${manualContentFolderName}`,'');
output.push(item);
};
});
return output;
});
//Process wordpress posts
eleventyConfig.addCollection("wordpressposts", function(collection) {
const FolderName = 'wordpress-posts';
let output = [];
collection.getAll().forEach(item => {
if(item.inputPath.includes(FolderName)) {
item.outputPath = item.outputPath.replace(`/${FolderName}`,'');
item.url = item.url.replace(`/${FolderName}`,'');
item.data.page.url = item.url;
output.push(item);
if(!item.data.title) {
//No title means fragment
console.log(`Skipping fragment ${item.inputPath}`)
item.outputPath = false;
}
};
});
return output;
});
eleventyConfig.addCollection("covidGuidance", function(collection) {
let posts = [];
collection.getAll().forEach( (item) => {
if(item.data.tags && item.data.tags[0] == 'guidancefeed') {
posts.push(item);
}
})
return posts.slice().sort(function(a, b) {
let bPub = new Date(b.data.publishdate);
let aPub = new Date(a.data.publishdate)
return bPub.getTime() - aPub.getTime();
});
});
eleventyConfig.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
// Format dates within templates.
eleventyConfig.addFilter('formatDate', function(datestring) {
if(datestring&&datestring.indexOf('Z') > -1) {
const date = new Date(datestring);
const locales = 'en-US';
const timeZone = 'America/Los_Angeles';
return `${date.toLocaleDateString(locales, { timeZone, day: 'numeric', month: 'long', year: 'numeric' })} at ${date.toLocaleTimeString(locales, { timeZone, hour: 'numeric', minute: 'numeric' })}`;
} else {
return datestring;
}
});
eleventyConfig.addFilter('truncate220', function(textstring) {
if(!textstring || textstring.length <221) {
return textstring.slice(0,220)+'...';
}
return textstring;
});
const contentfrompage = (content, page, slug) => {
if(page.fileSlug && slug && page.fileSlug.toLocaleLowerCase()===slug.toLocaleLowerCase()) {
return content;
}
return "";
}
const getTranslatedValue = (pageObj, tags, field) => {
let langTag = getLangRecord(tags);
if(!pageObj)
return "";
if(pageObj[langTag.wptag] && pageObj[langTag.wptag][field]) {
return pageObj[langTag.wptag][field];
}
//that page is missing for that lang, bring in the default
return pageObj[getLangRecord([]).wptag][field];
}
// return the active class for a matching string
eleventyConfig.addFilter('pageActive', (page, tags, pageObj) => contentfrompage(" active", page, getTranslatedValue(pageObj, tags, 'slug')));
// return the translated url or title if appropriate
eleventyConfig.addFilter('getTranslatedVal', getTranslatedValue);
// show or hide content based on page
eleventyConfig.addPairedShortcode("pagesection", contentfrompage);
eleventyConfig.addTransform("findaccordions", function(html, outputPath) {
if(outputPath&&outputPath.endsWith(".html")) {
const dom = new JSDOM(html);
const accordions = dom.window.document.querySelectorAll('.cwds-accordion');
if(accordions.length>0) {
accordions.forEach(accordion => {
// bunch of weird hax to make custom elements out of wordpress content
if(accordion.querySelector('h4')) {
const titleVal = accordion.querySelector('h4').innerHTML;
const target = accordion.querySelector('h4').parentNode;
accordion.querySelector('h4').remove();
accordion.querySelector('.wp-block-group__inner-container').classList.add('card');
let container = accordion.querySelector('.card-container');
if(!container) {
container = accordion.querySelector('ul');
}
if(container) {
const containerContent = container.innerHTML;
container.parentNode.insertAdjacentHTML('beforeend',`
<div class="card-container" aria-hidden="true" style="height: 0px;">
<div class="card-body">${containerContent}</div>
</div>`);
container.parentNode.removeChild(container);
target.insertAdjacentHTML('afterbegin',`<button class="card-header accordion-alpha" type="button" aria-expanded="false">
<div class="accordion-title">
<h4>${titleVal}</h4>
</div>
</button>`);
accordion.innerHTML = `<cwds-accordion>${accordion.innerHTML}</cwds-accordion>`;
}
}
});
return dom.serialize();
}
}
return html;
});
eleventyConfig.addTransform("findaccordions2", function(html, outputPath) {
const headerclass = 'wp-accordion';
const contentclass = 'wp-accordion-content';
if(outputPath&&outputPath.endsWith(".html")&&html.indexOf(headerclass)>-1) {
const dom = new JSDOM(html);
const document = dom.window.document;
for(const header of document.querySelectorAll(`.${headerclass}`)) {
//create the wrapper element and wrap it around the header
const cwdscontainer = document.createElement('cwds-accordion');
const container = document.createElement('div');
container.classList.add('card');
cwdscontainer.appendChild(container);
header.parentNode.insertBefore(cwdscontainer, header);
container.appendChild(header);
//remove the special wp class
header.classList.remove(headerclass);
if (header.classList.length===0) header.removeAttribute('class');
//create the card body section and add it to the container
const body = document.createElement('div');
body.className="card-body";
container.appendChild(body);
//Add all remaining content classes to the card body, they must be directly after the new container
let direct;
while (direct = document.querySelector(`cwds-accordion + .${contentclass}`)) {
body.appendChild(direct);
//remove custom class name
direct.classList.remove(contentclass);
if (direct.classList.length===0) direct.removeAttribute('class');
}
//apply required html around components
header.outerHTML=`
<button class="card-header accordion-alpha" type="button" aria-expanded="false">
<div class="accordion-title">
${header.outerHTML}
</div>
</button>`;
body.outerHTML = `
<div class="card-container" aria-hidden="true" style="height: 0px;">
${body.outerHTML}
</div>`;
}
return dom.serialize();
}
return html;
});
eleventyConfig.addFilter('jsonparse', json => JSON.parse(json));
eleventyConfig.addFilter('includes', (items,value) => (items || []).includes(value));
const getLangRecord = tags =>
langData.languages.filter(x=>x.enabled&&(tags || []).includes(x.wptag)).concat(langData.languages[0])[0];
const getLangCode = tags =>
getLangRecord(tags).hreflang;
eleventyConfig.addFilter('lang', getLangCode);
eleventyConfig.addFilter('langRecord', getLangRecord);
eleventyConfig.addFilter('htmllangattributes', tags => {
const langRecord = getLangRecord(tags);
return `lang="${langRecord.hreflang}" xml:lang="${langRecord.hreflang}"${(langRecord.rtl ? ` dir="rtl"` : "")}`;
});
eleventyConfig.addFilter('publishdateorfiledate', page =>
(page.data
? page.data.publishdate
: page.publishdate)
|| page.date.toISOString()
);
eleventyConfig.addPairedShortcode("dothisifcontentexists", (content, contentcontent, match) =>
contentcontent.match(match) ? content : "");
// return alternate language pages
eleventyConfig.addFilter('getAltPageRows', (page, tags) => {
const pageNavRecord = pageNav.navList.find(f=>langData.languages.find(l=>f[l.wptag]&&f[l.wptag].slug===page.fileSlug));
if(pageNavRecord) {
return langData.languages
.filter(x=>x.enabled&&pageNavRecord[x.wptag]&&pageNavRecord[x.wptag].slug!==page.fileSlug&&pageNavRecord[x.wptag].url)
.map(x=>({
langcode:x.id,
langname:x.name,
url:pageNavRecord[x.wptag].url
}));
}
});
eleventyConfig.htmlTemplateEngine = "njk,findaccordions,findaccordions2";
};