Skip to content

Commit cccccf0

Browse files
committed
introduce a document-type helper
1 parent 98f4163 commit cccccf0

2 files changed

Lines changed: 31 additions & 10 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
@@ -17,6 +17,7 @@ const slash = require('slash')
1717
const statsd = require('./statsd')
1818
const readFileContents = require('./read-file-contents')
1919
const getLinkData = require('./get-link-data')
20+
const getDocumentType = require('./get-document-type')
2021
const union = require('lodash/union')
2122

2223
class Page {
@@ -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

0 commit comments

Comments
 (0)