Skip to content

Commit aa437c4

Browse files
authored
temp convenience scripts for short titles (github#19939)
1 parent cc0091f commit aa437c4

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"cross-env": "^7.0.3",
125125
"csp-parse": "0.0.2",
126126
"css-loader": "^4.3.0",
127+
"csv-parse": "^4.16.0",
127128
"dedent": "^0.7.0",
128129
"domwaiter": "^1.3.0",
129130
"eslint": "^7.28.0",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const walk = require('walk-sync')
6+
const readFrontmatter = require('../../lib/read-frontmatter')
7+
const csvFile = path.join(process.cwd(), 'shortTitles.csv')
8+
fs.writeFileSync(csvFile, 'Product,Article Title,Short title,Relative path\n')
9+
10+
const files = walk(path.join(process.cwd(), 'content'), { includeBasePath: true, directories: false })
11+
files.forEach(file => {
12+
const relativeFilePath = file.replace(process.cwd(), '')
13+
const productName = relativeFilePath.split('/')[2]
14+
15+
const fileContent = fs.readFileSync(file, 'utf8')
16+
const { data } = readFrontmatter(fileContent)
17+
const { title, shortTitle } = data
18+
19+
if (title && !shortTitle && title.length > 25) {
20+
fs.appendFileSync(csvFile, `"${productName}","${title}",,${relativeFilePath}\n`)
21+
}
22+
})
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 csv = require('csv-parse')
7+
const { exit } = require('process')
8+
9+
main()
10+
11+
async function main() {
12+
let fileCounter = 0
13+
let csvHeader = []
14+
const csvFileName = 'shortTitles.csv'
15+
const filePath = path.join(process.cwd(), csvFileName)
16+
const reader = fs.createReadStream(filePath)
17+
18+
// Parse each row of the csv
19+
reader
20+
.pipe(csv())
21+
.on('data', (csvData) => {
22+
if (csvHeader.length === 0) {
23+
csvHeader = verifyHeader(csvData)
24+
} else {
25+
if (csvData[3] && csvData[4]) {
26+
updateFrontmatter(csvData)
27+
fileCounter++
28+
}
29+
}
30+
})
31+
.on('end', () => {
32+
console.log(`⭐ Completed updating the shortTitle frontmatter.\nUpdated ${fileCounter} files.`)
33+
})
34+
}
35+
36+
async function updateFrontmatter(csvData) {
37+
38+
const filePath = path.join(process.cwd(), csvData[4])
39+
const fileContent = fs.readFileSync(filePath, 'utf8')
40+
const { content, data } = readFrontmatter(fileContent)
41+
data.shortTitle = csvData[3]
42+
const newContents = readFrontmatter.stringify(content, data, { lineWidth: 10000 })
43+
fs.writeFileSync(filePath, newContents)
44+
45+
}
46+
47+
// Ensure the columns being read out are in the location expected
48+
async function verifyHeader(csvData) {
49+
50+
const csvHeader = []
51+
52+
csvData.forEach(element => {
53+
csvHeader.push(element)
54+
})
55+
56+
if (csvHeader[3] !== 'Short title') {
57+
console.log(`The CSV headers are malformed. Expected to see column 3 contain the header 'Short title'`)
58+
exit(1)
59+
}
60+
if (csvHeader[4] !== 'Relative path') {
61+
console.log(`The CSV headers are malformed. Expected to see column 4 contain the header 'Relative path'`)
62+
exit(1)
63+
}
64+
65+
return csvHeader
66+
}

0 commit comments

Comments
 (0)