forked from ruzyysmartt/json-ld-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
276 lines (252 loc) · 9.64 KB
/
common.js
File metadata and controls
276 lines (252 loc) · 9.64 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
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
/* globals omitTerms, respecConfig, $, require */
/* JSON-LD Working Group common spec JavaScript */
// We should be able to remove terms that are not actually
// referenced from the common definitions
//
// Add class "preserve" to a definition to ensure it is not removed.
//
// the termlist is in a block of class "termlist", so make sure that
// has an ID and put that ID into the termLists array so we can
// interrogate all of the included termlists later.
const termNames = [] ;
const termLists = [] ;
const termsReferencedByTerms = [] ;
function restrictReferences(utils, content) {
const base = document.createElement("div");
base.innerHTML = content;
// New new logic:
//
// 1. build a list of all term-internal references
// 2. When ready to process, for each reference INTO the terms,
// remove any terms they reference from the termNames array too.
const noPreserve = base.querySelectorAll("dfn:not(.preserve)");
for (const item of noPreserve) {
const $t = $(item) ;
const titles = $t.getDfnTitles();
const n = $t.makeID("dfn", titles[0]);
if (n) {
termNames[n] = $t.parent();
}
}
const $container = $(".termlist", base) ;
const containerID = $container.makeID("", "terms") ;
termLists.push(containerID) ;
return (base.innerHTML);
}
// add a handler to come in after all the definitions are resolved
//
// New logic: If the reference is within a 'dl' element of
// class 'termlist', and if the target of that reference is
// also within a 'dl' element of class 'termlist', then
// consider it an internal reference and ignore it.
require(["core/pubsubhub"], (respecEvents) => {
"use strict";
respecEvents.sub('end', (message) => {
if (message === 'core/link-to-dfn') {
// all definitions are linked; find any internal references
const internalTerms = document.querySelectorAll(".termlist a.internalDFN");
for (const item of internalTerms) {
const idref = item.getAttribute('href').replace(/^#/,"") ;
if (termNames[idref]) {
// this is a reference to another term
// what is the idref of THIS term?
const def = item.closest('dd');
if (def) {
const tid = def.previousElementSibling
.querySelector('dfn')
.getAttribute('id');
if (tid) {
if (termsReferencedByTerms[tid] === undefined) termsReferencedByTerms[tid] = [];
termsReferencedByTerms[tid].push(idref);
}
}
}
}
// clearRefs is recursive. Walk down the tree of
// references to ensure that all references are resolved.
const clearRefs = (theTerm) => {
if (termsReferencedByTerms[theTerm] ) {
for (const item of termsReferencedByTerms[theTerm]) {
if (termNames[item]) {
delete termNames[item];
clearRefs(item);
}
}
};
// make sure this term doesn't get removed
if (termNames[theTerm]) {
delete termNames[theTerm];
}
};
// now termsReferencedByTerms has ALL terms that
// reference other terms, and a list of the
// terms that they reference
const internalRefs = document.querySelectorAll("a.internalDFN");
for (const item of internalRefs) {
const idref = item.getAttribute('href').replace(/^#/,"") ;
// if the item is outside the term list
if (!item.closest('dl.termlist')) {
clearRefs(idref);
}
}
// delete any terms that were not referenced.
for (const term in termNames) {
const $p = $("#"+term);
if ($p.length > 0) {
const tList = $p.getDfnTitles();
$p.parent().next().remove(); // remove dd
$p.remove(); // remove dt
for (const item of tList) {
if (respecConfig.definitionMap[item]) {
delete respecConfig.definitionMap[item];
}
}
}
}
}
});
});
/*
*
* Replace github.io references to /TR references.
* The issue is as follows: when several specs are developed in parallel, it is a good idea
* to use, for mutual references, the github.io URI-s. That ensures that the editors' drafts are always
* correct in terms of mutual references.
*
* However, when publishing the documents, all those references must be exchanged against the final, /TR
* URI-s. That process, when done manually, is boring and error prone. This script solves the issue:
*
* * Create a separate file with the 'conversions' array. See, e.g., https://github.com/w3c/csvw/blob/gh-pages/local-biblio.js
* for an example.
* * Include a reference to that file and this to the respec code, after the inclusion of respec. E.g.:
* ```
* <script class="remove" src="../local-biblio.js"></script>
* <script class="remove" src="https://www.w3.org/Tools/respec/respec-w3c-common"></script>
* <script class="remove" src="../replace-ed-uris.js"></script>
* ```
*
* This function will be automatically executed when the respec source is saved in an (X)HTML file.
* Note that
*
* * Links in the header part will *not* be changed. That part is usually generated automatically, and the reference to the
* editor's draft must stay unchanged
* * The text content of an <a> element will also be converted (if needed). This means that the reference list may also
* use include the github.io address (as it should...)
*
*/
require(["core/pubsubhub"], (respecEvents) => {
"use strict";
respecEvents.sub('beforesave', (documentElement) => {
for (const anchor of document.querySelectorAll("a[href]")) {
const dd = anchor.closest('dd');
// Don't replace specific anchors
if (dd) {
const dt = dd.previousElementSibling;
if (dt.textContent.match(/Latest editor|Test suite|Implementation report/)) return;
}
if (anchor.closest('section.preserve')) return;
if (anchor.href === undefined) return;
for (const toReplace in jsonld.conversions) {
if (anchor.href.indexOf(toReplace) !== -1) {
const replacement = jsonld.conversions[toReplace];
const newHref = anchor.href.replace(toReplace, replacement);
anchor.setAttribute('href', newHref);
if (anchor.textContent.indexOf(toReplace) !== -1) {
anchor.textContent = anchor.textContent.replace(toReplace, replacement);
}
}
}
}
});
});
/*
* Implement tabbed examples.
*/
require(["core/pubsubhub"], (respecEvents) => {
"use strict";
respecEvents.sub('end-all', (documentElement) => {
// Add playground links
for (const link of document.querySelectorAll("a.playground")) {
let pre;
if (link.dataset.resultFor) {
// Referenced pre element
pre = document.querySelector(link.dataset.resultFor + ' > pre');
} else {
// First pre element of aside
pre = link.closest("aside").querySelector("pre");
}
const content = unComment(document, pre.textContent)
.replace(/\*\*\*\*/g, '')
.replace(/####([^#]*)####/g, '');
link.setAttribute('aria-label', 'playground link');
link.textContent = "Open in playground";
// startTab defaults to "expand"
const linkQueryParams = {
startTab: "tab-expand",
"json-ld": content
}
if (link.dataset.compact !== undefined) {
linkQueryParams.startTab = "tab-" + "compacted";
linkQueryParams.context = '{}';
}
if (link.dataset.flatten !== undefined) {
linkQueryParams.startTab = "tab-" + "flattened";
linkQueryParams.context = '{}';
}
if (link.dataset.frame !== undefined) {
linkQueryParams.startTab = "tab-" + "framed";
const frameContent = unComment(document, document.querySelector(link.dataset.frame + ' > pre').textContent)
.replace(/\*\*\*\*/g, '')
.replace(/####([^#]*)####/g, '');
linkQueryParams.frame = frameContent;
}
// Set context
if (link.dataset.context) {
const contextContent = unComment(document, document.querySelector(link.dataset.context + ' > pre').textContent)
.replace(/\*\*\*\*/g, '')
.replace(/####([^#]*)####/g, '');
linkQueryParams.context = contextContent;
}
link.setAttribute('href',
'https://json-ld.org/playground-dev/#' +
Object.keys(linkQueryParams).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(linkQueryParams[k])}`)
.join('&'));
}
// Add highlighting and remove comment from pre elements
for (const pre of document.querySelectorAll("pre")) {
// First pre element of aside
const content = pre.innerHTML
.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="hl-bold">$1</span>')
.replace(/####([^#]*)####/g, '<span class="comment">$1</span>');
pre.innerHTML = content;
}
});
});
function _esc(s) {
return s.replace(/&/g,'&')
.replace(/>/g,'>')
.replace(/"/g,'"')
.replace(/</g,'<');
}
function reindent(text) {
// TODO: use trimEnd when Edge supports it
const lines = text.trimRight().split("\n");
while (lines.length && !lines[0].trim()) {
lines.shift();
}
const indents = lines.filter(s => s.trim()).map(s => s.search(/[^\s]/));
const leastIndent = Math.min(...indents);
return lines.map(s => s.slice(leastIndent)).join("\n");
}
function updateExample(doc, content) {
// perform transformations to make it render and prettier
return _esc(reindent(unComment(doc, content)));
}
function unComment(doc, content) {
// perform transformations to make it render and prettier
return content
.replace(/<!--/, '')
.replace(/-->/, '')
.replace(/< !--/g, '<!--')
.replace(/-- >/g, '-->');
}