Skip to content

Commit eb44f99

Browse files
committed
add new content migration script for early access updates
1 parent 974b3bb commit eb44f99

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const readFrontmatter = require('../../lib/read-frontmatter')
6+
const earlyAccessDir = path.posix.join(process.cwd(), 'content', 'early-access')
7+
const { sentenceCase } = require('change-case')
8+
9+
updateOrCreateToc(earlyAccessDir)
10+
11+
console.log('Updated Early Access TOCs!')
12+
13+
function updateOrCreateToc (directory) {
14+
const children = fs.readdirSync(directory)
15+
.filter(subpath => !subpath.endsWith('index.md'))
16+
17+
if (!children.length) return
18+
19+
const tocFile = path.posix.join(directory, 'index.md')
20+
21+
let content, data
22+
23+
if (fs.existsSync(tocFile)) {
24+
const matter = readFrontmatter(fs.readFileSync(tocFile, 'utf8'))
25+
content = matter.content
26+
data = matter.data
27+
} else {
28+
content = ''
29+
data = {
30+
title: sentenceCase(path.basename(directory)),
31+
versions: '*',
32+
hidden: true
33+
}
34+
}
35+
36+
data.children = children.map(child => `/${child.replace('.md', '')}`)
37+
const newContents = readFrontmatter.stringify(content, data, { lineWidth: 10000 })
38+
fs.writeFileSync(tocFile, newContents)
39+
40+
children.forEach(child => {
41+
if (child.endsWith('.md')) return
42+
updateOrCreateToc(path.posix.join(directory, child))
43+
})
44+
}

script/content-migrations/remove-map-topics.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ categoryIndexFiles.forEach(categoryIndexFile => {
7979

8080
// Read the article file so we can add a redirect from its old path
8181
const articleContents = frontmatter(fs.readFileSync(newArticlePath, 'utf8'))
82-
addRedirectToFrontmatter(articleContents.data.redirect_from, `${oldTopicDirectory}/${article}`)
82+
83+
if (!articleContents.data.redirect_from) articleContents.data.redirect_from = []
84+
addRedirectToFrontmatter(articleContents.data.redirect_from, `${oldTopicDirectory.replace(/^.*?\/content\//, '/')}/${article}`)
8385

8486
// Write the article with updated frontmatter
8587
fs.writeFileSync(newArticlePath, frontmatter.stringify(articleContents.content.trim(), articleContents.data, { lineWidth: 10000 }))

0 commit comments

Comments
 (0)