Skip to content

Commit

Permalink
Move githubLinks() to scripts/plugins/githubLinks.js
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR authored and phillipj committed Oct 3, 2019
1 parent 6f76fa1 commit eca9e98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
28 changes: 1 addition & 27 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const stylus = require('stylus')
const ncp = require('ncp')
const junk = require('junk')

const githubLinks = require('./scripts/plugins/githubLinks')
const navigation = require('./scripts/plugins/navigation')
const anchorMarkdownHeadings = require('./scripts/plugins/anchor-markdown-headings')
const loadVersions = require('./scripts/load-versions')
Expand Down Expand Up @@ -209,33 +210,6 @@ function withPreserveLocale (preserveLocale) {
}
}

// This middleware adds "Edit on GitHub" links to every editable page
function githubLinks (options) {
return (files, m, next) => {
// add suffix (".html" or "/" or "\" for windows) to each part of regex
// to ignore possible occurrences in titles (e.g. blog posts)
const isEditable = /security\.html|about(\/|\\)|docs(\/|\\)|foundation(\/|\\)|get-involved(\/|\\)|knowledge(\/|\\)/

Object.keys(files).forEach((path) => {
if (!isEditable.test(path)) {
return
}

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

const contents = file.contents.toString().replace(/<h1(.*?)>(.*?)<\/h1>/, (match, $1, $2) => {
return `<a class="edit-link" href="${url}">${editText}</a> <h1${$1}>${$2}</h1>`
})

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

next()
}
}

// This function builds the static/css folder for all the Stylus files.
function buildCSS () {
console.log('[stylus] static/css started')
Expand Down
30 changes: 30 additions & 0 deletions scripts/plugins/githubLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

// This middleware adds "Edit on GitHub" links to every editable page
function githubLinks (options) {
return (files, m, next) => {
// add suffix (".html" or "/" or "\" for Windows) to each part of regex
// to ignore possible occurrences in titles (e.g. blog posts)
const isEditable = /security\.html|about(\/|\\)|docs(\/|\\)|foundation(\/|\\)|get-involved(\/|\\)|knowledge(\/|\\)/

Object.keys(files).forEach((path) => {
if (!isEditable.test(path)) {
return
}

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

const contents = file.contents.toString().replace(/<h1(.*?)>(.*?)<\/h1>/, (match, $1, $2) => {
return `<a class="edit-link" href="${url}">${editText}</a> <h1${$1}>${$2}</h1>`
})

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

next()
}
}

module.exports = githubLinks

0 comments on commit eca9e98

Please sign in to comment.