forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages.js
48 lines (42 loc) · 1.35 KB
/
pages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const path = require('path')
const walk = require('walk-sync').entries
const Page = require('./page')
const languages = require('./languages')
const fs = require('fs')
module.exports = async function () {
const pages = []
// load english pages
const englishPath = path.join(__dirname, '..', languages.en.dir, 'content')
const englishPages = walk(englishPath)
.filter(({ relativePath }) => {
return relativePath.endsWith('.md') &&
!relativePath.includes('README')
})
.map(fileData => new Page({ ...fileData, languageCode: languages.en.code }))
pages.push(...englishPages)
// load matching pages in other languages
for (const page of englishPages) {
for (const language of Object.values(languages)) {
if (language.code === 'en') continue
const basePath = path.join(__dirname, '..', language.dir, 'content')
const localizedPath = path.join(basePath, page.relativePath)
try {
fs.statSync(localizedPath)
} catch (_) {
continue
}
pages.push(new Page({
relativePath: page.relativePath,
basePath,
languageCode: language.code
}))
}
}
// add named keys to the array for fast object-like reference
for (const page of pages) {
page.permalinks.forEach(permalink => {
pages[permalink.href] = page
})
}
return Promise.resolve(pages)
}