Skip to content

Commit 9977a7e

Browse files
committed
add scripts
1 parent 4ffcff3 commit 9977a7e

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const walk = require('walk-sync')
6+
7+
const relativeRefRegex = /\/[a-zA-Z0-9-]+/g
8+
const linkString = /{% [^}]*?link.*? \/(.*?) ?%}/m
9+
const linksArray = new RegExp(linkString.source, 'gm')
10+
11+
const fullDirectoryPath = path.join(process.cwd(), '/content')
12+
const files = walk(fullDirectoryPath, {
13+
includeBasePath: true,
14+
directories: false
15+
})
16+
17+
files.forEach(file => {
18+
if (path.basename(file) !== 'index.md') return
19+
20+
let fileContent = fs.readFileSync(file, 'utf-8')
21+
// find array of TOC link strings
22+
const rawItems = fileContent.match(linksArray)
23+
if (!rawItems || !rawItems[0].includes('topic_link_in_list')) return
24+
25+
const pageToc = {}
26+
let currentTopic = ''
27+
28+
// Create an object of topics and articles
29+
rawItems.forEach(tocItem => {
30+
const relativePath = tocItem.match(relativeRefRegex).pop().replace('/', '')
31+
if (tocItem.includes('topic_link_in_list')) {
32+
currentTopic = relativePath
33+
pageToc[relativePath] = []
34+
} else {
35+
const tmpArray = pageToc[currentTopic]
36+
tmpArray.push(relativePath)
37+
pageToc[currentTopic] = tmpArray
38+
}
39+
})
40+
for (const topic in pageToc) {
41+
const oldTopicDirectory = path.dirname(file)
42+
const newTopicDirectory = path.join(oldTopicDirectory, topic)
43+
const topicFile = path.join(oldTopicDirectory, `${topic}.md`)
44+
45+
if (!fs.existsSync(newTopicDirectory)) fs.mkdirSync(newTopicDirectory)
46+
47+
let topicContent = fs.readFileSync(topicFile, 'utf-8')
48+
topicContent = topicContent.replace('mapTopic: true\n', '')
49+
50+
const articles = pageToc[topic]
51+
52+
articles.forEach(article => {
53+
fs.renameSync(`${oldTopicDirectory}/${article}.md`, `${newTopicDirectory}/${article}.md`)
54+
topicContent = topicContent + `{% link_with_intro /${article} %}\n`
55+
fileContent = fileContent.replace(`/{% link_in_list /${article}`, `/{% link_in_list /${newTopicDirectory}/${article}`)
56+
})
57+
fs.writeFileSync(`${newTopicDirectory}/index.md`, topicContent)
58+
fs.unlinkSync(topicFile)
59+
}
60+
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
walk(path.join(process.cwd(), 'content'), { includeBasePath: true, directories: false })
19+
.filter(file => file.endsWith('index.md'))
20+
.forEach(file => {
21+
const relativePath = file.replace(`${path.join(process.cwd(), 'content/')}`, '')
22+
const documentType = getDocumentType(relativePath)
23+
24+
const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
25+
let newContent = content
26+
27+
if (documentType === 'homepage') {
28+
data.children = sortedProductIds
29+
}
30+
31+
const linkItems = newContent.match(linksArray) || []
32+
33+
// Turn the `{% link /<link> %}` list into an array of /<link>
34+
if (documentType === 'product' || documentType === 'mapTopic') {
35+
data.children = getLinks(linkItems)
36+
}
37+
38+
if (documentType === 'category') {
39+
const childMapTopics = linkItems.filter(item => item.includes('topic_'))
40+
41+
data.children = childMapTopics.length ? getLinks(childMapTopics) : getLinks(linkItems)
42+
}
43+
44+
linkItems.forEach(linkItem => {
45+
newContent = newContent.replace(linkItem, '').trim()
46+
})
47+
48+
newContent = newContent.replace(/###? Table of Contents\n/i, '')
49+
50+
// Fix this one weird file
51+
if (file.relativePath === 'content/discussions/guides/index.md') {
52+
data.children = [
53+
'/best-practices-for-community-conversations-on-github',
54+
'/finding-discussions-across-multiple-repositories',
55+
'/granting-higher-permissions-to-top-contributors'
56+
]
57+
}
58+
59+
fs.writeFileSync(file, frontmatter.stringify(newContent.trim(), data, { lineWidth: 10000 }))
60+
})
61+
62+
function getLinks (linkItemArray) {
63+
// do a oneoff replacement while mapping
64+
return linkItemArray.map(item => item.match(linkString)[1].replace('/discussions-guides', '/guides'))
65+
}

0 commit comments

Comments
 (0)