Skip to content

Commit cf0873f

Browse files
author
Maledong
authored
improve: add "EditOnGitHub" on every necessary page (#3971)
For the previous version of "Edit on GitHub", we find there're some pages (such as "Download","Index"...ect), it doesn't have this link, because the original code depends on the <h1> generated from "#" in the markable file, but "Download" and "Index" without any "#", so we cannot generate these links. Now the solution is we've generated URL at githubLink.js, with a hiddenfield generated for each body content, and then we read it out by main.js with which we can fulfill the "Edit On GitHub" link with the correct URL. Besides, we've also some other changes: 1. Move "Edit On GitHub" onto the footer. 2. Add "EditOnGithub" instead of hard coding for 'en' site pages. 3. Use 'spe' instead of hard coding for character '/' at 'scripts/plugins/githubLinks.js'. 4. Add "{{{contents}}}" for "Download" and "Download Current", because gitHubLinks.js will save and return the content, we need to show it outside by appending the hidden field.
1 parent b0b170c commit cf0873f

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

layouts/download-current.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div class="download-header">
1212
<h1>{{downloads.headline}}</h1>
1313
</div>
14-
14+
{{{contents}}}
1515
{{> primary-download-matrix version=project.latestVersions.current versionTypeCurrent="true"}}
1616
{{> secondary-download-matrix version=project.latestVersions.current versionTypeCurrent="true"}}
1717

layouts/download.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div class="download-header">
1212
<h1>{{downloads.headline}}</h1>
1313
</div>
14-
14+
{{{contents}}}
1515
{{> primary-download-matrix version=project.latestVersions.lts versionTypeLts="true"}}
1616
{{> secondary-download-matrix version=project.latestVersions.lts versionTypeLts="true"}}
1717

layouts/partials/footer.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<img src="/static/images/openjs_foundation-logo.svg" alt="OpenJS Foundation" width="120" height="38">
1010
</a>
1111
<ul class="list-divider-pipe issue-link">
12+
<li><a id="editOnGitHubLink" href="#">{{site.editOnGithub}}</a></li>
1213
<li><a href="https://github.com/nodejs/node/issues">{{site.reportNodeIssue}}</a></li>
1314
<li><a href="https://github.com/nodejs/nodejs.org/issues">{{site.reportWebsiteIssue}}</a></li>
1415
<li><a href="https://github.com/nodejs/help/issues">{{site.getHelpIssue}}</a></li>

locale/en/site.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"title": "Node.js",
3+
"editOnGithub":"Edit On GitHub",
34
"author": "Node.js",
45
"url": "https://nodejs.org/en/",
56
"locale": "en",

scripts/plugins/githubLinks.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
'use strict'
22

3+
const sep = require('path').sep
4+
// add suffix (".html" or sep for windows test) to each part of regex
5+
// to ignore possible occurrences in titles (e.g. blog posts)
6+
const isEditable = `(security|index).html|(about|download|docs|foundation|get-involved|knowledge)\\${sep}`
7+
const isEditableReg = new RegExp(isEditable)
8+
39
// This middleware adds "Edit on GitHub" links to every editable page
410
function githubLinks (options) {
511
return (files, m, next) => {
6-
// add suffix (".html" or "/") to each part of regex
7-
// to ignore possible occurrences in titles (e.g. blog posts)
8-
const isEditable = /security\.html|about\/|docs\/|foundation\/|get-involved\/|knowledge\//
9-
1012
Object.keys(files).forEach((path) => {
11-
if (!isEditable.test(path)) {
13+
if (!isEditableReg.test(path)) {
1214
return
1315
}
1416

1517
const file = files[path]
16-
const url = `https://github.com/nodejs/nodejs.org/edit/master/locale/${options.locale}/${path.replace('.html', '.md')}`
17-
const editText = options.site.editOnGithub || 'Edit on GitHub'
18+
path = path.replace('.html', '.md').replace(/\\/g, '/')
19+
const url = `https://github.com/nodejs/nodejs.org/edit/master/locale/${options.locale}/${path}`
1820

19-
const contents = file.contents.toString().replace(/<h1(.*?)>(.*?)<\/h1>/, (match, $1, $2) => {
20-
return `<a class="edit-link" href="${url}">${editText}</a> <h1${$1}>${$2}</h1>`
21-
})
21+
const contents = file.contents.toString() +
22+
` <input type = "hidden" id = "editOnGitHubUrl" value="${url}"/> `
2223

2324
file.contents = Buffer.from(contents)
2425
})

static/js/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,17 @@
246246
winText.textContent = winText.textContent.replace(/x(86|64)/, arch)
247247
}
248248
})()
249+
250+
;(function () {
251+
// This function is used to replace the anchor
252+
// link of Edit on GitHub
253+
254+
var editOnGitHubElement = document.getElementById('editOnGitHubLink')
255+
var editOnGitHubUrlElement = document.getElementById('editOnGitHubUrl')
256+
257+
if (editOnGitHubUrlElement) {
258+
editOnGitHubElement.setAttribute('href', editOnGitHubUrlElement.value)
259+
} else {
260+
editOnGitHubElement.parentNode.parentNode.removeChild(editOnGitHubElement.parentNode)
261+
}
262+
})()

0 commit comments

Comments
 (0)