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-09 20:12:07 +03:00
|
|
|
const fs = require('fs')
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2020-12-04 01:31:10 +03:00
|
|
|
async function loadPageList () {
|
|
|
|
const pageList = []
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
// load english pages
|
|
|
|
const englishPath = path.join(__dirname, '..', languages.en.dir, 'content')
|
2020-12-09 20:12:07 +03:00
|
|
|
const englishPages = walk(englishPath)
|
|
|
|
.filter(({ relativePath }) => {
|
|
|
|
return relativePath.endsWith('.md') &&
|
|
|
|
!relativePath.includes('README')
|
|
|
|
})
|
|
|
|
.map(fileData => new Page({ ...fileData, languageCode: languages.en.code }))
|
|
|
|
|
2020-12-04 01:31:10 +03:00
|
|
|
pageList.push(...englishPages)
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
// load matching pages in other languages
|
2020-12-09 20:12:07 +03:00
|
|
|
for (const page of englishPages) {
|
|
|
|
for (const language of Object.values(languages)) {
|
|
|
|
if (language.code === 'en') continue
|
|
|
|
|
2020-09-27 15:10:11 +03:00
|
|
|
const basePath = path.join(__dirname, '..', language.dir, 'content')
|
2020-12-09 20:12:07 +03:00
|
|
|
const localizedPath = path.join(basePath, page.relativePath)
|
|
|
|
try {
|
|
|
|
fs.statSync(localizedPath)
|
|
|
|
} catch (_) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList.push(new Page({
|
2020-12-09 19:57:18 +03:00
|
|
|
relativePath: page.relativePath,
|
2020-12-09 20:12:07 +03:00
|
|
|
basePath,
|
2020-09-27 15:10:11 +03:00
|
|
|
languageCode: language.code
|
|
|
|
}))
|
2020-12-09 20:12:07 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2020-12-04 01:31:10 +03:00
|
|
|
return pageList
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|