pidgin/nest

bfceef396691
Use hugo refs for resolving links

This will allow us to find broken links easier since if we remove or change
the location of a particular page that's linked in another, then Hugo will
throw a warning/error when this happens if we are using the ref functionality.

The only link that I'm not dealing with here is the one for tips for bug
reports since that page hasn't yet been ported to the new nest site.

Testing Done:
Ran `dev-server.sh` and verified the links that got changed in this PR still worked as intended.

Reviewed at https://reviews.imfreedom.org/r/795/
/**
* Tracks the progress of migrating content
* from the the old site to the new site
*/
/*****************************************************************************
* Imports
*****************************************************************************/
const fs = require('fs')
const path = require('path')
const front = require('front-matter')
/*****************************************************************************
* Set Up
*****************************************************************************/
const filepath = __dirname.replace(/\/tools(\/)?$/, '/hugo/content')
const obsoleteRegex = /\s#(\s+)?obsolete(\s+)?$/i
const mdRegex = /\.md$/
const migrating = []
const obsoleted = []
/*****************************************************************************
* Execution
*****************************************************************************/
const paths = fs
.readFileSync(path.join(__dirname, 'paths.txt'), 'utf8')
.split('\r\n')
// Sift each URL
paths.forEach(path => {
if (obsoleteRegex.test(path)) {
obsoleted.push(path.replace(obsoleteRegex, ''))
} else {
migrating.push(path)
}
})
/** Information on new content */
const nestPages = getMdPaths(path.join(__dirname, '../hugo/content/'))
// discard translated content
.filter(path => !/\.\w+\.md$/.test(path))
.map(path => {
const frontMatter = getFrontMatter(path)
const replaces = castReplacesDataToArray(frontMatter.replaces)
return {
replaces,
path,
pathOnSite: path
// convert to relative file path
.replace(filepath, '')
// remove file extension
.replace(/\.md$/, '')
// remove _index
.replace(/\/_index(\.\w\w)?$/, ''),
}
})
/*****************************************************************************
* Generate Stats
*****************************************************************************/
const covered = new Set()
nestPages.forEach(page => page.replaces.forEach(path => covered.add(path)))
const countOfCovered = migrating.reduce(
(acc, path) => acc + (covered.has(path) ? 1 : 0),
0
)
const percentComplete = ((countOfCovered / migrating.length) * 100).toPrecision(
2
)
const countOfObsolete = paths.length - migrating.length
console.log(`
STATS:
tracking ${paths.length} pages
${countOfCovered}/${migrating.length} (${percentComplete}%) migrated
${countOfObsolete} obsoleted
`)
/*****************************************************************************
* Generate redirection mapping
*****************************************************************************/
// TODO: Generate redirection instructions
/*****************************************************************************
* Helpers
*****************************************************************************/
/**
* Retrieves front matter from markdown files
* @param {string} path path to markdown file
*/
function getFrontMatter(path) {
try {
const data = fs.readFileSync(path, 'utf8')
const { attributes } = front(data)
return attributes
} catch (error) {
console.log(error)
}
}
/**
* creates a list of all markdown files in a directory
* @param {string} directory directory to search for markdown files
*/
function getMdPaths(directory) {
let output = []
let items = fs.readdirSync(directory).map(i => path.join(directory, i))
while (items.length) {
const item = items.pop()
const stat = fs.statSync(item)
if (stat.isDirectory()) {
items.push(...fs.readdirSync(item).map(i => path.join(item, i)))
} else if (stat.isFile() && mdRegex.test(item)) {
output.push(item)
}
}
return output.sort()
}
/**
*
* @param {void|string|string[]} replaces
*/
function castReplacesDataToArray(replaces) {
if (!replaces) {
return []
} else if (typeof replaces === 'string') {
return [replaces]
} else if (Array.isArray(replaces)) {
return replaces
}
throw new Error(
`expected replaces of ${path} to be falsey, string or an array`
)
}