Skip to content

Commit 088aaf6

Browse files
committed
add script and helper module
1 parent 80bdffe commit 088aaf6

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

lib/get-document-type.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = function getDocumentType (relativePath) {
2+
if (!relativePath.endsWith('index.md')) {
3+
return 'article'
4+
}
5+
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'
16+
}
17+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const walk = require('walk-sync')
6+
const yaml = require('js-yaml')
7+
const frontmatter = require('../../lib/read-frontmatter')
8+
const getDocumentType = require('../../lib/get-document-type')
9+
10+
const linkString = /{% [^}]*?link.*? (\/.*?) ?%}/m
11+
const linksArray = new RegExp(linkString.source, 'gm')
12+
13+
// The product order is determined by data/products.yml
14+
const productsFile = path.join(process.cwd(), 'data/products.yml')
15+
const productsYml = yaml.load(fs.readFileSync(productsFile, 'utf8'))
16+
const sortedProductIds = productsYml.productsInOrder.concat('/early-access')
17+
18+
// This script turns `{% link /<link> %} style content into children: [ -/<link> ] frontmatter arrays.
19+
//
20+
// It MUST be run after script/content-migrations/remove-map-topics.js.
21+
//
22+
// NOTE: The results won't work with the TOC handling currently in production, so the results must NOT
23+
// be committed until the updated handling is in place.
24+
25+
walk(path.join(process.cwd(), 'content'), { includeBasePath: true, directories: false })
26+
.filter(file => file.endsWith('index.md'))
27+
.forEach(file => {
28+
const relativePath = file.replace(`${path.join(process.cwd(), 'content/')}`, '')
29+
const documentType = getDocumentType(relativePath)
30+
31+
const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
32+
let newContent = content
33+
34+
if (documentType === 'homepage') {
35+
data.children = sortedProductIds
36+
}
37+
38+
const linkItems = newContent.match(linksArray) || []
39+
40+
// Turn the `{% link /<link> %}` list into an array of /<link>
41+
if (documentType === 'product' || documentType === 'mapTopic') {
42+
data.children = getLinks(linkItems)
43+
}
44+
45+
if (documentType === 'category') {
46+
const childMapTopics = linkItems.filter(item => item.includes('topic_'))
47+
48+
data.children = childMapTopics.length ? getLinks(childMapTopics) : getLinks(linkItems)
49+
}
50+
51+
linkItems.forEach(linkItem => {
52+
newContent = newContent.replace(linkItem, '').trim()
53+
})
54+
55+
newContent = newContent
56+
.replace(/###? Table of Contents\n/i, '')
57+
.replace(/<!-- {2}-->\n/g, '')
58+
59+
// Fix this one weird file
60+
if (relativePath === 'discussions/guides/index.md') {
61+
data.children = [
62+
'/best-practices-for-community-conversations-on-github',
63+
'/finding-discussions-across-multiple-repositories',
64+
'/granting-higher-permissions-to-top-contributors'
65+
]
66+
}
67+
68+
fs.writeFileSync(file, frontmatter.stringify(newContent.trim(), data, { lineWidth: 10000 }))
69+
})
70+
71+
function getLinks (linkItemArray) {
72+
// do a oneoff replacement while mapping
73+
return linkItemArray.map(item => item.match(linkString)[1].replace('/discussions-guides', '/guides'))
74+
}

0 commit comments

Comments
 (0)