feat: custom pages (#994)

This commit is contained in:
Endilie Yacop Sucipto 2018-09-29 00:18:38 +08:00 committed by GitHub
parent 7d4d9fe961
commit 8691a2525c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 263 additions and 128 deletions

View file

@ -1,16 +1,57 @@
const globby = require('globby');
const {encodePath, fileToPath} = require('./utils');
const path = require('path');
const {encodePath, fileToPath, idx} = require('./utils');
async function loadPages(pagesDir) {
async function loadPages({pagesDir, env, siteConfig}) {
const pagesFiles = await globby(['**/*.js'], {
cwd: pagesDir,
});
const pagesMetadatas = await Promise.all(
pagesFiles.map(async source => ({
path: encodePath(fileToPath(source)),
source,
})),
const {baseUrl} = siteConfig;
/* Prepare metadata container */
const pagesMetadatas = [];
/* Translation */
const translationEnabled = idx(env, ['translation', 'enabled']);
const enabledLanguages =
translationEnabled && idx(env, ['translation', 'enabledLanguages']);
const enabledLangTags =
(enabledLanguages && enabledLanguages.map(lang => lang.tag)) || [];
const defaultLangTag = idx(env, ['translation', 'defaultLanguage', 'tag']);
await Promise.all(
pagesFiles.map(async relativeSource => {
const source = path.join(pagesDir, relativeSource);
const pathName = encodePath(fileToPath(relativeSource));
if (translationEnabled && enabledLangTags.length > 0) {
enabledLangTags.forEach(langTag => {
/* default lang should also be available. E.g: /en/users and /users is the same */
if (langTag === defaultLangTag) {
pagesMetadatas.push({
permalink: pathName.replace(/^\//, baseUrl),
language: langTag,
source,
});
}
const metadata = {
permalink: pathName.replace(/^\//, `${baseUrl}${langTag}/`),
language: langTag,
source,
};
pagesMetadatas.push(metadata);
});
// for defaultLanguage
} else {
const metadata = {
permalink: pathName.replace(/^\//, baseUrl),
source,
};
pagesMetadatas.push(metadata);
}
}),
);
return pagesMetadatas;
}