Skip to content

Commit

Permalink
use new module
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahs committed Nov 12, 2020
1 parent 5822fa4 commit 9bfaee1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 77 deletions.
28 changes: 13 additions & 15 deletions lib/check-developer-links.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const cheerio = require('cheerio')
const findPage = require('./find-page')
const findPageInVersion = require('./find-page-in-version')
const renderContent = require('./render-content')
const rewriteLocalLinks = require('./rewrite-local-links')
const getApplicableVersions = require('./get-applicable-versions')
const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
const { getPathWithoutLanguage } = require('./path-utils')
const { getEnterpriseVersionNumber } = require('./patterns')
const { deprecated } = require('./enterprise-server-releases')
const { getEnterpriseVersionNumber, adminProduct } = require('./patterns')
const { deprecated, latest } = require('./enterprise-server-releases')

// internal links will have a language code by the time we're testing them
// we also want to capture same-page anchors (#foo)
Expand Down Expand Up @@ -59,7 +59,15 @@ module.exports = async function checkLinks ($, page, context, version, checkedLi
if (gheVersionInLink && deprecated.includes(gheVersionInLink[1])) continue

// look for linked page
const linkedPage = findPage(link, context.pages, context.redirects, languageCode)
const isDotcomOnly = $(internalLink).attr('class')

// special case for GHES Admin links on dotcom, which are not broken; they go to the latest GHES version
let versionToCheck = version
if (version === nonEnterpriseDefaultVersion && adminProduct.test(link)) {
versionToCheck = `enterprise-server@${latest}`
}

const linkedPage = findPageInVersion(link, context.pages, context.redirects, languageCode, versionToCheck, isDotcomOnly)

if (!linkedPage) {
brokenLinks.links.push({ 'broken link': link, reason: 'linked page not found' })
Expand All @@ -79,16 +87,6 @@ module.exports = async function checkLinks ($, page, context, version, checkedLi
.filter(operation => operation.operationId.startsWith(docsPath))
}

// finding the linked page isn't enough if it's a github.com page; also need to check versions
if (linkedPage.relativePath.startsWith('github')) {
const linkedPageVersions = getApplicableVersions(linkedPage.versions, linkedPage.relativePath)

if (!linkedPageVersions.includes(version) && $(internalLink).attr('class') !== 'dotcom-only') {
brokenLinks.links.push({ 'broken link': link, reason: `${version} not found in linked page versions`, 'linked page': linkedPage.fullPath })
continue
}
}

// collect elements of the page that may contain links
const linkedPageContent = linkedPage.relativePath.includes('graphql/reference/objects')
? linkedPage.markdown + context.graphql.prerenderedObjectsForCurrentVersion.html
Expand Down
28 changes: 13 additions & 15 deletions lib/check-links.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const cheerio = require('cheerio')
const findPage = require('./find-page')
const findPageInVersion = require('./find-page-in-version')
const renderContent = require('./render-content')
const rewriteLocalLinks = require('./rewrite-local-links')
const getApplicableVersions = require('./get-applicable-versions')
const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
const { getPathWithoutLanguage } = require('./path-utils')
const { getEnterpriseVersionNumber } = require('./patterns')
const { deprecated } = require('./enterprise-server-releases')
const { getEnterpriseVersionNumber, adminProduct } = require('./patterns')
const { deprecated, latest } = require('./enterprise-server-releases')

// internal links will have a language code by the time we're testing them
// we also want to capture same-page anchors (#foo)
Expand Down Expand Up @@ -59,21 +59,19 @@ module.exports = async function checkLinks ($, page, context, version, checkedLi
if (gheVersionInLink && deprecated.includes(gheVersionInLink[1])) continue

// look for linked page
const linkedPage = findPage(link, context.pages, context.redirects, languageCode)
const isDotcomOnly = $(internalLink).attr('class')

if (!linkedPage) {
brokenLinks.links.push({ 'broken link': link, reason: 'linked page not found' })
continue
// special case for GHES Admin links on dotcom, which are not broken; they go to the latest GHES version
let versionToCheck = version
if (version === nonEnterpriseDefaultVersion && adminProduct.test(link)) {
versionToCheck = `enterprise-server@${latest}`
}

// finding the linked page isn't enough if it's a github.com page; also need to check versions
if (linkedPage.relativePath.startsWith('github')) {
const linkedPageVersions = getApplicableVersions(linkedPage.versions, linkedPage.relativePath)
const linkedPage = findPageInVersion(link, context.pages, context.redirects, languageCode, versionToCheck, isDotcomOnly)

if (!linkedPageVersions.includes(version) && $(internalLink).attr('class') !== 'dotcom-only') {
brokenLinks.links.push({ 'broken link': link, reason: `${version} not found in linked page versions`, 'linked page': linkedPage.fullPath })
continue
}
if (!linkedPage) {
brokenLinks.links.push({ 'broken link': link, reason: 'linked page not found' })
continue
}

// don't check anchors on developers content
Expand Down
16 changes: 3 additions & 13 deletions lib/liquid-tags/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const Liquid = require('liquid')
const liquid = new Liquid.Engine()
const { getPathWithLanguage } = require('../path-utils')
const LiquidTag = require('./liquid-tag')
const findPage = require('../find-page')
const getApplicableVersions = require('../get-applicable-versions')
const findPageInVersion = require('../find-page-in-version')

// This class supports a set of link tags. Each tag expects one parameter, a language-agnostic href:
//
Expand Down Expand Up @@ -56,20 +55,11 @@ module.exports = class Link extends LiquidTag {
fullPath = getPathWithLanguage(fullPath, ctx.currentLanguage)

// find the page based on the full path
const page = findPage(fullPath, ctx.pages, ctx.redirects, ctx.currentLanguage)

// workaround for localized links that can't be found because they are not in sync
if (!page) return ''

// get versions of the found page
const applicableVersions = getApplicableVersions(page.versions, fullPath)

// check whether the page versions include the current version
const shouldLinkRenderInCurrentVersion = applicableVersions.includes(ctx.currentVersion)
const page = findPageInVersion(fullPath, ctx.pages, ctx.redirects, ctx.currentLanguage, ctx.currentVersion)

// if found page should NOT render in current version, return early with an empty string
// also return if it's a hidden link on a non-hidden page (hidden links on hidden pages are OK)
if (!shouldLinkRenderInCurrentVersion || (page.hidden && !ctx.page.hidden)) {
if (!page || (page.hidden && !ctx.page.hidden)) {
return ''
}

Expand Down
31 changes: 21 additions & 10 deletions lib/path-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,30 @@ function getVersionedPathWithoutLanguage (href, version) {
// example: enterprise-server@2.22 or free-pro-team@latest
let versionFromPath = getVersionStringFromPath(href)

// if versionFromPath doesn't match any current versions, this may be an old
// versioned path that should be converted to new versioned path. Examples:
// OLD: /enterprise/2.22/admin/installation OR /enterprise/admin/installation
// NEW: /enterprise-server@2.22/admin/installation
// OLD: /desktop/installing-and-configuring-github-desktop
// NEW: /free-pro-team@latest/desktop/installing-and-configuring-github-desktop
// if the version found is not a currently supported version...
let productObjectFromPath
if (!Object.keys(allVersions).includes(versionFromPath)) {
href = getNewVersionedPath(href)
versionFromPath = getVersionStringFromPath(href)
// first check if the first segment is instead a current product;
// example: /admin/foo or /desktop/foo
productObjectFromPath = allProducts[versionFromPath]

// if so, add the first supported version for that product to the href
if (productObjectFromPath) {
href = path.join('/', productObjectFromPath.versions[0], href)
versionFromPath = productObjectFromPath.versions[0]
} else {
// otherwise, this may be an old path that should be converted to new path;
// OLD: /enterprise/2.22/admin/installation OR /enterprise/admin/installation
// NEW: /enterprise-server@2.22/admin/installation
href = getNewVersionedPath(href)
versionFromPath = getVersionStringFromPath(href)
}
}

// derive the product from the path (e.g., github or admin) and get corresponding object
const productObjectFromPath = getProductObjectFromPath(href)
// if not previously found, derive the product object from the path (e.g., github or admin)
if (!productObjectFromPath) {
productObjectFromPath = getProductObjectFromPath(href)
}

// if the product's versions don't include the specified version, nothing to change!
if (productObjectFromPath && !productObjectFromPath.versions.includes(version)) {
Expand Down
38 changes: 14 additions & 24 deletions lib/site-tree.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require('path')
const findPage = require('./find-page')
const getApplicableVersions = require('./get-applicable-versions')
const findPageInVersion = require('./find-page-in-version')
const products = Object.values(require('../lib/all-products'))
const { getVersionedPathWithoutLanguage } = require('./path-utils')
const languageCodes = Object.keys(require('./languages'))
Expand Down Expand Up @@ -39,7 +38,10 @@ module.exports = async function buildSiteTree (pages, site, redirects) {
product.href = item.href

// find the product TOC page and get TOC items
const page = findPage(item.href, pages, redirects, languageCode)
const page = findPageInVersion(item.href, pages, redirects, languageCode, version)

// skip if page can't be found in this version
if (!page) return

product.categories = buildCategoriesTree(page.tocItems, item.href, pages, redirects, version, languageCode)

Expand Down Expand Up @@ -69,17 +71,13 @@ function buildCategoriesTree (tocItems, productHref, pages, redirects, version,
category.href = versionedCategoryHref

// find the category TOC page and get its TOC items
const page = findPage(categoryHref, pages, redirects, languageCode)
const page = findPageInVersion(categoryHref, pages, redirects, languageCode, version)

// skip if translated page can't be found
if (!page && languageCode !== 'en') return
// skip if page can't be found in this version
if (!page) return

category.title = page.shortTitle || page.title

if (!getApplicableVersions(page.versions, page.fullPath).includes(version)) {
return
}

// support standalone pages at the category level, like actions/quickstart.md
if (!page.tocItems) {
category.standalone = true
Expand Down Expand Up @@ -120,18 +118,14 @@ function buildMaptopicsTree (tocItems, categoryHref, pages, redirects, version,

// we already have access to the child articles via the category TOC items
// but we still need the page to get the available versions
const page = findPage(maptopicHref, pages, redirects, languageCode)
const page = findPageInVersion(maptopicHref, pages, redirects, languageCode, version)

// skip if translated page can't be found
if (!page && languageCode !== 'en') return
// skip if page can't be found in this version
if (!page) return

// if this is not a maptopic, return early
if (!page.mapTopic) return

if (!getApplicableVersions(page.versions, page.fullPath).includes(version)) {
return
}

const childArticles = getChildArticles(tocItems, item.href)

maptopic.title = page.title
Expand Down Expand Up @@ -163,17 +157,13 @@ function buildArticlesTree (tocItems, categoryHref, pages, redirects, version, l
const versionedArticleHref = getVersionedPathWithoutLanguage(articleHref, version)
article.href = versionedArticleHref

const page = findPage(articleHref, pages, redirects, languageCode)
const page = findPageInVersion(articleHref, pages, redirects, languageCode, version)

// skip if translated page can't be found
if (!page && languageCode !== 'en') return
// skip if page can't be found in this version
if (!page) return

article.title = page.shortTitle || page.title

if (!getApplicableVersions(page.versions, page.fullPath).includes(version)) {
return
}

articleTree[versionedArticleHref] = article
})

Expand Down

0 comments on commit 9bfaee1

Please sign in to comment.