@webref/xref 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ed/headings/SVG2.json +1 -1
- package/index.js +28 -3
- package/package.json +1 -1
package/ed/headings/SVG2.json
CHANGED
package/index.js
CHANGED
|
@@ -63,7 +63,7 @@ function setupExtracts(type, rootFolder) {
|
|
|
63
63
|
if (entry.alternateIds) {
|
|
64
64
|
for (const id of entry.alternateIds) {
|
|
65
65
|
const url = new URL(entry.href);
|
|
66
|
-
url.hash = id;
|
|
66
|
+
url.hash = encodeURIComponent(id);
|
|
67
67
|
const href = url.toString();
|
|
68
68
|
indexUrl(href, { source: 'headings', entry });
|
|
69
69
|
}
|
|
@@ -100,8 +100,33 @@ export function setup(rootFolder = scriptPath) {
|
|
|
100
100
|
*/
|
|
101
101
|
export function lookup(url) {
|
|
102
102
|
if (!initialized) {
|
|
103
|
-
throw new Error('
|
|
103
|
+
throw new Error('The `lookup()` function was called before `setup()`.');
|
|
104
104
|
}
|
|
105
|
-
|
|
105
|
+
if (!url) {
|
|
106
|
+
throw new Error('The `lookup()` function was called without argument.');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// URL hashes in Webref extracts are percent-encoded. Goal is to support
|
|
110
|
+
// lookup of both percent-encoded and non percent-encoded URLs. To make sure
|
|
111
|
+
// that we end up with what we need, let's force a decode (this will be a
|
|
112
|
+
// no-op if the fragment is not percent-encoded) before we re-encode.
|
|
113
|
+
let parsedUrl;
|
|
114
|
+
try {
|
|
115
|
+
parsedUrl = new URL(url);
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
throw new Error('The `lookup()` function was called with an invalid URL.', err);
|
|
119
|
+
}
|
|
120
|
+
let decodedHash;
|
|
121
|
+
try {
|
|
122
|
+
decodedHash = decodeURIComponent(parsedUrl.hash);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
decodedHash = parsedUrl.hash;
|
|
126
|
+
}
|
|
127
|
+
parsedUrl.hash = encodeURIComponent(decodedHash.replace(/^#/, ''));
|
|
128
|
+
const lookupUrl = parsedUrl.toString();
|
|
129
|
+
|
|
130
|
+
const res = urlIndex[lookupUrl.toString()];
|
|
106
131
|
return structuredClone(res) ?? [];
|
|
107
132
|
}
|