From 596d97cd2556831e0999bb9339c92c903e8ef1a0 Mon Sep 17 00:00:00 2001 From: endiliey Date: Tue, 17 Jul 2018 00:49:56 +0800 Subject: [PATCH 1/8] nits too many comments --- lib/server/server.js | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/lib/server/server.js b/lib/server/server.js index ca067e056e7f..6dc7e44ac9af 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -9,10 +9,8 @@ function execute(port, options) { const extractTranslations = require('../write-translations'); - const metadataUtils = require('./metadataUtils'); const docs = require('./docs'); - const env = require('./env.js'); const express = require('express'); const React = require('react'); @@ -25,17 +23,13 @@ function execute(port, options) { const chalk = require('chalk'); const gaze = require('gaze'); const tinylr = require('tiny-lr'); - const constants = require('../core/constants'); const translate = require('./translate'); const {renderToStaticMarkupWithDoctype} = require('./renderUtils'); - const feed = require('./feed'); const sitemap = require('./sitemap'); const routing = require('./routing'); - const CWD = process.cwd(); - const join = path.join; const sep = path.sep; @@ -46,7 +40,6 @@ function execute(port, options) { delete module.constructor._pathCache[cacheKey]; } }); - /* eslint-enable no-underscore-dangle */ } // Remove a module and child modules from require cache, so server @@ -112,7 +105,6 @@ function execute(port, options) { } function startLiveReload() { - // Start LiveReload server. process.env.NODE_ENV = 'development'; const server = tinylr(); server.listen(constants.LIVE_RELOAD_PORT, () => { @@ -122,19 +114,10 @@ function execute(port, options) { ); }); - // gaze watches some specified dirs and triggers a callback when they change. gaze( - [ - `../${readMetadata.getDocsPath()}/**/*`, // docs - '**/*', // website - '!node_modules/**/*', // node_modules - ], + [`../${readMetadata.getDocsPath()}/**/*`, '**/*', '!node_modules/**/*'], function() { - // Listen for all kinds of file changes - modified/added/deleted. this.on('all', () => { - // Notify LiveReload clients that there's a change. - // Typically, LiveReload will only refresh the changed paths, - // so we use / here to force a full-page reload. server.notifyClients(['/']); }); } @@ -148,7 +131,6 @@ function execute(port, options) { const app = express(); - // handle all requests for document pages app.get(routing.docs(siteConfig.baseUrl), (req, res, next) => { const url = req.path.toString().replace(siteConfig.baseUrl, ''); const metadata = @@ -192,7 +174,6 @@ function execute(port, options) { next(); }); - // Handle all requests for blog pages and posts. app.get(routing.blog(siteConfig.baseUrl), (req, res, next) => { // Regenerate the blog metadata in case it has changed. Consider improving // this to regenerate on file save rather than on page request. @@ -282,7 +263,6 @@ function execute(port, options) { } }); - // handle all other main pages app.get(routing.page(siteConfig.baseUrl), (req, res, next) => { // Look for user-provided HTML file first. let htmlFile = req.path.toString().replace(siteConfig.baseUrl, ''); @@ -390,7 +370,6 @@ function execute(port, options) { } }); - // generate the main.css file by concatenating user provided css to the end app.get(/main\.css$/, (req, res) => { const mainCssPath = join( __dirname, @@ -473,7 +452,7 @@ function execute(port, options) { }); // handle special cleanUrl case like '/blog/1.2.3' & '/blog.robots.hai' - // where we should try to serve 'blog/1.2.3.html' & '/blog.robots.hai.html' + // where we should try to serve '/blog/1.2.3.html' & '/blog.robots.hai.html' app.get(routing.dotfiles(), (req, res, next) => { if (!siteConfig.cleanUrl) { next(); From 7cc0d11f2c6c350f582ac10ace5a2ac121b2167b Mon Sep 17 00:00:00 2001 From: endiliey Date: Tue, 17 Jul 2018 01:16:55 +0800 Subject: [PATCH 2/8] Refactor to blog.getPages --- lib/server/blog.js | 28 ++++++++++++++++++++++++++++ lib/server/generate.js | 28 +++++++--------------------- lib/server/server.js | 26 ++------------------------ 3 files changed, 37 insertions(+), 45 deletions(-) create mode 100644 lib/server/blog.js diff --git a/lib/server/blog.js b/lib/server/blog.js new file mode 100644 index 000000000000..529128a4e335 --- /dev/null +++ b/lib/server/blog.js @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2017-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +const React = require('react'); +const {renderToStaticMarkupWithDoctype} = require('./renderUtils'); + +function getPages(length, config) { + const BlogPageLayout = require('../core/BlogPageLayout.js'); + const blogPages = {}; + const perPage = 10; + for (let page = 0; page < Math.ceil(length / perPage); page++) { + const metadata = {page, perPage}; + const blogPageComp = ( + + ); + const str = renderToStaticMarkupWithDoctype(blogPageComp); + const pagePath = `${page > 0 ? `page${page + 1}` : ''}/index.html`; + blogPages[pagePath] = str; + } + return blogPages; +} + +module.exports = { + getPages, +}; diff --git a/lib/server/generate.js b/lib/server/generate.js index 06a7e8e8a211..67b1f61723a7 100644 --- a/lib/server/generate.js +++ b/lib/server/generate.js @@ -9,6 +9,7 @@ async function execute() { require('../write-translations.js'); const metadataUtils = require('./metadataUtils'); + const blog = require('./blog'); const docs = require('./docs'); const CWD = process.cwd(); @@ -177,29 +178,14 @@ async function execute() { const targetFile = join(buildDir, 'blog', filePath); writeFileAndCreateFolder(targetFile, str); }); + // create html files for all blog pages (collections of article previews) - const BlogPageLayout = require('../core/BlogPageLayout.js'); - const perPage = 10; - for (let page = 0; page < Math.ceil(MetadataBlog.length / perPage); page++) { - const language = 'en'; - const metadata = {page, perPage}; - const blogPageComp = ( - - ); - const str = renderToStaticMarkupWithDoctype(blogPageComp); + const blogPages = blog.getPages(MetadataBlog.length, siteConfig); + Object.keys(blogPages).forEach(pagePath => { + const targetFile = join(buildDir, 'blog', pagePath); + writeFileAndCreateFolder(targetFile, blogPages[pagePath]); + }); - const targetFile = join( - buildDir, - 'blog', - page > 0 ? `page${page + 1}` : '', - 'index.html' - ); - writeFileAndCreateFolder(targetFile, str); - } // create rss files for all blog pages, if there are any blog files if (MetadataBlog.length > 0) { let targetFile = join(buildDir, 'blog', 'feed.xml'); diff --git a/lib/server/server.js b/lib/server/server.js index 6dc7e44ac9af..0883daabc3a1 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -10,6 +10,7 @@ function execute(port, options) { const extractTranslations = require('../write-translations'); const metadataUtils = require('./metadataUtils'); + const blog = require('./blog'); const docs = require('./docs'); const env = require('./env.js'); const express = require('express'); @@ -180,30 +181,7 @@ function execute(port, options) { reloadMetadataBlog(); // Generate all of the blog pages. removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPageLayout.js')); - const BlogPageLayout = require(join('..', 'core', 'BlogPageLayout.js')); - const blogPages = {}; - // Make blog pages with 10 posts per page. - const perPage = 10; - for ( - let page = 0; - page < Math.ceil(MetadataBlog.length / perPage); - page++ - ) { - const language = 'en'; - const metadata = {page, perPage}; - const blogPageComp = ( - - ); - const str = renderToStaticMarkupWithDoctype(blogPageComp); - - const pagePath = `${page > 0 ? `page${page + 1}` : ''}/index.html`; - blogPages[pagePath] = str; - } - + const blogPages = blog.getPages(MetadataBlog.length, siteConfig); const parts = req.path.toString().split('blog/'); // send corresponding blog page if appropriate if (parts[1] === 'index.html') { From 8962f61f81fe686b94c6d65391513646c4d4657b Mon Sep 17 00:00:00 2001 From: endiliey Date: Tue, 17 Jul 2018 02:34:09 +0800 Subject: [PATCH 3/8] Refactor to getPost, fileToUrl, urlToFile --- lib/server/blog.js | 49 ++++++++++++++++++++++++++++++-- lib/server/generate.js | 33 ++-------------------- lib/server/server.js | 64 ++++++++---------------------------------- 3 files changed, 62 insertions(+), 84 deletions(-) diff --git a/lib/server/blog.js b/lib/server/blog.js index 529128a4e335..16a04f0b3bbc 100644 --- a/lib/server/blog.js +++ b/lib/server/blog.js @@ -5,13 +5,16 @@ * LICENSE file in the root directory of this source tree. */ const React = require('react'); +const path = require('path'); +const fs = require('fs-extra'); const {renderToStaticMarkupWithDoctype} = require('./renderUtils'); +const metadataUtils = require('./metadataUtils'); -function getPages(length, config) { +function getPages(numOfBlog, config) { const BlogPageLayout = require('../core/BlogPageLayout.js'); const blogPages = {}; const perPage = 10; - for (let page = 0; page < Math.ceil(length / perPage); page++) { + for (let page = 0; page < Math.ceil(numOfBlog / perPage); page++) { const metadata = {page, perPage}; const blogPageComp = ( @@ -23,6 +26,48 @@ function getPages(length, config) { return blogPages; } +function getPost(file, urlPath, config) { + if (!fs.existsSync(file)) { + return null; + } + const result = metadataUtils.extractMetadata( + fs.readFileSync(file, {encoding: 'utf8'}) + ); + const rawContent = result.rawContent; + const metadata = Object.assign( + {path: urlPath, content: rawContent}, + result.metadata + ); + metadata.id = metadata.title; + + const BlogPostLayout = require('../core/BlogPostLayout.js'); + const blogPostComp = ( + + {rawContent} + + ); + return renderToStaticMarkupWithDoctype(blogPostComp); +} + +function urlToFile(url) { + return url + .replace(/\/index.html$/, '.md') + .replace(/\.html$/, '.md') + .replace(new RegExp('/', 'g'), '-'); +} + +function fileToUrl(file) { + return path + .basename(file) + .replace('-', '/') + .replace('-', '/') + .replace('-', '/') + .replace(/\.md$/, '.html'); +} + module.exports = { + fileToUrl, getPages, + getPost, + urlToFile, }; diff --git a/lib/server/generate.js b/lib/server/generate.js index 67b1f61723a7..da3c72d1f25b 100644 --- a/lib/server/generate.js +++ b/lib/server/generate.js @@ -132,7 +132,6 @@ async function execute() { } readMetadata.generateMetadataBlog(); const MetadataBlog = require('../core/MetadataBlog.js'); - const BlogPostLayout = require('../core/BlogPostLayout.js'); let files = glob.sync(join(CWD, 'blog', '**', '*.*')); files @@ -147,35 +146,9 @@ async function execute() { return; } - // convert filename to use slashes - const filePath = path - .basename(normalizedFile) - .replace('-', '/') - .replace('-', '/') - .replace('-', '/') - .replace(/\.md$/, '.html'); - const result = metadataUtils.extractMetadata( - fs.readFileSync(normalizedFile, {encoding: 'utf8'}) - ); - const rawContent = result.rawContent; - const metadata = Object.assign( - {path: filePath, content: rawContent}, - result.metadata - ); - metadata.id = metadata.title; - - const language = 'en'; - const blogPostComp = ( - - {rawContent} - - ); - const str = renderToStaticMarkupWithDoctype(blogPostComp); - - const targetFile = join(buildDir, 'blog', filePath); + const urlPath = blog.fileToUrl(normalizedFile); + const str = blog.getPost(normalizedFile, urlPath, siteConfig); + const targetFile = join(buildDir, 'blog', urlPath); writeFileAndCreateFolder(targetFile, str); }); diff --git a/lib/server/server.js b/lib/server/server.js index 0883daabc3a1..d9775d9393f6 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -179,65 +179,25 @@ function execute(port, options) { // Regenerate the blog metadata in case it has changed. Consider improving // this to regenerate on file save rather than on page request. reloadMetadataBlog(); - // Generate all of the blog pages. removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPageLayout.js')); const blogPages = blog.getPages(MetadataBlog.length, siteConfig); - const parts = req.path.toString().split('blog/'); - // send corresponding blog page if appropriate - if (parts[1] === 'index.html') { + const urlPath = req.path.toString().split('blog/')[1]; + + if (urlPath === 'index.html') { res.send(blogPages['/index.html']); - } else if (parts[1].endsWith('/index.html') && blogPages[parts[1]]) { - res.send(blogPages[parts[1]]); - } else if (parts[1].match(/page([0-9]+)/)) { - if (parts[1].endsWith('/')) { - res.send(blogPages[`${parts[1]}index.html`]); - } else { - res.send(blogPages[`${parts[1]}/index.html`]); - } + } else if (urlPath.endsWith('/index.html') && blogPages[urlPath]) { + res.send(blogPages[urlPath]); + } else if (urlPath.match(/page([0-9]+)/)) { + res.send(blogPages[`${urlPath.replace(/\/$/, '')}/index.html`]); } else { - // send corresponding blog post. Ex: request to "blog/test/index.html" or - // "blog/test.html" will return html rendered version of "blog/test.md" - let file = parts[1]; - if (file.endsWith('/index.html')) { - file = file.replace(/\/index.html$/, '.md'); - } else { - file = file.replace(/\.html$/, '.md'); - } - file = file.replace(new RegExp('/', 'g'), '-'); - file = join(CWD, 'blog', file); - - if (!fs.existsSync(file)) { + const file = join(CWD, 'blog', blog.urlToFile(urlPath)); + removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js')); + const blogPost = blog.getPost(file, urlPath, siteConfig); + if (blogPost) { next(); return; } - - const result = metadataUtils.extractMetadata( - fs.readFileSync(file, {encoding: 'utf8'}) - ); - let rawContent = result.rawContent; - rawContent = rawContent.replace( - /\]\(assets\//g, - `](${siteConfig.baseUrl}blog/assets/` - ); - const metadata = Object.assign( - {path: req.path.toString().split('blog/')[1], content: rawContent}, - result.metadata - ); - metadata.id = metadata.title; - - const language = 'en'; - removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js')); - const BlogPostLayout = require(join('..', 'core', 'BlogPostLayout.js')); - - const blogPostComp = ( - - {rawContent} - - ); - res.send(renderToStaticMarkupWithDoctype(blogPostComp)); + res.send(blogPost); } }); From 4480d3a1a296bdba659b7faae4de05b7206cbc42 Mon Sep 17 00:00:00 2001 From: endiliey Date: Tue, 17 Jul 2018 02:45:21 +0800 Subject: [PATCH 4/8] Refactor redirectcomponent generation for docs --- lib/server/docs.js | 20 ++++++++++++++++++++ lib/server/generate.js | 37 +++++++++---------------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/server/docs.js b/lib/server/docs.js index 896d3e1ec994..2b2c2e917c06 100644 --- a/lib/server/docs.js +++ b/lib/server/docs.js @@ -93,8 +93,28 @@ function getComponent(rawContent, mdToHtml, metadata) { ); } +function getRedirectComponent(metadata) { + if ( + !env.translation.enabled || + metadata.permalink.indexOf('docs/en') === -1 + ) { + return null; + } + const Redirect = require('../core/Redirect.js'); + const redirectlink = getPath(metadata.permalink, siteConfig.cleanUrl); + return ( + + ); +} + module.exports = { getComponent, getFile, + getRedirectComponent, mdToHtmlify, }; diff --git a/lib/server/generate.js b/lib/server/generate.js index da3c72d1f25b..b62cb7445519 100644 --- a/lib/server/generate.js +++ b/lib/server/generate.js @@ -7,16 +7,13 @@ async function execute() { require('../write-translations.js'); - const metadataUtils = require('./metadataUtils'); const blog = require('./blog'); const docs = require('./docs'); - const CWD = process.cwd(); const fs = require('fs-extra'); const readMetadata = require('./readMetadata.js'); const path = require('path'); - const {getPath} = require('../core/utils.js'); const {minifyCss, isSeparateCss} = require('./utils'); const React = require('react'); const mkdirp = require('mkdirp'); @@ -73,11 +70,8 @@ async function execute() { // needed when the project's a GitHub org page const buildDir = join(CWD, 'build', siteConfig.projectName); - const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl); - const Redirect = require('../core/Redirect.js'); - fs.removeSync(join(CWD, 'build')); // create html files for all docs by going through all doc ids @@ -94,28 +88,16 @@ async function execute() { writeFileAndCreateFolder(targetFile, str); // generate english page redirects when languages are enabled - if ( - env.translation.enabled && - metadata.permalink.indexOf('docs/en') !== -1 - ) { - const redirectlink = getPath(metadata.permalink, siteConfig.cleanUrl); - const redirectComp = ( - - ); - const redirectStr = renderToStaticMarkupWithDoctype(redirectComp); - - // create a redirects page for doc files - const redirectFile = join( - buildDir, - metadata.permalink.replace('docs/en', 'docs') - ); - writeFileAndCreateFolder(redirectFile, redirectStr); + const redirectComp = docs.getRedirectComponent(metadata); + if (!redirectComp) { + return; } + const redirectStr = renderToStaticMarkupWithDoctype(redirectComp); + const redirectFile = join( + buildDir, + metadata.permalink.replace('docs/en', 'docs') + ); + writeFileAndCreateFolder(redirectFile, redirectStr); }); // copy docs assets if they exist @@ -145,7 +127,6 @@ async function execute() { if (extension !== '.md' && extension !== '.markdown') { return; } - const urlPath = blog.fileToUrl(normalizedFile); const str = blog.getPost(normalizedFile, urlPath, siteConfig); const targetFile = join(buildDir, 'blog', urlPath); From c089ff70926b06089a9bb45bc4a48297d5ea1c12 Mon Sep 17 00:00:00 2001 From: endiliey Date: Tue, 17 Jul 2018 14:49:01 +0800 Subject: [PATCH 5/8] nits & fix typo --- lib/server/docs.js | 13 +++++++------ lib/server/generate.js | 19 +++++++------------ lib/server/server.js | 6 +++--- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/server/docs.js b/lib/server/docs.js index 2b2c2e917c06..95604f18e70f 100644 --- a/lib/server/docs.js +++ b/lib/server/docs.js @@ -10,6 +10,7 @@ const {join} = require('path'); const fs = require('fs-extra'); const React = require('react'); const env = require('./env.js'); +const {renderToStaticMarkupWithDoctype} = require('./renderUtils'); const readMetadata = require('./readMetadata.js'); const {insertTOC} = require('../core/toc.js'); const {getPath} = require('../core/utils.js'); @@ -69,7 +70,7 @@ function mdToHtmlify(oldContent, mdToHtml, metadata) { return content; } -function getComponent(rawContent, mdToHtml, metadata) { +function getStr(rawContent, mdToHtml, metadata) { // generate table of contents let content = insertTOC(rawContent); @@ -83,7 +84,7 @@ function getComponent(rawContent, mdToHtml, metadata) { ); const DocsLayout = require('../core/DocsLayout.js'); - return ( + return renderToStaticMarkupWithDoctype( lang.tag); - readMetadata.generateMetadataDocs(); const Metadata = require('../core/metadata.js'); @@ -70,11 +65,10 @@ async function execute() { // needed when the project's a GitHub org page const buildDir = join(CWD, 'build', siteConfig.projectName); - const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl); - fs.removeSync(join(CWD, 'build')); // create html files for all docs by going through all doc ids + const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl); Object.keys(Metadata).forEach(id => { const metadata = Metadata[id]; const file = docs.getFile(metadata); @@ -82,17 +76,15 @@ async function execute() { return; } const rawContent = metadataUtils.extractMetadata(file).rawContent; - const docComp = docs.getComponent(rawContent, mdToHtml, metadata); - const str = renderToStaticMarkupWithDoctype(docComp); + const str = docs.getStr(rawContent, mdToHtml, metadata); const targetFile = join(buildDir, metadata.permalink); writeFileAndCreateFolder(targetFile, str); // generate english page redirects when languages are enabled - const redirectComp = docs.getRedirectComponent(metadata); - if (!redirectComp) { + const redirectStr = docs.getRedirectStr(metadata); + if (!redirectStr) { return; } - const redirectStr = renderToStaticMarkupWithDoctype(redirectComp); const redirectFile = join( buildDir, metadata.permalink.replace('docs/en', 'docs') @@ -282,6 +274,9 @@ async function execute() { fs.writeFileSync(mainCss, css); // compile/copy pages from user + const enabledLanguages = env.translation + .enabledLanguages() + .map(lang => lang.tag); files = glob.sync(join(CWD, 'pages', '**')); files.forEach(file => { // Why normalize? In case we are on Windows. diff --git a/lib/server/server.js b/lib/server/server.js index d9775d9393f6..8138c5324e62 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -146,8 +146,8 @@ function execute(port, options) { const rawContent = metadataUtils.extractMetadata(file).rawContent; removeModuleAndChildrenFromCache('../core/DocsLayout.js'); const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl); - const docComp = docs.getComponent(rawContent, mdToHtml, metadata); - res.send(renderToStaticMarkupWithDoctype(docComp)); + const str = docs.getStr(rawContent, mdToHtml, metadata); + res.send(str); }); app.get(routing.sitemap(siteConfig.baseUrl), (req, res) => { @@ -193,7 +193,7 @@ function execute(port, options) { const file = join(CWD, 'blog', blog.urlToFile(urlPath)); removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js')); const blogPost = blog.getPost(file, urlPath, siteConfig); - if (blogPost) { + if (!blogPost) { next(); return; } From 21c5a224e3a22e5da68b5b3d9b268bc5744c65ef Mon Sep 17 00:00:00 2001 From: endiliey Date: Tue, 17 Jul 2018 15:45:50 +0800 Subject: [PATCH 6/8] Refactor to blog.getMetadata --- lib/server/blog.js | 47 ++++++++++++++++++++++---------------- lib/server/generate.js | 5 +++- lib/server/readMetadata.js | 23 ++----------------- lib/server/server.js | 2 +- 4 files changed, 34 insertions(+), 43 deletions(-) diff --git a/lib/server/blog.js b/lib/server/blog.js index 16a04f0b3bbc..ee5c72a721b8 100644 --- a/lib/server/blog.js +++ b/lib/server/blog.js @@ -10,6 +10,22 @@ const fs = require('fs-extra'); const {renderToStaticMarkupWithDoctype} = require('./renderUtils'); const metadataUtils = require('./metadataUtils'); +function urlToFile(url) { + return url + .replace(/\/index.html$/, '.md') + .replace(/\.html$/, '.md') + .replace(new RegExp('/', 'g'), '-'); +} + +function fileToUrl(file) { + return path + .basename(file) + .replace('-', '/') + .replace('-', '/') + .replace('-', '/') + .replace(/\.md$/, '.html'); +} + function getPages(numOfBlog, config) { const BlogPageLayout = require('../core/BlogPageLayout.js'); const blogPages = {}; @@ -26,47 +42,38 @@ function getPages(numOfBlog, config) { return blogPages; } -function getPost(file, urlPath, config) { +function getMetadata(file) { if (!fs.existsSync(file)) { return null; } const result = metadataUtils.extractMetadata( fs.readFileSync(file, {encoding: 'utf8'}) ); - const rawContent = result.rawContent; const metadata = Object.assign( - {path: urlPath, content: rawContent}, + {path: fileToUrl(file), content: result.rawContent}, result.metadata ); metadata.id = metadata.title; + return metadata; +} +function getPost(file, config) { + const metadata = getMetadata(file); + if (!metadata) { + return null; + } const BlogPostLayout = require('../core/BlogPostLayout.js'); const blogPostComp = ( - {rawContent} + {metadata.content} ); return renderToStaticMarkupWithDoctype(blogPostComp); } -function urlToFile(url) { - return url - .replace(/\/index.html$/, '.md') - .replace(/\.html$/, '.md') - .replace(new RegExp('/', 'g'), '-'); -} - -function fileToUrl(file) { - return path - .basename(file) - .replace('-', '/') - .replace('-', '/') - .replace('-', '/') - .replace(/\.md$/, '.html'); -} - module.exports = { fileToUrl, + getMetadata, getPages, getPost, urlToFile, diff --git a/lib/server/generate.js b/lib/server/generate.js index 4dcf40bcdd3c..06c8082d434a 100644 --- a/lib/server/generate.js +++ b/lib/server/generate.js @@ -120,7 +120,10 @@ async function execute() { return; } const urlPath = blog.fileToUrl(normalizedFile); - const str = blog.getPost(normalizedFile, urlPath, siteConfig); + const str = blog.getPost(normalizedFile, siteConfig); + if (!str) { + return; + } const targetFile = join(buildDir, 'blog', urlPath); writeFileAndCreateFolder(targetFile, str); }); diff --git a/lib/server/readMetadata.js b/lib/server/readMetadata.js index b039ec522727..db6060bae27e 100644 --- a/lib/server/readMetadata.js +++ b/lib/server/readMetadata.js @@ -14,6 +14,7 @@ const glob = require('glob'); const metadataUtils = require('./metadataUtils'); const env = require('./env.js'); +const blog = require('./blog.js'); const siteConfig = require(`${CWD}/siteConfig.js`); const versionFallback = require('./versionFallback.js'); @@ -313,27 +314,7 @@ function generateMetadataBlog() { if (extension !== '.md' && extension !== '.markdown') { return; } - // Transform - // 2015-08-13-blog-post-name-0.5.md - // into - // 2015/08/13/blog-post-name-0-5.html - const filePath = path - .basename(file) - .replace('-', '/') - .replace('-', '/') - .replace('-', '/') - .replace(/\.md$/, '.html'); - const result = metadataUtils.extractMetadata( - fs.readFileSync(file, {encoding: 'utf8'}) - ); - const rawContent = result.rawContent; - const metadata = Object.assign( - {path: filePath, content: rawContent}, - result.metadata - ); - - metadata.id = metadata.title; - + const metadata = blog.getMetadata(file); // Extract, YYYY, MM, DD from the file name const filePathDateArr = path .basename(file) diff --git a/lib/server/server.js b/lib/server/server.js index 8138c5324e62..e5fe75e12e99 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -192,7 +192,7 @@ function execute(port, options) { } else { const file = join(CWD, 'blog', blog.urlToFile(urlPath)); removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js')); - const blogPost = blog.getPost(file, urlPath, siteConfig); + const blogPost = blog.getPost(file, siteConfig); if (!blogPost) { next(); return; From 387d52950cebb0a87c7592919e3a8d9798d0b86b Mon Sep 17 00:00:00 2001 From: endiliey Date: Tue, 17 Jul 2018 16:45:45 +0800 Subject: [PATCH 7/8] Add test for getMetadata, fileToSUrl and urlToSource --- .../__fixtures__/2018-08-17-docusaurus.md | 11 +++ .../__tests__/__snapshots__/blog.test.js.snap | 17 +++++ lib/server/__tests__/blog.test.js | 68 +++++++++++++++++++ lib/server/blog.js | 12 +++- lib/server/server.js | 2 +- 5 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 lib/server/__tests__/__fixtures__/2018-08-17-docusaurus.md create mode 100644 lib/server/__tests__/__snapshots__/blog.test.js.snap create mode 100644 lib/server/__tests__/blog.test.js diff --git a/lib/server/__tests__/__fixtures__/2018-08-17-docusaurus.md b/lib/server/__tests__/__fixtures__/2018-08-17-docusaurus.md new file mode 100644 index 000000000000..3f6f4416e412 --- /dev/null +++ b/lib/server/__tests__/__fixtures__/2018-08-17-docusaurus.md @@ -0,0 +1,11 @@ +--- +title: Docusaurus +author: Endilie +authorURL: https://github.com/endiliey +authorFBID: 100000251103620 +authorTwitter: endiliey +--- + +![Docusaurus](/img/slash-introducing.png) + +We are very happy to introduce [Docusaurus](https://github.com/facebook/Docusaurus) to help you manage one or many open source websites. \ No newline at end of file diff --git a/lib/server/__tests__/__snapshots__/blog.test.js.snap b/lib/server/__tests__/__snapshots__/blog.test.js.snap new file mode 100644 index 000000000000..9e024937a154 --- /dev/null +++ b/lib/server/__tests__/__snapshots__/blog.test.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`getMetadata blog file 1`] = ` +Object { + "author": "Endilie", + "authorFBID": 100000251103620, + "authorTwitter": "endiliey", + "authorURL": "https://github.com/endiliey", + "content": " +![Docusaurus](/img/slash-introducing.png) + +We are very happy to introduce [Docusaurus](https://github.com/facebook/Docusaurus) to help you manage one or many open source websites.", + "id": "Docusaurus", + "path": "2018/08/17/docusaurus.html", + "title": "Docusaurus", +} +`; diff --git a/lib/server/__tests__/blog.test.js b/lib/server/__tests__/blog.test.js new file mode 100644 index 000000000000..442ba0de18d2 --- /dev/null +++ b/lib/server/__tests__/blog.test.js @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2017-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +const path = require('path'); +const fs = require('fs-extra'); +const blog = require('../blog'); + +const testFile = path.join( + __dirname, + '__fixtures__', + '2018-08-17-docusaurus.md' +); + +fs.existsSync = jest.fn().mockReturnValue(true); + +describe('getMetadata', () => { + test('file does not exist', () => { + fs.existsSync.mockReturnValueOnce(null); + expect(blog.getMetadata('/this/path/does-not-exist/')).toBeNull(); + }); + + test('null/undefined', () => { + expect(blog.getMetadata(null)).toBeNull(); + expect(blog.getMetadata(undefined)).toBeNull(); + }); + + test('blog file', () => { + const metadata = blog.getMetadata(testFile); + expect(metadata).toMatchSnapshot(); + expect(metadata).not.toBeNull(); + expect(metadata).toHaveProperty('id'); + expect(metadata).toHaveProperty('path'); + expect(metadata).toHaveProperty('content'); + }); +}); + +describe('fileToUrl', () => { + test('invalid file path', () => { + expect(blog.fileToUrl(null)).toBeNull(); + expect(blog.fileToUrl(undefined)).toBeNull(); + expect(blog.fileToUrl(true)).toBeNull(); + fs.existsSync.mockReturnValueOnce(null); + expect(blog.fileToUrl('2018-03-02-this-does-not-exist.md')).toBeNull(); + }); + + test('valid filepath', () => { + expect(blog.fileToUrl(testFile)).toEqual('2018/08/17/docusaurus.html'); + }); +}); + +describe('urlToSource', () => { + test('invalid url path', () => { + expect(blog.urlToSource(null)).toBeNull(); + expect(blog.urlToSource(undefined)).toBeNull(); + expect(blog.urlToSource(true)).toBeNull(); + }); + test('valid url path', () => { + expect(blog.urlToSource(`${blog.fileToUrl(testFile)}`)).toEqual( + '2018-08-17-docusaurus.md' + ); + expect(blog.urlToSource('2018/03/04/test-name-lol.html')).toEqual( + '2018-03-04-test-name-lol.md' + ); + }); +}); diff --git a/lib/server/blog.js b/lib/server/blog.js index ee5c72a721b8..c8b4cf10ac41 100644 --- a/lib/server/blog.js +++ b/lib/server/blog.js @@ -10,7 +10,10 @@ const fs = require('fs-extra'); const {renderToStaticMarkupWithDoctype} = require('./renderUtils'); const metadataUtils = require('./metadataUtils'); -function urlToFile(url) { +function urlToSource(url) { + if (!url || typeof url !== 'string') { + return null; + } return url .replace(/\/index.html$/, '.md') .replace(/\.html$/, '.md') @@ -18,6 +21,9 @@ function urlToFile(url) { } function fileToUrl(file) { + if (!file || !fs.existsSync(file) || typeof file !== 'string') { + return null; + } return path .basename(file) .replace('-', '/') @@ -43,7 +49,7 @@ function getPages(numOfBlog, config) { } function getMetadata(file) { - if (!fs.existsSync(file)) { + if (!file || !fs.existsSync(file)) { return null; } const result = metadataUtils.extractMetadata( @@ -76,5 +82,5 @@ module.exports = { getMetadata, getPages, getPost, - urlToFile, + urlToSource, }; diff --git a/lib/server/server.js b/lib/server/server.js index e5fe75e12e99..070c5cb1275d 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -190,7 +190,7 @@ function execute(port, options) { } else if (urlPath.match(/page([0-9]+)/)) { res.send(blogPages[`${urlPath.replace(/\/$/, '')}/index.html`]); } else { - const file = join(CWD, 'blog', blog.urlToFile(urlPath)); + const file = join(CWD, 'blog', blog.urlToSource(urlPath)); removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js')); const blogPost = blog.getPost(file, siteConfig); if (!blogPost) { From ea0363480105bb5d73711b4335b46da1600138e1 Mon Sep 17 00:00:00 2001 From: endiliey Date: Mon, 23 Jul 2018 16:57:53 +0800 Subject: [PATCH 8/8] use includes() and add 'markup' naming for function --- lib/server/blog.js | 8 ++++---- lib/server/docs.js | 13 +++++-------- lib/server/generate.js | 16 ++++++++-------- lib/server/server.js | 7 +++---- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/server/blog.js b/lib/server/blog.js index c8b4cf10ac41..0c218efc8034 100644 --- a/lib/server/blog.js +++ b/lib/server/blog.js @@ -32,7 +32,7 @@ function fileToUrl(file) { .replace(/\.md$/, '.html'); } -function getPages(numOfBlog, config) { +function getPagesMarkup(numOfBlog, config) { const BlogPageLayout = require('../core/BlogPageLayout.js'); const blogPages = {}; const perPage = 10; @@ -63,7 +63,7 @@ function getMetadata(file) { return metadata; } -function getPost(file, config) { +function getPostMarkup(file, config) { const metadata = getMetadata(file); if (!metadata) { return null; @@ -80,7 +80,7 @@ function getPost(file, config) { module.exports = { fileToUrl, getMetadata, - getPages, - getPost, + getPagesMarkup, + getPostMarkup, urlToSource, }; diff --git a/lib/server/docs.js b/lib/server/docs.js index df8e2aae4e95..e6159fb16ee2 100644 --- a/lib/server/docs.js +++ b/lib/server/docs.js @@ -83,7 +83,7 @@ function replaceAssetsLink(oldContent) { return lines.join('\n'); } -function getStr(rawContent, mdToHtml, metadata) { +function getMarkup(rawContent, mdToHtml, metadata) { // generate table of contents let content = insertTOC(rawContent); @@ -104,11 +104,8 @@ function getStr(rawContent, mdToHtml, metadata) { ); } -function getRedirectStr(metadata) { - if ( - !env.translation.enabled || - metadata.permalink.indexOf('docs/en') === -1 - ) { +function getRedirectMarkup(metadata) { + if (!env.translation.enabled || !metadata.permalink.includes('docs/en')) { return null; } const Redirect = require('../core/Redirect.js'); @@ -124,9 +121,9 @@ function getRedirectStr(metadata) { } module.exports = { - getStr, + getMarkup, getFile, - getRedirectStr, + getRedirectMarkup, mdToHtmlify, replaceAssetsLink, }; diff --git a/lib/server/generate.js b/lib/server/generate.js index d30e004ebcdf..8f4e702d7ccd 100644 --- a/lib/server/generate.js +++ b/lib/server/generate.js @@ -76,20 +76,20 @@ async function execute() { return; } const rawContent = metadataUtils.extractMetadata(file).rawContent; - const str = docs.getStr(rawContent, mdToHtml, metadata); + const str = docs.getMarkup(rawContent, mdToHtml, metadata); const targetFile = join(buildDir, metadata.permalink); writeFileAndCreateFolder(targetFile, str); // generate english page redirects when languages are enabled - const redirectStr = docs.getRedirectStr(metadata); - if (!redirectStr) { + const redirectMarkup = docs.getRedirectMarkup(metadata); + if (!redirectMarkup) { return; } const redirectFile = join( buildDir, metadata.permalink.replace('docs/en', 'docs') ); - writeFileAndCreateFolder(redirectFile, redirectStr); + writeFileAndCreateFolder(redirectFile, redirectMarkup); }); // copy docs assets if they exist @@ -120,16 +120,16 @@ async function execute() { return; } const urlPath = blog.fileToUrl(normalizedFile); - const str = blog.getPost(normalizedFile, siteConfig); - if (!str) { + const blogPost = blog.getPostMarkup(normalizedFile, siteConfig); + if (!blogPost) { return; } const targetFile = join(buildDir, 'blog', urlPath); - writeFileAndCreateFolder(targetFile, str); + writeFileAndCreateFolder(targetFile, blogPost); }); // create html files for all blog pages (collections of article previews) - const blogPages = blog.getPages(MetadataBlog.length, siteConfig); + const blogPages = blog.getPagesMarkup(MetadataBlog.length, siteConfig); Object.keys(blogPages).forEach(pagePath => { const targetFile = join(buildDir, 'blog', pagePath); writeFileAndCreateFolder(targetFile, blogPages[pagePath]); diff --git a/lib/server/server.js b/lib/server/server.js index 070c5cb1275d..7ca679f0d59d 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -146,8 +146,7 @@ function execute(port, options) { const rawContent = metadataUtils.extractMetadata(file).rawContent; removeModuleAndChildrenFromCache('../core/DocsLayout.js'); const mdToHtml = metadataUtils.mdToHtml(Metadata, siteConfig.baseUrl); - const str = docs.getStr(rawContent, mdToHtml, metadata); - res.send(str); + res.send(docs.getMarkup(rawContent, mdToHtml, metadata)); }); app.get(routing.sitemap(siteConfig.baseUrl), (req, res) => { @@ -180,7 +179,7 @@ function execute(port, options) { // this to regenerate on file save rather than on page request. reloadMetadataBlog(); removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPageLayout.js')); - const blogPages = blog.getPages(MetadataBlog.length, siteConfig); + const blogPages = blog.getPagesMarkup(MetadataBlog.length, siteConfig); const urlPath = req.path.toString().split('blog/')[1]; if (urlPath === 'index.html') { @@ -192,7 +191,7 @@ function execute(port, options) { } else { const file = join(CWD, 'blog', blog.urlToSource(urlPath)); removeModuleAndChildrenFromCache(join('..', 'core', 'BlogPostLayout.js')); - const blogPost = blog.getPost(file, siteConfig); + const blogPost = blog.getPostMarkup(file, siteConfig); if (!blogPost) { next(); return;