2020-09-27 15:10:11 +03:00
|
|
|
const path = require('path')
|
|
|
|
const walk = require('walk-sync').entries
|
|
|
|
const Page = require('./page')
|
|
|
|
const languages = require('./languages')
|
2020-12-14 19:44:09 +03:00
|
|
|
const { mapLimit } = require('async')
|
2020-12-14 18:56:05 +03:00
|
|
|
|
|
|
|
const FILE_READ_LIMIT = 100
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2020-12-04 01:31:10 +03:00
|
|
|
async function loadPageList () {
|
2020-09-27 15:10:11 +03:00
|
|
|
// load english pages
|
|
|
|
const englishPath = path.join(__dirname, '..', languages.en.dir, 'content')
|
2020-12-14 19:44:09 +03:00
|
|
|
const englishPaths = walk(englishPath, {
|
|
|
|
globs: ['**/*.md'],
|
|
|
|
ignore: ['**/README.md']
|
|
|
|
})
|
2020-12-09 20:40:58 +03:00
|
|
|
const englishPages = await mapLimit(
|
|
|
|
englishPaths,
|
|
|
|
FILE_READ_LIMIT,
|
2020-12-15 21:56:25 +03:00
|
|
|
async opts => await Page.read({
|
|
|
|
...opts,
|
|
|
|
languageCode: languages.en.code
|
|
|
|
})
|
2020-12-09 20:40:58 +03:00
|
|
|
)
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
// load matching pages in other languages
|
2020-12-14 19:44:09 +03:00
|
|
|
const localizedPaths = Object.values(languages)
|
2020-12-09 20:40:58 +03:00
|
|
|
.filter(({ code }) => code !== 'en')
|
|
|
|
.map(language => {
|
2020-09-27 15:10:11 +03:00
|
|
|
const basePath = path.join(__dirname, '..', language.dir, 'content')
|
2020-12-09 20:40:58 +03:00
|
|
|
return englishPages.map(page => ({
|
2020-12-09 20:12:07 +03:00
|
|
|
basePath,
|
2020-12-09 20:40:58 +03:00
|
|
|
relativePath: page.relativePath,
|
2020-09-27 15:10:11 +03:00
|
|
|
languageCode: language.code
|
|
|
|
}))
|
2020-12-09 20:40:58 +03:00
|
|
|
})
|
|
|
|
.flat()
|
|
|
|
const localizedPages = await mapLimit(
|
|
|
|
localizedPaths,
|
|
|
|
FILE_READ_LIMIT,
|
|
|
|
async ({ basePath, relativePath, languageCode }) =>
|
2020-12-15 21:56:25 +03:00
|
|
|
await Page.read({ basePath, relativePath, languageCode })
|
2020-12-09 20:40:58 +03:00
|
|
|
)
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2020-12-15 21:56:25 +03:00
|
|
|
// Build out the list of prepared pages
|
|
|
|
return englishPages
|
|
|
|
.concat(localizedPages)
|
|
|
|
.filter(Boolean)
|
|
|
|
.map(opts => new Page(opts))
|
2020-12-04 01:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function createMapFromArray (pageList) {
|
|
|
|
// add keys to the object for each of the page's permalinks for fast lookup
|
|
|
|
const pageMap =
|
|
|
|
pageList.reduce(
|
|
|
|
(pageMap, page) => {
|
|
|
|
for (const permalink of page.permalinks) {
|
|
|
|
pageMap[permalink.href] = page
|
|
|
|
}
|
|
|
|
return pageMap
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
|
|
|
return pageMap
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadPageMap (pageList) {
|
|
|
|
const pages = pageList || await loadPageList()
|
|
|
|
return createMapFromArray(pages)
|
|
|
|
}
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2020-12-04 01:31:10 +03:00
|
|
|
module.exports = {
|
|
|
|
loadPages: loadPageList,
|
|
|
|
loadPageMap
|
2020-09-27 15:10:11 +03:00
|
|
|
}
|