'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 ``; }; const contents = file.contents .toString() .replace( /