forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-map-topics.js
executable file
·121 lines (96 loc) · 4.17 KB
/
remove-map-topics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
import fs from 'fs'
import path from 'path'
import walk from 'walk-sync'
import stripHtmlComments from 'strip-html-comments'
import languages from '../../lib/languages.js'
import frontmatter from '../../lib/read-frontmatter.js'
import addRedirectToFrontmatter from '../helpers/add-redirect-to-frontmatter.js'
const relativeRefRegex = /\/[a-zA-Z0-9-]+/g
const linkString = /{% [^}]*?link.*? \/(.*?) ?%}/m
const linksArray = new RegExp(linkString.source, 'gm')
const walkOpts = {
includeBasePath: true,
directories: false,
}
// We only want category TOC files, not product TOCs.
const categoryFileRegex = /content\/[^/]+?\/[^/]+?\/index.md/
const fullDirectoryPaths = Object.values(languages).map((langObj) =>
path.join(process.cwd(), langObj.dir, 'content')
)
const categoryIndexFiles = fullDirectoryPaths
.map((fullDirectoryPath) => walk(fullDirectoryPath, walkOpts))
.flat()
.filter((file) => categoryFileRegex.test(file))
categoryIndexFiles.forEach((categoryIndexFile) => {
let categoryIndexContent = fs.readFileSync(categoryIndexFile, 'utf8')
if (categoryIndexFile.endsWith('github/getting-started-with-github/index.md')) {
categoryIndexContent = stripHtmlComments(categoryIndexContent.replace(/\n<!--/g, '<!--'))
}
// find array of TOC link strings
const rawItems = categoryIndexContent.match(linksArray)
if (!rawItems || !rawItems[0].includes('topic_link_in_list')) return
const pageToc = {}
let currentTopic = ''
// Create an object of topics and articles
rawItems.forEach((tocItem) => {
const relativePath = tocItem.match(relativeRefRegex).pop().replace('/', '')
if (tocItem.includes('topic_link_in_list')) {
currentTopic = relativePath
pageToc[relativePath] = []
} else {
const tmpArray = pageToc[currentTopic]
tmpArray.push(relativePath)
pageToc[currentTopic] = tmpArray
}
})
for (const topic in pageToc) {
const oldTopicDirectory = path.dirname(categoryIndexFile)
const newTopicDirectory = path.join(oldTopicDirectory, topic)
const oldTopicFile = path.join(oldTopicDirectory, `${topic}.md`)
// Some translated category TOCs may be outdated and contain incorrect links
if (!fs.existsSync(oldTopicFile)) continue
if (!fs.existsSync(newTopicDirectory)) fs.mkdirSync(newTopicDirectory)
const { data, content } = frontmatter(fs.readFileSync(oldTopicFile, 'utf8'))
delete data.mapTopic
let topicContent = content
const articles = pageToc[topic]
articles.forEach((article) => {
// Update the new map topic index file content
topicContent = topicContent + `{% link_with_intro /${article} %}\n`
// Update the category index file content
categoryIndexContent = categoryIndexContent.replace(
`{% link_in_list /${article}`,
`{% link_in_list /${topic}/${article}`
)
// Early return if the article doesn't exist (some translated category TOCs may be outdated and contain incorrect links)
if (!fs.existsSync(`${oldTopicDirectory}/${article}.md`)) return
// Move the file under the new map topic directory
const newArticlePath = `${newTopicDirectory}/${article}.md`
fs.renameSync(`${oldTopicDirectory}/${article}.md`, newArticlePath)
// Read the article file so we can add a redirect from its old path
const articleContents = frontmatter(fs.readFileSync(newArticlePath, 'utf8'))
if (!articleContents.data.redirect_from) articleContents.data.redirect_from = []
addRedirectToFrontmatter(
articleContents.data.redirect_from,
`${oldTopicDirectory.replace(/^.*?\/content\//, '/')}/${article}`
)
// Write the article with updated frontmatter
fs.writeFileSync(
newArticlePath,
frontmatter.stringify(articleContents.content.trim(), articleContents.data, {
lineWidth: 10000,
})
)
})
// Write the map topic index file
fs.writeFileSync(
`${newTopicDirectory}/index.md`,
frontmatter.stringify(topicContent.trim(), data, { lineWidth: 10000 })
)
// Write the category index file
fs.writeFileSync(categoryIndexFile, categoryIndexContent)
// Delete the old map topic
fs.unlinkSync(oldTopicFile)
}
})