Skip to content

Commit

Permalink
temp convenience scripts for short titles (github#19939)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachmari authored Jun 23, 2021
1 parent cc0091f commit aa437c4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"cross-env": "^7.0.3",
"csp-parse": "0.0.2",
"css-loader": "^4.3.0",
"csv-parse": "^4.16.0",
"dedent": "^0.7.0",
"domwaiter": "^1.3.0",
"eslint": "^7.28.0",
Expand Down
22 changes: 22 additions & 0 deletions script/content-migrations/create-csv-of-short-titles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const walk = require('walk-sync')
const readFrontmatter = require('../../lib/read-frontmatter')
const csvFile = path.join(process.cwd(), 'shortTitles.csv')
fs.writeFileSync(csvFile, 'Product,Article Title,Short title,Relative path\n')

const files = walk(path.join(process.cwd(), 'content'), { includeBasePath: true, directories: false })
files.forEach(file => {
const relativeFilePath = file.replace(process.cwd(), '')
const productName = relativeFilePath.split('/')[2]

const fileContent = fs.readFileSync(file, 'utf8')
const { data } = readFrontmatter(fileContent)
const { title, shortTitle } = data

if (title && !shortTitle && title.length > 25) {
fs.appendFileSync(csvFile, `"${productName}","${title}",,${relativeFilePath}\n`)
}
})
66 changes: 66 additions & 0 deletions script/content-migrations/update-short-titles-from-csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const readFrontmatter = require('../../lib/read-frontmatter')
const csv = require('csv-parse')
const { exit } = require('process')

main()

async function main() {
let fileCounter = 0
let csvHeader = []
const csvFileName = 'shortTitles.csv'
const filePath = path.join(process.cwd(), csvFileName)
const reader = fs.createReadStream(filePath)

// Parse each row of the csv
reader
.pipe(csv())
.on('data', (csvData) => {
if (csvHeader.length === 0) {
csvHeader = verifyHeader(csvData)
} else {
if (csvData[3] && csvData[4]) {
updateFrontmatter(csvData)
fileCounter++
}
}
})
.on('end', () => {
console.log(`⭐ Completed updating the shortTitle frontmatter.\nUpdated ${fileCounter} files.`)
})
}

async function updateFrontmatter(csvData) {

const filePath = path.join(process.cwd(), csvData[4])
const fileContent = fs.readFileSync(filePath, 'utf8')
const { content, data } = readFrontmatter(fileContent)
data.shortTitle = csvData[3]
const newContents = readFrontmatter.stringify(content, data, { lineWidth: 10000 })
fs.writeFileSync(filePath, newContents)

}

// Ensure the columns being read out are in the location expected
async function verifyHeader(csvData) {

const csvHeader = []

csvData.forEach(element => {
csvHeader.push(element)
})

if (csvHeader[3] !== 'Short title') {
console.log(`The CSV headers are malformed. Expected to see column 3 contain the header 'Short title'`)
exit(1)
}
if (csvHeader[4] !== 'Relative path') {
console.log(`The CSV headers are malformed. Expected to see column 4 contain the header 'Relative path'`)
exit(1)
}

return csvHeader
}

0 comments on commit aa437c4

Please sign in to comment.