Skip to content

Commit 2f7bfc1

Browse files
committed
some lib updates
1 parent 2a12a3c commit 2f7bfc1

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

lib/get-document-type.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1+
// This function derives the document type from the *relative path* segment length,
2+
// where a relative path refers to the content path starting with the product dir.
3+
// For example: actions/index.md or github/getting-started-with-github/quickstart.md.
14
module.exports = function getDocumentType (relativePath) {
5+
// A non-index file is ALWAYS considered an article in this approach,
6+
// even if it's at the category level (like actions/quickstart.md)
27
if (!relativePath.endsWith('index.md')) {
38
return 'article'
49
}
510

6-
// Derive the document type from the path segment length
7-
switch (relativePath.split('/').length) {
8-
case 1:
9-
return 'homepage'
10-
case 2:
11-
return 'product'
12-
case 3:
13-
return 'category'
14-
case 4:
15-
return 'mapTopic'
11+
const segmentLength = relativePath.split('/').length
12+
13+
// Early Access has an extra tree segment, so it has a different number of segments.
14+
const isEarlyAccess = relativePath.startsWith('early-access')
15+
16+
const publicDocs = {
17+
1: 'homepage',
18+
2: 'product',
19+
3: 'category',
20+
4: 'mapTopic'
1621
}
22+
23+
const earlyAccessDocs = {
24+
1: 'homepage',
25+
2: 'early-access',
26+
3: 'product',
27+
4: 'category',
28+
5: 'mapTopic'
29+
}
30+
31+
return isEarlyAccess
32+
? earlyAccessDocs[segmentLength]
33+
: publicDocs[segmentLength]
1734
}

lib/page.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const statsd = require('./statsd')
1818
const readFileContents = require('./read-file-contents')
1919
const getLinkData = require('./get-link-data')
2020
const union = require('lodash/union')
21+
const getDocumentType = require('./get-document-type')
2122

2223
class Page {
2324
static async init (opts) {
@@ -76,6 +77,9 @@ class Page {
7677
this.introLinks.rawOverview = this.introLinks.overview
7778
}
7879

80+
// Is this the Homepage or a Product, Category, Topic, or Article?
81+
this.documentType = getDocumentType(this.relativePath)
82+
7983
// Get array of versions that the page is available in for fast lookup
8084
this.applicableVersions = getApplicableVersions(this.versions, this.fullPath)
8185

lib/redirects/add-redirect-to-frontmatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// add a new redirect string to redirect_from frontmatter
22

33
module.exports = function addRedirectToFrontmatter (redirectFromData, newRedirectString) {
4-
if (Array.isArray(redirectFromData)) {
4+
if (Array.isArray(redirectFromData) && !redirectFromData.includes(newRedirectString)) {
55
redirectFromData.push(newRedirectString)
66
} else if (typeof redirectFromData === 'string') {
77
redirectFromData = [redirectFromData]

0 commit comments

Comments
 (0)