diff --git a/lib/page.js b/lib/page.js index 9927edb32a99..c97e49470f7f 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 876b9ca31af1..19164e4d9d4c 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',