-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathinfo.js
331 lines (310 loc) · 11.1 KB
/
info.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
const cheerio = require("cheerio");
const Parser = require("./parser.js");
const { VALID_LOCALES, Document, Redirect } = require("../../content");
const { m2hSync } = require("../../markdown");
const DUMMY_BASE_URL = "https://example.com";
const MACROS_IN_SUMMARY_TO_IGNORE = new Set([
"apiref",
"jsref",
"compat",
"index",
"page",
"gecko_minversion_inline",
"obsolete_header",
"gecko_minversion_header",
"deprecated_header",
"previous",
"previousmenu",
"previousnext",
"previousmenunext",
"wiki.localize",
"quicklinkswithsubpages",
]);
const MACROS_IN_SUMMARY_TO_REPLACE_WITH_FIRST_ARGUMENT = new Set([
"draft",
"glossary",
"anch",
]);
function repairURL(url) {
// Returns a lowercase URI with common irregularities repaired.
url = url.trim().toLowerCase();
if (!url.startsWith("/")) {
// Ensure the URI starts with a "/".
url = `/${url}`;
}
// Remove redundant forward slashes, like "//".
url = url.replace(/\/{2,}/g, "/");
// Ensure the URI starts with a valid locale.
const maybeLocale = url.split("/")[1];
if (!VALID_LOCALES.has(maybeLocale)) {
if (maybeLocale === "en") {
// Converts URI's like "/en/..." to "/en-us/...".
url = url.replace(`/${maybeLocale}`, "/en-us");
} else {
// Converts URI's like "/web/..." to "/en-us/web/...", or
// URI's like "/docs/..." to "/en-us/docs/...".
url = `/en-us${url}`;
}
}
// Ensure the locale is followed by "/docs".
const [locale, maybeDocs] = url.split("/").slice(1, 3);
if (maybeDocs !== "docs") {
// Converts URI's like "/en-us/web/..." to "/en-us/docs/web/...".
url = url.replace(`/${locale}`, `/${locale}/docs`);
}
return url;
}
const info = {
getPathname(url) {
// This function returns just the pathname of the given "url", removing
// any trailing "/".
return new URL(url, DUMMY_BASE_URL).pathname.replace(/\/$/, "");
},
cleanURL(url, followRedirects = true) {
// This function returns just the lowercase pathname of the given "url",
// removing any trailing "/". The DUMMY_BASE_URL is not important here, since
// we're only after the path of any incoming "url", but it's required by
// the URL constructor when the incoming "url" is relative.
const repairedURL = repairURL(
new URL(url, DUMMY_BASE_URL).pathname.replace(/\/$/, "").toLowerCase()
);
if (followRedirects) {
const resolvedURL = Redirect.resolve(repairedURL);
if (resolvedURL !== repairedURL) {
// The `Redirect.resolve()` returned an actual redirect, and that needs
// to be "repaired" as well.
// Remember, it defaults to the URL you passed in if nothing was found
// in the redirects lookup.
return repairURL(resolvedURL);
}
return resolvedURL;
}
return repairedURL;
},
getDescription(url) {
const cleanedURL = info.cleanURL(url);
let description = `${cleanedURL}`;
if (cleanedURL !== url.toLowerCase()) {
description += ` (derived from "${url}")`;
}
return description;
},
getChildren(url, includeSelf) {
// We don't need "depth" since it's handled dynamically (lazily).
// The caller can keep requesting "subpages" as deep as the
// hierarchy goes, and they'll be provided on-demand.
// IMPORTANT: The list returned does not need to be frozen since
// it's re-created for each caller (so one caller can't mess with
// another), but also should NOT be frozen since some macros sort
// the list in-place.
const page = info.getPageByURL(url, { throwIfDoesNotExist: true });
if (includeSelf) {
return [page];
}
return page.subpages;
},
// TODO
getTranslations(url) {
// function buildTranslationObjects(data) {
// // Builds a list of translation objects suitable for
// // consumption by Kumascript macros, using the translation
// // information from the given "data" as well as the "pageInfoByUri".
// const result = [];
// let rawTranslations = data.translations || [];
// if (!rawTranslations.length && data.translation_of) {
// const englishUri = `/en-US/docs/${data.translation_of}`;
// const englishData = pageInfoByUri.get(englishUri.toLowerCase());
// if (englishData) {
// // First, add the English translation for this non-English locale.
// result.push(
// Object.freeze({
// url: englishUri,
// locale: "en-US",
// title: englishData.title,
// summary: englishData.summary,
// })
// );
// rawTranslations = englishData.translations || [];
// }
// }
// for (const { locale, slug } of rawTranslations) {
// if (locale !== data.locale) {
// // A locale is never a translation of itself.
// const uri = `/${locale}/docs/${slug}`;
// const pageData = pageInfoByUri.get(uri.toLowerCase());
// result.push(
// Object.freeze({
// url: uri,
// locale: locale,
// title: pageData.title,
// summary: pageData.summary,
// })
// );
// }
// }
// return result;
// }
return info.getPageByURL(url, { throwIfDoesNotExist: true }).translations;
},
getPageByURL(
url,
{ throwIfDoesNotExist = false, followRedirects = true } = {}
) {
// Always start by looking it up *without* following redirects.
let document = Document.findByURL(info.cleanURL(url, false));
// Usually, `followRedirects` is disabled if the caller definitely is not
// not interested in following redirects (e.g. listing sub-pages)
if (!document && followRedirects) {
document = Document.findByURL(info.cleanURL(url, true));
}
if (!document) {
// The macros expect an empty object if the URL does not exist, so
// "throwIfDoesNotExist" should only be used within "info" itself.
if (throwIfDoesNotExist) {
throw new Error(
`${info.getDescription(url)} (url: ${url}) does not exist`
);
}
return {};
}
return this.getPage(document);
},
getPage(document) {
if (typeof document === "string") {
console.trace(
"getPage() was called with a string, presumably a URL. " +
"This is deprecated in favor of getPageByURL()."
);
return this.getPageByURL(document);
}
const { locale, slug, title, tags } = document.metadata;
const { rawBody, isMarkdown } = document;
return {
url: document.url,
locale,
slug,
title,
tags: tags || [],
translations: [], // TODO Object.freeze(buildTranslationObjects(data)),
get summary() {
// Back in the old Kuma days we used to store the summary as another piece
// of metadata on each document. It was always available, with any kumascript
// macros rendered out.
// In Yari, this is not possible. We don't duplicate the summary in every
// document. Instead, we extract it from the document when we build it.
// So, to avoid the whole chicken-and-egg problem, instead, we're going to
// try to extract it on-the-fly, from raw HTML or Markdown.
// Note, we can't always use Cheerio here because the `document.rawBody` is
// actually not valid HTML, hence the desperate fall back on regex.
// A lot of times, you'll actually find that the first paragraph isn't
// a <p> tag. But often, in those cases it'll have that `seoSummary`
// <span> tag. Like this for example:
//
// <div><span class="seoSummary">The <code>window.stop()</code> ...
//
// So that's why we always start by looking for that tag first.
let $ = null;
let summary = "";
try {
$ = cheerio.load(isMarkdown ? m2hSync(rawBody) : rawBody);
$("span.seoSummary, .summary").each((i, element) => {
if (!summary) {
const html = $(element)
.html()
.replace(/"/g, '"')
.replace(/'/g, "'");
summary = postProcessSummaryHTMLSnippet(html, document);
}
});
if (!summary) {
// To avoid <p> tags that are inside things like
// `<div class="notecard>`, just remove those divs first.
$("div.notecard, div.note, div.blockIndicator").remove();
$("p").each((i, element) => {
if (!summary) {
const html = $(element)
.html()
.replace(/"/g, '"')
.replace(/'/g, "'");
summary = postProcessSummaryHTMLSnippet(html, document);
}
});
}
} catch (er) {
console.warn(
`Cheerio on document.rawBody (${document.url}) failed to parse`,
er
);
}
return summary;
},
get subpages() {
return Document.findChildren(document.url)
.map((document) => info.getPage(document))
.filter((p) => p && p.url);
},
};
},
hasPage(url) {
return Boolean(Document.findByURL(info.cleanURL(url)));
},
};
/**
* Return the HTML string as if we had it KumaScript rendered. When we extract
* a summary from the raw HTML, we sometimes get things like KS macros in it.
* We can't fully/properly render these because it can easily get us into an
* infinite recursion problem. So we have to make the most of it we can from
* the raw HTML.
*
* @param {string} text The summary as taken from the raw HTML.
*/
function postProcessSummaryHTMLSnippet(text, document) {
if (!text.trim()) {
return "";
}
let tokens;
try {
tokens = Parser.parse(text);
} catch (e) {
// Unfortunate, but not the right time to flag this as a flaw.
console.warn(
`(${document.url}) Unable to Parser.parse() text '${text}' due to error:`,
e
);
return text;
}
let output = "";
for (const token of tokens) {
if (token.type !== "MACRO") {
// If it isn't a MACRO token, it's a TEXT token.
output += token.chars;
continue;
}
if (!token.args.length) {
// Any macro that doesn't have arguments should just be ignored.
// Examples are:
// {{AddonsSidebar }}
// {{ SeeCompatTable() }}
// {{Non-standard_header}}
// {{Non-standard_header()}}
continue;
}
const macroName = token.name.toLowerCase();
// Some macros do have arguments, but there's no good reason to render them out
// for the benefit of a summary, in this context.
if (MACROS_IN_SUMMARY_TO_IGNORE.has(macroName)) {
continue;
}
if (MACROS_IN_SUMMARY_TO_REPLACE_WITH_FIRST_ARGUMENT.has(macroName)) {
output += token.args[0];
} else if (macroName === "interwiki") {
// Include the last one. E.g.
// {{Interwiki("wikipedia","Flynn%27s_taxonomy","classification of computer")}}
output += token.args[token.args.length - 1];
} else {
output += `<code>${token.args[0]}</code>`;
}
}
return output.trim();
}
module.exports = info;