forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolveMdnSrc.js
More file actions
executable file
·50 lines (34 loc) · 1.23 KB
/
Copy pathresolveMdnSrc.js
File metadata and controls
executable file
·50 lines (34 loc) · 1.23 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
'use strict';
/**
* Replaces relative img src with resourceRoot/src
*/
const tokenUtils = require('../utils/token');
module.exports = function(md) {
md.core.ruler.push('resolve_mdn_src', function(state) {
for (let idx = 0; idx < state.tokens.length; idx++) {
let token = state.tokens[idx];
if (token.type !== 'inline') continue;
for (let i = 0; i < token.children.length; i++) {
let inlineToken = token.children[i];
if (inlineToken.type == 'link_open') {
let href = tokenUtils.attrGet(inlineToken, 'href');
if (href.startsWith('mdn:')) {
let parts = href.slice(4).split('/');
let locale = process.env.NODE_LANG == 'en' ? 'en-US' : process.env.NODE_LANG;
let prefix = `https://developer.mozilla.org/${locale}/docs/Web`;
if (parts[0] == 'js') {
prefix += '/JavaScript/Reference/Global_Objects/';
} else if (parts[0] == 'api') {
prefix += '/API/';
} else {
prefix += '/';
}
parts.shift();
href = prefix + parts.join('/');
tokenUtils.attrReplace(inlineToken, 'href', href);
}
}
}
}
});
};