-
Notifications
You must be signed in to change notification settings - Fork 11
/
.eleventy.js
609 lines (514 loc) · 17.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import { EleventyHtmlBasePlugin } from "@11ty/eleventy";
import feedPlugin from "@11ty/eleventy-plugin-rss";
import YAML from 'yaml';
import { browsers, features, groups } from "web-features";
import bcd from "@mdn/browser-compat-data" with { type: "json" };
import specs from "browser-specs" with { type: "json" };
import mdnInventory from "@ddbeck/mdn-content-inventory";
import mdnDocsOverrides from "./additional-data/mdn-docs.json" with { type: "json" };
import standardPositions from "./additional-data/standard-positions.json" with { type: "json" };
import originTrials from "./additional-data/origin-trials.json" with { type: "json" };
const BROWSER_BUG_TRACKERS = {
chrome: "issues.chromium.org",
chrome_android: "issues.chromium.org",
edge: "issues.chromium.org",
firefox: "bugzilla.mozilla.org",
firefox_android: "bugzilla.mozilla.org",
safari: "bugs.webkit.org",
safari_ios: "bugs.webkit.org",
};
const MDN_URL_ROOT = "https://developer.mozilla.org/docs/";
function findParentGroupId(group) {
if (!group.parent) {
return null;
}
return group.parent;
}
function augmentFeatureData(id, feature) {
// Add the id.
feature.id = id;
// Make groups always an array.
feature.groups = [];
if (feature.group && !Array.isArray(feature.group)) {
feature.groups = [feature.group];
} else if (feature.group) {
feature.groups = feature.group;
}
// Create group paths. The groups that a feature belongs to might be
// nested in parent groups.
feature.groupPaths = [];
for (const groupId of feature.groups) {
const path = [groups[groupId].name];
let currentGroupId = groupId;
while (true) {
const parentId = findParentGroupId(groups[currentGroupId]);
if (!parentId) {
break;
}
path.unshift(groups[parentId].name);
currentGroupId = parentId;
}
feature.groupPaths.push(path);
}
// Make the spec always an array.
if (!feature.spec) {
feature.spec = [];
} else if (!Array.isArray(feature.spec)) {
feature.spec = [feature.spec];
}
// Add spec data from browser-specs, when possible.
feature.spec = feature.spec.map((spec) => {
const fragment = spec.includes("#") ? spec.split("#")[1] : null;
// Look for the spec URL in the browser-specs data.
const specData = specs.find((specData) => {
return (
specData.url === spec ||
spec.startsWith(specData.url) ||
(specData.nightly && spec.startsWith(specData.nightly.url))
);
});
return specData ? { ...specData, url: spec, fragment } : { url: spec };
});
// Get the first part of each BCD key in the feature (e.g. css, javascript, html, api, ...)
// to use as tags.
const bcdTags = [];
const bcdKeysData = (feature.compat_features || [])
.map((key) => {
// Find the BCD entry for this key.
const keyParts = key.split(".");
let data = bcd;
for (const part of keyParts) {
if (!data || !data[part]) {
console.warn(
`No BCD data for ${key}. Check if the web-features and browser-compat-data dependencies are in sync.`
);
return null;
}
data = data[part];
}
bcdTags.push(keyParts[0]);
return data && data.__compat ? { key, compat: data.__compat } : null;
})
.filter((data) => !!data);
// Add MDN doc links, if any.
const mdnUrls = [];
if (mdnDocsOverrides[id] && mdnDocsOverrides[id].length) {
// If the feature has a doc override, use that.
for (const slug of mdnDocsOverrides[id]) {
const slugParts = slug.split("#");
const hasAnchor = slugParts.length > 1;
const mdnArticleData = mdnInventory.inventory.find((item) => {
return item.frontmatter.slug === (hasAnchor ? slugParts[0] : slug);
});
if (mdnArticleData) {
mdnUrls.push({
title: mdnArticleData.frontmatter.title,
anchor: hasAnchor ? slugParts[1] : null,
url: MDN_URL_ROOT + mdnArticleData.frontmatter.slug + (hasAnchor ? `#${slugParts[1]}` : ""),
});
}
}
} else {
// Otherwise, compute a list of MDN docs based on BCD keys.
for (const { key, mdn_url } of bcdKeysData) {
const mdnArticleData = mdnInventory.inventory.find((item) => {
return item.frontmatter["browser-compat"] === key;
});
if (mdnArticleData) {
mdnUrls.push({
title: mdnArticleData.frontmatter.title,
url: MDN_URL_ROOT + mdnArticleData.frontmatter.slug,
});
} else if (mdn_url) {
mdnUrls.push({
url: mdn_url,
title: mdn_url,
});
}
}
}
feature.mdnUrls = mdnUrls;
// Add standard positions.
feature.standardPositions = standardPositions[id];
// Add origin trials.
feature.originTrials = originTrials[id];
// Add the BCD data to the feature.
feature.bcdData = bcdKeysData;
feature.bcdTags = [...new Set(bcdTags)];
// Add impl_url links, if any, per browser.
const browserImplUrls = Object.keys(browsers).reduce((acc, browserId) => {
acc[browserId] = [];
return acc;
}, {});
for (const { compat } of bcdKeysData) {
for (const browserId in browsers) {
const browserSupport = compat.support[browserId];
if (!browserSupport.version_added && browserSupport.impl_url) {
browserImplUrls[browserId] = [
...new Set([...browserImplUrls[browserId], browserSupport.impl_url]),
];
}
}
}
feature.implUrls = browserImplUrls;
}
// Massage the web-features data so it's more directly useful in our 11ty templates.
for (const id in features) {
const feature = features[id];
augmentFeatureData(id, feature);
}
function getBrowserVersionReleaseDate(browser, version) {
const isBeforeThan = version.startsWith("≤");
const cleanVersion = isBeforeThan ? version.substring(1) : version;
const date = browser.releases.find(
(release) => release.version === cleanVersion
).date;
return {
isBeforeThan,
date
};
}
export default function (eleventyConfig) {
eleventyConfig.addPlugin(EleventyHtmlBasePlugin);
eleventyConfig.addPassthroughCopy("site/assets");
eleventyConfig.addDataExtension("yml,yaml", (contents, filePath) => {
return YAML.parse(contents);
});
eleventyConfig.addShortcode(
"browserVersionRelease",
function (browser, version) {
const { isBeforeThan, date } = getBrowserVersionReleaseDate(browser, version);
return isBeforeThan ? `Released before ${date}` : `Released on ${date}`;
}
);
eleventyConfig.addShortcode("baselineDate", function (dateStr) {
const isBefore = dateStr.startsWith("≤");
if (isBefore) {
return `before ${dateStr.substring(1)}`;
}
return dateStr;
});
eleventyConfig.addGlobalData("versions", async () => {
const { default: webFeaturesPackageJson } = await import(
"./node_modules/web-features/package.json",
{
with: { type: "json" },
}
);
return {
date: new Date().toLocaleDateString(),
webFeatures: webFeaturesPackageJson.version,
bcd: bcd.__meta.version,
};
});
eleventyConfig.addGlobalData("browsers", () => {
return Object.keys(browsers).map((browserId) => {
return {
id: browserId,
name: browsers[browserId].name,
releases: browsers[browserId].releases,
bugTracker: BROWSER_BUG_TRACKERS[browserId],
};
});
});
eleventyConfig.addGlobalData("perGroup", () => {
return groups;
});
eleventyConfig.addGlobalData("latest", () => {
const maxLowFeatures = 5;
const lowFeatures = [];
const maxHighFeatures = 5;
const highFeatures = [];
for (const id in features) {
const feature = features[id];
if (feature.status.baseline === "low") {
lowFeatures.push(feature);
}
if (feature.status.baseline === "high") {
highFeatures.push(feature);
}
}
return {
latestBaselineLow: lowFeatures.sort((a, b) => {
return (
new Date(b.status.baseline_low_date) -
new Date(a.status.baseline_low_date)
);
}).slice(0, maxLowFeatures),
latestBaselineHigh: highFeatures.sort((a, b) => {
return (
new Date(b.status.baseline_high_date) -
new Date(a.status.baseline_high_date)
);
}).slice(0, maxHighFeatures)
};
});
eleventyConfig.addGlobalData("perMonth", () => {
const monthly = new Map();
const ensureMonthEntry = (month) => {
if (!monthly.has(month)) {
const obj = { high: [], low: [], all: new Set() };
for (const browserId in browsers) {
obj[browserId] = [];
}
monthly.set(month, obj);
}
};
const getMonth = (dateStr) => {
if (!dateStr) {
return null;
}
if (dateStr.startsWith("≤")) {
dateStr = dateStr.substring(1);
}
return dateStr.substring(0, 7);
};
const getBaselineHighMonth = (feature) => getMonth(feature.status.baseline_high_date);
const getBaselineLowMonth = (feature) => getMonth(feature.status.baseline_low_date);
const getBrowserSupportMonth = (feature, browserId) => {
const versionSupported = feature.status.support[browserId];
const releaseData = browsers[browserId].releases;
if (!versionSupported) {
return null;
}
const release = releaseData.find(r => r.version === versionSupported);
if (!release) {
return null;
}
return getMonth(release.date);
};
for (const id in features) {
const feature = features[id];
const baselineHighMonth = getBaselineHighMonth(feature);
if (baselineHighMonth) {
ensureMonthEntry(baselineHighMonth);
monthly.get(baselineHighMonth).high.push(feature);
monthly.get(baselineHighMonth).all.add(feature);
}
const baselineLowMonth = getBaselineLowMonth(feature);
if (baselineLowMonth) {
ensureMonthEntry(baselineLowMonth);
monthly.get(baselineLowMonth).low.push(feature);
monthly.get(baselineLowMonth).all.add(feature);
}
for (const browserId in browsers) {
const browserSupportMonth = getBrowserSupportMonth(feature, browserId);
if (browserSupportMonth) {
ensureMonthEntry(browserSupportMonth);
// Only record the feature if it hasn't already been recorded as baseline
// low or high for the same month.
const alreadyRecorded =
monthly
.get(browserSupportMonth)
.high.some((f) => f.id === feature.id) ||
monthly
.get(browserSupportMonth)
.low.some((f) => f.id === feature.id);
if (!alreadyRecorded) {
monthly.get(browserSupportMonth)[browserId].push(feature);
monthly.get(browserSupportMonth).all.add(feature);
}
}
}
}
const now = new Date();
return [...monthly]
.sort((a, b) => {
return new Date(b[0]) - new Date(a[0]);
})
.map((month) => {
const absoluteDate = new Date(month[0]);
const isCurrentMonth =
absoluteDate.getMonth() === now.getMonth() &&
absoluteDate.getFullYear() === now.getFullYear();
return {
date: new Date(month[0]).toLocaleDateString("en-us", {
month: "long",
year: "numeric",
}),
absoluteDate,
// current month is not stable because it is still updating
// RSS feed should not include the current month
// https://github.com/web-platform-dx/web-features-explorer/pull/23
isStableMonth: !isCurrentMonth,
all: [...month[1].all],
features: month[1],
};
});
});
eleventyConfig.addGlobalData("features", () => {
return features;
});
eleventyConfig.addGlobalData("allFeatures", () => {
const all = [];
for (const id in features) {
const feature = features[id];
all.push(feature);
}
return all;
});
eleventyConfig.addGlobalData("allFeaturesAsJSON", () => {
const all = [];
for (const id in features) {
const feature = features[id];
all.push({
description_html: feature.description_html,
id: feature.id,
name: feature.name,
status: feature.status,
});
}
return JSON.stringify(all);
});
eleventyConfig.addGlobalData("widelyAvailableFeatures", () => {
const widelyAvailable = [];
for (const id in features) {
const feature = features[id];
// Baseline features only.
if (feature.status.baseline === "high") {
widelyAvailable.push(feature);
}
}
return widelyAvailable.sort((a, b) => {
// Sort by baseline_high_date, descending, so the most recent is first.
return (
new Date(b.status.baseline_high_date) -
new Date(a.status.baseline_high_date)
);
});
});
eleventyConfig.addGlobalData("limitedAvailabilityFeatures", () => {
const limitedAvailability = [];
for (const id in features) {
const feature = features[id];
// Non-baseline features only.
if (!feature.status.baseline) {
limitedAvailability.push(feature);
}
}
// Sort the features by date, with the most recently updated
// feature first, irrespective of which browser it was updated for.
return limitedAvailability.sort((a, b) => {
let maxADate = 0;
let maxBDate = 0;
// Get all of the browser release dates for the compared features
// and get the most recent.
for (const browserId in browsers) {
const aVersion = a.status.support[browserId];
if (aVersion) {
const date = new Date(getBrowserVersionReleaseDate(browsers[browserId], aVersion).date);
maxADate = Math.max(maxADate, date.getTime());
}
const bVersion = b.status.support[browserId];
if (bVersion) {
const date = new Date(getBrowserVersionReleaseDate(browsers[browserId], bVersion).date);
maxBDate = Math.max(maxBDate, date.getTime());
}
}
return maxBDate - maxADate;
});
});
eleventyConfig.addGlobalData("newlyAvailableFeatures", () => {
const newlyAvailable = [];
for (const id in features) {
const feature = features[id];
// Only baseline low.
if (feature.status.baseline === "low") {
newlyAvailable.push(feature);
}
}
return newlyAvailable.sort((a, b) => {
// Sort by baseline_low_date, descending, so the most recent is first.
return (
new Date(b.status.baseline_low_date) -
new Date(a.status.baseline_low_date)
);
});
});
eleventyConfig.addGlobalData("missingOneBrowserFeatures", () => {
const missingOne = [];
for (const id in features) {
const feature = features[id];
// Only non-baseline features.
if (feature.status.baseline) {
continue;
}
// And, out of those, only those that are missing support in just one browser (engine).
const noSupport = [];
for (const browserId in browsers) {
if (!feature.status.support[browserId]) {
noSupport.push(browserId);
}
}
if (noSupport.length === 1) {
feature.blockedOn = browsers[noSupport[0]].name;
missingOne.push(feature);
}
if (noSupport.length === 2) {
// If one of the two values is a substring of the other, then these are the same engine.
const [first, second] = noSupport;
if (first.includes(second) || second.includes(first)) {
feature.blockedOn = browsers[noSupport[0]].name;
missingOne.push(feature);
}
}
}
// Go over the features we found and add some information about the last browser
// that doesn't yet support the feature.
missingOne.forEach((feature) => {
let mostRecent = null;
const support = feature.status.support;
for (const browserId in support) {
let versionSupported = support[browserId];
if (versionSupported.startsWith("≤")) {
versionSupported = versionSupported.substring(1);
}
// Grab release date string from BCD as it has a more complete list of
// browser releases than the web features data.
const releaseDateStr =
bcd.browsers[browserId]?.releases[versionSupported]?.release_date;
// Some are missing
if (!releaseDateStr) {
continue;
}
const releaseDate = new Date(releaseDateStr);
if (!mostRecent) {
mostRecent = releaseDate;
} else {
if (releaseDate > mostRecent) {
mostRecent = releaseDate;
}
}
}
const today = new Date();
const formatter = new Intl.DateTimeFormat("en", {
year: "numeric",
month: "long",
});
const monthDiff = (older, newer) => {
return (
(newer.getFullYear() - older.getFullYear()) * 12 +
(newer.getMonth() - older.getMonth())
);
};
feature.monthsBlocked = monthDiff(mostRecent, today);
feature.blockedSince = formatter.format(mostRecent);
});
// Sort the features by monthsBlocked, with the most recently
// updated feature first. Most recently updated means the
// shortest blocked time.
return missingOne.sort((a, b) => {
return a.monthsBlocked - b.monthsBlocked;
});
});
// RSS Feed Plugin
eleventyConfig.addPlugin(feedPlugin);
eleventyConfig.addLiquidFilter("dateToRfc3339", feedPlugin.dateToRfc3339);
return {
dir: {
input: "site",
output: "docs",
},
pathPrefix: "/web-features-explorer/",
};
}