Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Path module functions and properties in server and generate #490

Merged
merged 6 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 53 additions & 34 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ function execute() {
const env = require('./env.js');
const siteConfig = require(CWD + '/siteConfig.js');
const translate = require('./translate.js');

const feed = require('./feed.js');
const sitemap = require('./sitemap.js');

const join = path.join;
const sep = path.sep;
const escapeStringRegexp = require('escape-string-regexp');

// create the folder path for a file if it does not exist, then write the file
function writeFileAndCreateFolder(file, content) {
Expand Down Expand Up @@ -230,20 +230,23 @@ function execute() {
.sort()
.reverse()
.forEach(file => {
const extension = path.extname(file);
// Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
let normalizedFile = path.normalize(file);
const extension = path.extname(normalizedFile);
if (extension !== '.md' && extension !== '.markdown') {
return;
}

// convert filename to use slashes
const filePath = path
.basename(file)
.basename(normalizedFile)
.replace('-', '/')
.replace('-', '/')
.replace('-', '/')
.replace(/\.md$/, '.html');
const result = readMetadata.extractMetadata(
fs.readFileSync(file, {encoding: 'utf8'})
fs.readFileSync(normalizedFile, {encoding: 'utf8'})
);
const rawContent = result.rawContent;
const metadata = Object.assign(
Expand Down Expand Up @@ -313,10 +316,12 @@ function execute() {
// copy all static files from docusaurus
files = glob.sync(join(__dirname, '..', 'static', '**'));
files.forEach(file => {
let targetFile = join(
// Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
let targetFile = path.normalize(file);
targetFile = join(
buildDir,
// TODO: use x-platform path functions
file.split('/static/')[1] || ''
targetFile.split(sep + 'static' + sep)[1] || ''
);
// parse css files to replace colors according to siteConfig
if (file.match(/\.css$/)) {
Expand Down Expand Up @@ -369,10 +374,13 @@ function execute() {
// copy all static files from user
files = glob.sync(join(CWD, 'static', '**'), {dot: true});
files.forEach(file => {
// Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
let normalizedFile = path.normalize(file);
// parse css files to replace colors and fonts according to siteConfig
if (file.match(/\.css$/) && !isSeparateCss(file)) {
if (normalizedFile.match(/\.css$/) && !isSeparateCss(normalizedFile)) {
const mainCss = join(buildDir, 'css', 'main.css');
let cssContent = fs.readFileSync(file, 'utf8');
let cssContent = fs.readFileSync(normalizedFile, 'utf8');
cssContent = fs.readFileSync(mainCss, 'utf8') + '\n' + cssContent;

Object.keys(siteConfig.colors).forEach(key => {
Expand All @@ -393,49 +401,57 @@ function execute() {
}

fs.writeFileSync(mainCss, cssContent);
} else if (!fs.lstatSync(file).isDirectory()) {
let parts = file.split('/static/');
} else if (!fs.lstatSync(normalizedFile).isDirectory()) {
let parts = normalizedFile.split(sep + 'static' + sep);
let targetFile = join(buildDir, parts[1]);
mkdirp.sync(path.dirname(targetFile));
fs.copySync(file, targetFile);
fs.copySync(normalizedFile, targetFile);
}
});

// compile/copy pages from user
let pagesArr = [];
files = glob.sync(join(CWD, 'pages', '**'));
files.forEach(file => {
// Why normalize? In case we are on Windows.
// Remember the nuance of glob: https://www.npmjs.com/package/glob#windows
let normalizedFile = path.normalize(file);
// render .js files to strings
if (file.match(/\.js$/)) {
const pageID = path.basename(file, '.js');
if (normalizedFile.match(/\.js$/)) {
const pageID = path.basename(normalizedFile, '.js');

// make temp file for sake of require paths
const parts = file.split('pages');
const parts = normalizedFile.split('pages');
let tempFile = join(__dirname, '..', 'pages', parts[1]);
tempFile = tempFile.replace(
path.basename(file),
'temp' + path.basename(file)
path.basename(normalizedFile),
'temp' + path.basename(normalizedFile)
);
mkdirp.sync(path.dirname(tempFile));
fs.copySync(file, tempFile);
fs.copySync(normalizedFile, tempFile);

const ReactComp = require(tempFile);

let targetFile = join(buildDir, parts[1]);
targetFile = targetFile.replace(/\.js$/, '.html');

const regexLang = /\/pages\/(.*)\//;
const match = regexLang.exec(file);
const langParts = match[1].split('/');
const regexLang = new RegExp(
escapeStringRegexp(sep + 'pages' + sep) +
'(.*)' +
escapeStringRegexp(sep)
);
const match = regexLang.exec(normalizedFile);
const langParts = match[1].split(sep);
if (langParts.indexOf('en') !== -1) {
// copy and compile a page for each enabled language from the English file
for (let i = 0; i < enabledLanguages.length; i++) {
let language = enabledLanguages[i];
// skip conversion from english file if a file exists for this language
if (
language !== 'en' &&
// TODO: use path functions
fs.existsSync(file.replace('/en/', '/' + language + '/'))
fs.existsSync(
normalizedFile.replace(sep + 'en' + sep, sep + language + sep)
)
) {
continue;
}
Expand All @@ -450,7 +466,7 @@ function execute() {
);
writeFileAndCreateFolder(
// TODO: use path functions
targetFile.replace('/en/', '/' + language + '/'),
targetFile.replace(sep + 'en' + sep, sep + language + sep),
str
);
}
Expand All @@ -463,7 +479,10 @@ function execute() {
<ReactComp language={language} />
</Site>
);
writeFileAndCreateFolder(targetFile.replace('/en/', '/'), str);
writeFileAndCreateFolder(
targetFile.replace(sep + 'en' + sep, sep),
str
);
} else {
// allow for rendering of other files not in pages/en folder
let language = env.translation.enabled ? 'en' : '';
Expand All @@ -473,30 +492,30 @@ function execute() {
<ReactComp language={language} />
</Site>
);
writeFileAndCreateFolder(targetFile.replace('/en/', '/'), str);
writeFileAndCreateFolder(targetFile.replace(sep + en + sep, sep), str);
}
fs.removeSync(tempFile);
} else if (siteConfig.wrapPagesHTML && file.match(/\.html$/)) {
const pageID = path.basename(file, '.html');
const parts = file.split('pages');
} else if (siteConfig.wrapPagesHTML && normalizedFile.match(/\.html$/)) {
const pageID = path.basename(normalizedFile, '.html');
const parts = normalizedFile.split('pages');
const targetFile = join(buildDir, parts[1]);
const str = renderToStaticMarkup(
<Site language="en" config={siteConfig} metadata={{id: pageID}}>
<div
dangerouslySetInnerHTML={{
__html: fs.readFileSync(file, {encoding: 'utf8'}),
__html: fs.readFileSync(normalizedFile, {encoding: 'utf8'}),
}}
/>
</Site>
);

writeFileAndCreateFolder(targetFile, str);
} else if (!fs.lstatSync(file).isDirectory()) {
} else if (!fs.lstatSync(normalizedFile).isDirectory()) {
// copy other non .js files
let parts = file.split('pages');
let parts = normalizedFile.split('pages');
let targetFile = join(buildDir, parts[1]);
mkdirp.sync(path.dirname(targetFile));
fs.copySync(file, targetFile);
fs.copySync(normalizedFile, targetFile);
}
});

Expand Down
Loading