-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathreferences.js
More file actions
114 lines (86 loc) · 2.7 KB
/
references.js
File metadata and controls
114 lines (86 loc) · 2.7 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
'use strict';
const { JSDOM } = require('jsdom');
const { slugifyTerm } = require('./src/utils');
function validateReferences(references, definitions, html) {
const unresolvedRefs = [];
for (const ref of new Set(references)) {
if (!html.includes(`id="term:${slugifyTerm(ref)}"`)) {
unresolvedRefs.push(ref);
}
}
const danglingDefs = [];
for (const synonyms of definitions) {
const isReferenced = synonyms.some(definition => {
return html.includes(`href="#term:${slugifyTerm(definition)}"`);
});
if (!isReferenced && synonyms[0]) {
danglingDefs.push(synonyms[0]);
}
}
return {
danglingDefs,
unresolvedRefs
};
}
function logReferenceWarnings(results, logger = console) {
if (results.unresolvedRefs.length > 0) {
logger.log('Unresolved References:', results.unresolvedRefs);
}
if (results.danglingDefs.length > 0) {
logger.log('Dangling Definitions:', results.danglingDefs);
}
}
function findExternalSpecByKey(externalSpecs = [], key) {
for (const entry of externalSpecs) {
if (entry && typeof entry === 'object' && Object.prototype.hasOwnProperty.call(entry, key)) {
return entry[key];
}
}
return null;
}
async function fetchExternalSpecs(externalSpecs = [], options = {}) {
const fetchImpl = options.fetchImpl || globalThis.fetch;
if (typeof fetchImpl !== 'function') {
throw new Error('A fetch implementation is required to resolve external specs.');
}
const results = await Promise.all(externalSpecs.map(async entry => {
const [title, url] = Object.entries(entry || {})[0] || [];
if (!title || !url) {
return '';
}
const response = await fetchImpl(url);
if (!response.ok) {
throw new Error(`Failed to fetch external spec "${title}" from ${url}: ${response.status}`);
}
return createNewDLWithTerms(title, await response.text());
}));
return results.filter(Boolean).join('\n');
}
function createNewDLWithTerms(title, html) {
const dom = new JSDOM(html);
const document = dom.window.document;
const newDl = document.createElement('dl');
newDl.setAttribute('id', title);
const terms = document.querySelectorAll('dt span[id^="term:"]');
for (const term of terms) {
const [, localId] = term.id.split(':');
const dt = term.closest('dt');
const dd = dt ? dt.nextElementSibling : null;
term.id = `term:${title}:${localId}`;
if (dt) {
newDl.appendChild(dt.cloneNode(true));
}
if (dd && dd.tagName === 'DD') {
newDl.appendChild(dd.cloneNode(true));
}
}
return newDl.outerHTML;
}
module.exports = {
createNewDLWithTerms,
fetchExternalSpecs,
findExternalSpecByKey,
logReferenceWarnings,
slugifyTerm,
validateReferences
};