-
Notifications
You must be signed in to change notification settings - Fork 513
/
jsxref.ejs
65 lines (57 loc) · 1.61 KB
/
jsxref.ejs
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
<%
// Inserts a link to a JS API's documentation in the JS Reference.
// Appropriate styling is applied.
//
// Parameters:
//
// $0 - API name
// $1 - name to display (optional)
// $2 - anchor (#xyz) (optional)
// $3 - If set, do not put the text in <code></code>
//
// Examples:
//
// {{jsxref("Date")}}
// {{jsxref("Date.prototype.toString()")}}
// {{jsxref("Global_Objects/Date/toString", "Date.prototype.toString()")}}
//
// Constants.
const baseSlug = 'Web/JavaScript/Reference/';
const localUrl = `/${env.locale}/docs/${baseSlug}`;
const defaultUrl = `/en-US/docs/${baseSlug}`;
const globalObjects = 'Global_Objects';
// Parameters.
const apiName = $0;
const displayName = $1 || '';
const anchor = $2 || '';
const wrap = !$3;
let slug = apiName.replace('()', '').replace('.prototype.', '.');
if (apiName.includes(".") && !apiName.includes("/")) {
// E.g. "Array.filter", but not "Statements/try...catch".
slug = slug.replace('.', '/');
}
let basePath;
if (wiki.hasPage(defaultUrl + slug)) {
// Page exists as is.
basePath = localUrl;
} else if (wiki.hasPage(defaultUrl + globalObjects + '/' + slug)) {
// Page exists in "Global_Objects" section.
basePath = localUrl + globalObjects + '/';
} else {
// Page does not exist.
basePath = localUrl;
}
let href = basePath + web.safeDecodeURIComponent(slug);
if (anchor) {
if (anchor[0].startsWith('#')) {
href += anchor;
} else {
href += "#" + anchor;
}
}
let content = displayName || apiName;
if (wrap) {
content = `<code>${content}</code>`;
}
const link = web.smartLink(href, null, content, apiName, basePath);
%><%- link %>