-
Notifications
You must be signed in to change notification settings - Fork 28
/
references.js
91 lines (79 loc) · 2.36 KB
/
references.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
const {JSDOM} = require("jsdom");
const axios = require('axios').default;
const spaceRegex = /\s+/g;
function validateReferences(references, definitions, render) {
const resolvedRefs = [];
const unresolvedRefs = [];
[...new Set(references)].forEach(
ref => {
if(render.includes(`id="term:${ref.replace(spaceRegex, '-').toLowerCase()}"`)) {
resolvedRefs.push(ref);
} else {
unresolvedRefs.push(ref);
}
}
);
if (unresolvedRefs.length > 0 ) {
console.log('Unresolved References: ', unresolvedRefs)
}
const danglingDefs = [];
definitions.forEach(defs => {
let found = defs.some(def => render.includes(`href="#term:${def.replace(spaceRegex, '-').toLowerCase()}"`))
if (!found) {
danglingDefs.push(defs[0]);
}
})
if(danglingDefs.length > 0) {
console.log('Dangling Definitions: ', danglingDefs)
}
}
function findExternalSpecByKey(config, key) {
for (const spec of config.specs) {
if (spec.external_specs) {
for (const externalSpec of spec.external_specs) {
if (externalSpec[key]) {
return externalSpec[key];
}
}
}
}
return null;
}
async function fetchExternalSpecs(spec){
try {
let results = await Promise.all(
spec.external_specs.map(s => {
const url = Object.values(s)[0];
return axios.get(url);
})
);
results = results.map((r, index) => (r.status === 200 ? { [Object.keys(spec.external_specs[index])[0]]: r.data } : null)).filter(r_1 => r_1);
return results.map(r_2 => createNewDLWithTerms(Object.keys(r_2)[0], Object.values(r_2)[0]));
} catch (e) {
return console.log(e);
}
}
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:"]');
terms.forEach(term => {
const modifiedId = `term:${title}:${term.id.split(':')[1]}`;
term.id = modifiedId;
const dt = term.closest('dt');
const dd = dt.nextElementSibling;
newDl.appendChild(dt.cloneNode(true));
if (dd && dd.tagName === 'DD') {
newDl.appendChild(dd.cloneNode(true));
}
});
return newDl.outerHTML;
}
module.exports = {
findExternalSpecByKey,
validateReferences,
fetchExternalSpecs,
createNewDLWithTerms
}