forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubLinks.js
More file actions
68 lines (57 loc) · 2.58 KB
/
Copy pathgithubLinks.js
File metadata and controls
68 lines (57 loc) · 2.58 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
'use strict';
const { sep, resolve } = require('path');
const fs = require('fs');
// add suffix (".html" or sep for windows test) to each part of regex
// to ignore possible occurrences in titles (e.g. blog posts)
const isEditable = `(security|index).html|(about|download|docs|foundation|get-involved|knowledge)\\${sep}`;
const isEditableReg = new RegExp(isEditable);
function githubLinks(options) {
return (files, m, next) => {
Object.keys(files).forEach((path) => {
if (!isEditableReg.test(path)) {
return;
}
const file = files[path];
path = path.replace('.html', '.md').replace(/\\/g, '/');
// Step 1: Check each page's URL (generated by "permalinks") really matches
// the real file when converted back to "md".
let currentFilePath = resolve(
__dirname,
`../../locale/${options.locale}/${path}`
);
if (!fs.existsSync(currentFilePath)) {
path = path.replace('/index.md', '.md');
}
// Step 2: Check again each page URL's really exists or not (Because some files aren't
// localized, you can NEVER find the md file under such localization but under 'en').
// we have to create a new file with the given name here:
// E.g: 1. Create a new file named "aaa.md" under "locale/zh-cn/":
// https://github.com/nodejs/nodejs.org/new/main/locale/zh-cn/locale?filename=aaa.md
// E.g: 2. Create a new file with new folders:
// https://github.com/nodejs/nodejs.org/new/main/locale/zh-cn/locale?filename=folder1/folder2.../folderN/filename.md
let url = `https://github.com/nodejs/nodejs.org/edit/main/locale/${options.locale}/${path}`;
currentFilePath = resolve(
__dirname,
`../../locale/${options.locale}/${path}`
);
if (!fs.existsSync(currentFilePath)) {
url = `https://github.com/nodejs/nodejs.org/new/main/locale/${options.locale}/locale?filename=${path}&value=contribute%20your%20translation%20here,for%20more%20you%20can%20see:%20https://github.com/nodejs/nodejs.org/blob/main/TRANSLATION.md`;
}
const editOnGitHubTrans = options.site.editOnGithub || 'Edit on GitHub';
const replCallBack = (match, $1, $2) => {
return `<div class="openjsfoundation-footer-edit">
| <a href="${url}">${editOnGitHubTrans}</a>
</div>`;
};
const contents = file.contents
.toString()
.replace(
/<div class="openjsfoundation-footer-edit"><\/div>/,
replCallBack
);
file.contents = Buffer.from(contents);
});
next();
};
}
module.exports = githubLinks;