forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
temp convenience scripts for short titles (github#19939)
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |