Skip to content

Commit bec9cfd

Browse files
committed
run script on translations
1 parent 2ff36db commit bec9cfd

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

script/content-migrations/update-tocs.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const walk = require('walk-sync')
66
const yaml = require('js-yaml')
77
const frontmatter = require('../../lib/read-frontmatter')
88
const getDocumentType = require('../../lib/get-document-type')
9+
const languages = require('../../lib/languages')
910

1011
const linkString = /{% [^}]*?link.*? (\/.*?) ?%}/m
1112
const linksArray = new RegExp(linkString.source, 'gm')
@@ -22,13 +23,22 @@ const sortedProductIds = productsYml.productsInOrder.concat('/early-access')
2223
// NOTE: The results won't work with the TOC handling currently in production, so the results must NOT
2324
// be committed until the updated handling is in place.
2425

25-
walk(path.join(process.cwd(), 'content'), { includeBasePath: true, directories: false })
26+
const walkOpts = {
27+
includeBasePath: true,
28+
directories: false
29+
}
30+
31+
const fullDirectoryPaths = Object.values(languages).map(langObj => path.join(process.cwd(), langObj.dir, 'content'))
32+
const indexFiles = fullDirectoryPaths.map(fullDirectoryPath => walk(fullDirectoryPath, walkOpts)).flat()
2633
.filter(file => file.endsWith('index.md'))
27-
.forEach(file => {
28-
const relativePath = file.replace(`${path.join(process.cwd(), 'content/')}`, '')
34+
35+
indexFiles
36+
.forEach(indexFile => {
37+
const relativePath = indexFile.replace(/^.+content\//, '')
38+
console.log(relativePath)
2939
const documentType = getDocumentType(relativePath)
3040

31-
const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
41+
const { data, content } = frontmatter(fs.readFileSync(indexFile, 'utf8'))
3242
let newContent = content
3343

3444
if (documentType === 'homepage') {
@@ -37,7 +47,7 @@ walk(path.join(process.cwd(), 'content'), { includeBasePath: true, directories:
3747

3848
const linkItems = newContent.match(linksArray) || []
3949

40-
// Turn the `{% link /<link> %}` list into an array of /<link>
50+
// Turn the `{% link /<link> %}` list into an array of /<link> items
4151
if (documentType === 'product' || documentType === 'mapTopic') {
4252
data.children = getLinks(linkItems)
4353
}
@@ -65,7 +75,7 @@ walk(path.join(process.cwd(), 'content'), { includeBasePath: true, directories:
6575
]
6676
}
6777

68-
fs.writeFileSync(file, frontmatter.stringify(newContent.trim(), data, { lineWidth: 10000 }))
78+
fs.writeFileSync(indexFile, frontmatter.stringify(newContent.trim(), data, { lineWidth: 10000 }))
6979
})
7080

7181
function getLinks (linkItemArray) {

0 commit comments

Comments
 (0)