From 39bad0ec87bf9c6f8a9236f44c9b088f62d2acf5 Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Thu, 12 Nov 2020 13:48:17 -0500 Subject: [PATCH 1/3] Script to update v3 and v4 links (#16434) * add script * add README to content-migrations subdir --- script/content-migrations/README.md | 7 ++ .../update-developer-site-links.js | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 script/content-migrations/README.md create mode 100755 script/content-migrations/update-developer-site-links.js diff --git a/script/content-migrations/README.md b/script/content-migrations/README.md new file mode 100644 index 000000000000..b27ce9d77405 --- /dev/null +++ b/script/content-migrations/README.md @@ -0,0 +1,7 @@ +# Content migration scripts + +This directory stores scripts that modify content and/or data files. Because +writers are updating content all the time, scripts in here require more +cross-team coordination and planning before they are run. Make sure to consider +whether a script added here also needs to be run on translation files or if we +can wait for the changes to come in organically via Crowdin. diff --git a/script/content-migrations/update-developer-site-links.js b/script/content-migrations/update-developer-site-links.js new file mode 100755 index 000000000000..06adcbb99029 --- /dev/null +++ b/script/content-migrations/update-developer-site-links.js @@ -0,0 +1,73 @@ +#!/usr/bin/env node + +const fs = require('fs') +const path = require('path') +const walk = require('walk-sync') +const frontmatter = require('@github-docs/frontmatter') +const loadPages = require('../../lib/pages') +const patterns = require('../../lib/patterns') +const loadRedirects = require('../../lib/redirects/precompile') +const allVersions = Object.keys(require('../../lib/all-versions')) + +// get all content and data files +const files = ['content', 'data'].map(dir => { + return walk(path.join(process.cwd(), dir), { includeBasePath: true, directories: false }) + .filter(file => file.endsWith('.md') && !file.endsWith('README.md')) +}).flat() + +// match [foo](/v3) and [bar](/v4) Markdown links +const linkRegex = new RegExp('\\(/v[34].*?\\)', 'g') + +main() + +async function main () { + // we need to load the pages so we can get the redirects + const englishPages = (await loadPages()).filter(p => p.languageCode === 'en') + const redirects = await loadRedirects(englishPages) + + for (const file of files) { + const { data, content } = frontmatter(fs.readFileSync(file, 'utf8')) + + const links = content.match(linkRegex) + if (!links) continue + + // remove parentheses: (/v3) -> /v3 + // also remove trailing slash before closing parens if there is one + const devLinks = links + .map(link => link.replace('(', '').replace(/\/?\)/, '')) + + let newContent = content + + for (const devLink of devLinks) { + const [link, fragment] = devLink.split(/\/?#/) + + let redirect = redirects[link] + + if (!redirect) { + console.log(`no redirect found for ${devLink} in ${file}`) + continue + } + + // do some cleanup + redirect = redirect + // remove language code segment + .replace(patterns.getLanguageCode, '') + // remove version segment + .replace(new RegExp(`/(${allVersions.join('|')})`), '') + + // re-add the fragment + const newLink = fragment + ? redirect + '#' + fragment + : redirect + + // first remove any trailing slashes from the old link, + // then replace with the new link + newContent = newContent + .replace(`${devLink}/`, devLink) + .replace(devLink, newLink) + } + + fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 })) + } + console.log('Done!') +} From 8788ab7fc49f63b3eec0d0ed428daef6e88e8e3f Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Thu, 12 Nov 2020 14:53:28 -0500 Subject: [PATCH 2/3] Continue on error during link check (#16442) * continue on error * use failure instead of continue-on-error * Update .github/workflows/check-all-english-links.yml Co-authored-by: James M. Greene * add missing colon Co-authored-by: James M. Greene --- .github/workflows/check-all-english-links.yml | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/.github/workflows/check-all-english-links.yml b/.github/workflows/check-all-english-links.yml index 15ea8c0fed86..6f4b2b46726a 100644 --- a/.github/workflows/check-all-english-links.yml +++ b/.github/workflows/check-all-english-links.yml @@ -18,16 +18,11 @@ jobs: run: npm run build - name: Run script run: script/check-english-links.js > broken_links.md - - name: Check if any broken links + - if: ${{ failure() }} + name: Get title for issue id: check - run: | - if [ "$(grep '0 broken links found' broken_links.md)" ]; then - echo ::set-output name=continue::no - else - echo "::set-output name=continue::yes" - echo "::set-output name=title::$(head -1 broken_links.md)" - fi - - if: ${{ steps.check.outputs.continue == 'yes' }} + run: echo "::set-output name=title::$(head -1 broken_links.md)" + - if: ${{ failure() }} name: Create issue from file uses: peter-evans/create-issue-from-file@35e304e2a12caac08c568247a2cb46ecd0c3ecc5 with: From 5705d334caa89695cf04caa5fb12a8b31b1a3e5e Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Thu, 12 Nov 2020 16:27:29 -0500 Subject: [PATCH 3/3] Header notification updates (#16425) * remove middleware that sets notification flags * move all notification handling into includes/header-notification.html * update tests --- includes/header-notification.html | 36 +++++++++++++++++++++++++---- includes/header.html | 18 --------------- middleware/index.js | 1 - middleware/notices.js | 38 ------------------------------- tests/rendering/header.js | 8 +++---- 5 files changed, 34 insertions(+), 67 deletions(-) delete mode 100644 middleware/notices.js diff --git a/includes/header-notification.html b/includes/header-notification.html index 928ba01d9439..416f6686b19c 100644 --- a/includes/header-notification.html +++ b/includes/header-notification.html @@ -1,11 +1,37 @@ + + +{% if currentLanguage != 'en' and page.relativePath contains '/site-policy/' %} + {% assign translation_notification_type = "true" %} + {% assign translation_notification = site.data.reusables.policies.translation %} + + +{% elsif currentLanguage != 'en' && !languages[currentLanguage].wip %} + {% assign translation_notification_type = "true" %} + {% assign translation_notification = site.data.ui.header.notices.localization_complete %} + + +{% elsif currentLanguage != 'en' && languages[currentLanguage].wip %} + {% assign translation_notification_type = "true" %} + {% assign translation_notification = site.data.ui.header.notices.localization_in_progress %} +{% endif %} + + + + {% if currentVersion == "github-ae@latest" %} -
- {% data ui.header.notices.ghae_silent_launch %} + {% assign release_notification_type = "true" %} + {% assign release_notification = site.data.ui.header.notices.ghae_silent_launch %} +{% endif %} + + +{% if translation_notification_type %} +
+ {{ translation_notification }}
{% endif %} -{% if header_notification %} -
- {{ header_notification }} +{% if release_notification_type %} +
+ {{ release_notification }}
{% endif %} diff --git a/includes/header.html b/includes/header.html index 491c58404406..978031b26b04 100644 --- a/includes/header.html +++ b/includes/header.html @@ -1,22 +1,4 @@
- - {% if currentLanguage != 'en' and page.relativePath contains '/site-policy/' %} - {% assign header_notification_type = "translation_policy" %} - {% assign header_notification = site.data.reusables.policies.translation %} - - {% elsif site.data.ui.header.notices.flags.localization_complete == true %} - {% assign header_notification_type = "localization_complete" %} - {% assign header_notification = site.data.ui.header.notices.localization_complete %} - - {% elsif site.data.ui.header.notices.flags.localization_in_progress == true %} - {% assign header_notification_type = "localization_in_progress" %} - {% assign header_notification = site.data.ui.header.notices.localization_in_progress %} - - {% elsif currentLanguage == 'en' and site.data.ui.header.notices.flags.product_in_progress == true %} - {% assign header_notification_type = "product_in_progress" %} - {% assign header_notification = site.data.ui.header.notices.product_in_progress %} - {% endif %} - {% include header-notification %}
diff --git a/middleware/index.js b/middleware/index.js index 30f7762ddcfb..19ebf17ba665 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -42,7 +42,6 @@ module.exports = function (app) { app.use(require('./early-access-paths')) app.use(require('./early-access-proxy')) app.use(require('./find-page')) - app.use(require('./notices')) app.use(require('./archived-enterprise-versions')) app.use(require('./archived-enterprise-versions-assets')) app.use('/assets', express.static('assets')) diff --git a/middleware/notices.js b/middleware/notices.js deleted file mode 100644 index 5b8e5a1b7da6..000000000000 --- a/middleware/notices.js +++ /dev/null @@ -1,38 +0,0 @@ -const { set } = require('lodash') -const patterns = require('../lib/patterns') -const languages = require('../lib/languages') - -module.exports = (req, res, next) => { - // Skip asset paths - if (patterns.assetPaths.test(req.path)) return next() - - const language = languages[req.language] - - // Set flag that enables `localization_complete` message for no-longer-WIP languages - set( - req.context, - 'site.data.ui.header.notices.flags.localization_complete', - language.code !== 'en' && !language.wip - ) - - // Set flag that enables `localization_in_progress` message for WIP languages - set( - req.context, - 'site.data.ui.header.notices.flags.localization_in_progress', - language.wip - ) - - const currentProduct = req.context.allProducts[req.context.currentProduct] - - // if this is the homepage and no product is chosen yet, return early - if (!currentProduct) return next() - - // Set flag that enables `product_in_progress` message for WIP products - set( - req.context, - 'site.data.ui.header.notices.flags.product_in_progress', - currentProduct.wip - ) - - return next() -} diff --git a/tests/rendering/header.js b/tests/rendering/header.js index 006327409168..4965bc037de8 100644 --- a/tests/rendering/header.js +++ b/tests/rendering/header.js @@ -42,15 +42,13 @@ describe('header', () => { describe('notices', () => { test('displays a "localization in progress" notice for WIP languages', async () => { const $ = await getDOM('/de') - expect($('.header-notifications.localization_in_progress').length).toBe(1) - expect($('.localization_complete').length).toBe(0) + expect($('.header-notifications.translation_notice').length).toBe(1) expect($('.header-notifications a[href="/en"]').length).toBe(1) }) test('displays "complete" notice for non-WIP non-English languages', async () => { const $ = await getDOM('/ja') - expect($('.header-notifications.localization_complete').length).toBe(1) - expect($('.localization_in_progress').length).toBe(0) + expect($('.header-notifications.translation_notice').length).toBe(1) expect($('.header-notifications a[href="/en"]').length).toBe(1) expect($('.header-notifications a[href*="github.com/contact"]').length).toBe(1) }) @@ -62,7 +60,7 @@ describe('header', () => { test('displays translation disclaimer notice on localized site-policy pages', async () => { const $ = await getDOM('/ja/github/site-policy/github-logo-policy') - expect($('.header-notifications.translation_policy a[href="https://github.com/github/site-policy/issues"]').length).toBe(1) + expect($('.header-notifications.translation_notice a[href="https://github.com/github/site-policy/issues"]').length).toBe(1) }) })