Skip to content

Commit eca9e98

Browse files
XhmikosRphillipj
authored andcommitted
Move githubLinks() to scripts/plugins/githubLinks.js
1 parent 6f76fa1 commit eca9e98

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

build.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const stylus = require('stylus')
2323
const ncp = require('ncp')
2424
const junk = require('junk')
2525

26+
const githubLinks = require('./scripts/plugins/githubLinks')
2627
const navigation = require('./scripts/plugins/navigation')
2728
const anchorMarkdownHeadings = require('./scripts/plugins/anchor-markdown-headings')
2829
const loadVersions = require('./scripts/load-versions')
@@ -209,33 +210,6 @@ function withPreserveLocale (preserveLocale) {
209210
}
210211
}
211212

212-
// This middleware adds "Edit on GitHub" links to every editable page
213-
function githubLinks (options) {
214-
return (files, m, next) => {
215-
// add suffix (".html" or "/" or "\" for windows) to each part of regex
216-
// to ignore possible occurrences in titles (e.g. blog posts)
217-
const isEditable = /security\.html|about(\/|\\)|docs(\/|\\)|foundation(\/|\\)|get-involved(\/|\\)|knowledge(\/|\\)/
218-
219-
Object.keys(files).forEach((path) => {
220-
if (!isEditable.test(path)) {
221-
return
222-
}
223-
224-
const file = files[path]
225-
const url = `https://github.com/nodejs/nodejs.org/edit/master/locale/${options.locale}/${path.replace('.html', '.md').replace(/\\/g, '/')}`
226-
const editText = options.site.editOnGithub || 'Edit on GitHub'
227-
228-
const contents = file.contents.toString().replace(/<h1(.*?)>(.*?)<\/h1>/, (match, $1, $2) => {
229-
return `<a class="edit-link" href="${url}">${editText}</a> <h1${$1}>${$2}</h1>`
230-
})
231-
232-
file.contents = Buffer.from(contents)
233-
})
234-
235-
next()
236-
}
237-
}
238-
239213
// This function builds the static/css folder for all the Stylus files.
240214
function buildCSS () {
241215
console.log('[stylus] static/css started')

scripts/plugins/githubLinks.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
// This middleware adds "Edit on GitHub" links to every editable page
4+
function githubLinks (options) {
5+
return (files, m, next) => {
6+
// add suffix (".html" or "/" or "\" for Windows) 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+
10+
Object.keys(files).forEach((path) => {
11+
if (!isEditable.test(path)) {
12+
return
13+
}
14+
15+
const file = files[path]
16+
const url = `https://github.com/nodejs/nodejs.org/edit/master/locale/${options.locale}/${path.replace('.html', '.md').replace(/\\/g, '/')}`
17+
const editText = options.site.editOnGithub || 'Edit on GitHub'
18+
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+
})
22+
23+
file.contents = Buffer.from(contents)
24+
})
25+
26+
next()
27+
}
28+
}
29+
30+
module.exports = githubLinks

0 commit comments

Comments
 (0)