From 52dbd9214468f49fa1abd3278ce7c8ff0ade63f1 Mon Sep 17 00:00:00 2001 From: Jason Etcovitch Date: Thu, 19 Nov 2020 15:53:35 -0500 Subject: [PATCH] Rewrite links in Page.intro with language (#16550) * Add defaultLanguage behavior * Add a million logs because I'm confused * Rewrite intro links * Undo changes to detect-language * Add a test --- lib/page.js | 6 ++++++ tests/unit/page.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/page.js b/lib/page.js index 9927edb32a..c97e49470f 100644 --- a/lib/page.js +++ b/lib/page.js @@ -123,6 +123,12 @@ class Page { async _render (context) { this.intro = await renderContent(this.rawIntro, context) + + // rewrite local links in the intro to include current language code and GHE version if needed + const introHtml = cheerio.load(this.intro) + rewriteLocalLinks(introHtml, context.currentVersion, context.currentLanguage) + this.intro = introHtml('body').html() + this.introPlainText = await renderContent(this.rawIntro, context, { textOnly: true }) this.title = await renderContent(this.rawTitle, context, { textOnly: true, encodeEntities: true }) this.shortTitle = await renderContent(this.shortTitle, context, { textOnly: true, encodeEntities: true }) diff --git a/tests/unit/page.js b/tests/unit/page.js index 876b9ca31a..19164e4d9d 100644 --- a/tests/unit/page.js +++ b/tests/unit/page.js @@ -90,6 +90,21 @@ describe('Page class', () => { expect($(`a[href="/en/${nonEnterpriseDefaultVersion}/articles/about-pull-requests"]`).length).toBeGreaterThan(0) }) + test('rewrites links in the intro to include the current language prefix and version', async () => { + const page = new Page(opts) + page.rawIntro = '[Pull requests](/articles/about-pull-requests)' + const context = { + page: { version: nonEnterpriseDefaultVersion }, + currentVersion: nonEnterpriseDefaultVersion, + currentPath: '/en/github/collaborating-with-issues-and-pull-requests/about-branches', + currentLanguage: 'en' + } + await page.render(context) + const $ = cheerio.load(page.intro) + expect($('a[href="/articles/about-pull-requests"]').length).toBe(0) + expect($(`a[href="/en/${nonEnterpriseDefaultVersion}/articles/about-pull-requests"]`).length).toBeGreaterThan(0) + }) + test('does not rewrite links that include deprecated enterprise release numbers', async () => { const page = new Page({ relativePath: 'admin/enterprise-management/migrating-from-github-enterprise-1110x-to-2123.md',