This repository was archived by the owner on Oct 1, 2024. It is now read-only.
forked from discordjs/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
107 lines (91 loc) · 2.81 KB
/
Copy pathutil.js
File metadata and controls
107 lines (91 loc) · 2.81 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
import { resolve as resolveURL } from 'url';
// Highlights an element with highlight.js, delaying until it's loaded
export function hljs(el) {
if (window.hljs) {
window.hljs.highlightBlock(el);
} else {
let attempts = 0;
const interval = setInterval(() => {
if (window.hljs) {
window.hljs.highlightBlock(el);
clearInterval(interval);
} else {
attempts++;
if (attempts >= 50) clearInterval(interval);
}
}, 100);
}
}
// Creates a full URL for a GitHub source view
export function sourceURL(url, tag, path, file, line) {
return resolveURL(url, `${tag}/${path}${file ? `/${file}` : ''}${line ? `#L${line}` : ''}`);
}
// Converts a JSDoc link value to an object of link information
export function parseLink(link, docs) {
const matches = link.match(/\{@link\s+(.+?)(?:\s+(.+?))?\s*\}/i);
if (matches) link = matches[1];
let text = matches ? matches[2] : null;
// Type link
const split = link.split(/(\.|#)/);
if (docs.links[split[0]]) {
return {
text: text || link,
link: typeof docs.links[split[0]] === 'object' ? {
name: docs.links[split[0]].name,
params: docs.links[split[0]].params,
query: { scrollTo: split[1] ? `${split[1] === '.' ? 's-' : ''}${split[2]}` : undefined },
} : docs.links[split[0]],
};
}
// Any link
if (link.match(/^https?:\/\//i)) {
return {
text: text || link,
link: link,
};
}
// Plain text
return { text: text || link };
}
// Converts all JSDoc links to markdown links
export function convertLinks(text, docs, router, route) {
if (!text) return null;
const regex = /\{@link\s+(.+?)(?:\s+(.+?))?\s*\}/gi;
let match;
let newText = '';
let start = 0;
while (match = regex.exec(text)) { // eslint-disable-line no-cond-assign
newText += text.slice(start, match.index);
const parsed = parseLink(match[0], docs);
if (parsed.link) {
let link;
if (typeof parsed.link === 'object') {
if (!parsed.link.params) parsed.link.params = {};
parsed.link.params.source = route.params.source;
parsed.link.params.tag = route.params.tag;
link = router.resolve(parsed.link).href;
} else {
link = parsed.link;
}
newText += `[${parsed.text}](${link})`;
} else {
newText += parsed.text;
}
start = match.index + match[0].length;
}
if (newText) {
newText += text.slice(start);
return newText;
}
return text;
}
// Prefixes a doc item's name with "s-" if static
export function scopedName(item) {
return `${item.scope === 'static' ? 's-' : ''}${item.name}`;
}
// Makes a key for a types array/string
export function typeKey(type) {
return typeof type === 'string' ? type : type.join('-');
}
// Stupid Holder to Improve Tag Switching
export const SHITS = { switching: false };