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

Refactor(server): share logic between server.js & generate.js #856

Merged
merged 10 commits into from
Jul 24, 2018
11 changes: 11 additions & 0 deletions lib/server/__tests__/__fixtures__/2018-08-17-docusaurus.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions lib/server/__tests__/__snapshots__/blog.test.js.snap
Original file line number Diff line number Diff line change
@@ -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",
}
`;
68 changes: 68 additions & 0 deletions lib/server/__tests__/blog.test.js
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
86 changes: 86 additions & 0 deletions lib/server/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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 path = require('path');
const fs = require('fs-extra');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
const metadataUtils = require('./metadataUtils');

function urlToSource(url) {
if (!url || typeof url !== 'string') {
return null;
}
return url
.replace(/\/index.html$/, '.md')
.replace(/\.html$/, '.md')
.replace(new RegExp('/', 'g'), '-');
}

function fileToUrl(file) {
if (!file || !fs.existsSync(file) || typeof file !== 'string') {
return null;
}
return path
.basename(file)
.replace('-', '/')
.replace('-', '/')
.replace('-', '/')
.replace(/\.md$/, '.html');
}

function getPagesMarkup(numOfBlog, config) {
const BlogPageLayout = require('../core/BlogPageLayout.js');
const blogPages = {};
const perPage = 10;
for (let page = 0; page < Math.ceil(numOfBlog / perPage); page++) {
const metadata = {page, perPage};
const blogPageComp = (
<BlogPageLayout metadata={metadata} language="en" config={config} />
);
const str = renderToStaticMarkupWithDoctype(blogPageComp);
const pagePath = `${page > 0 ? `page${page + 1}` : ''}/index.html`;
blogPages[pagePath] = str;
}
return blogPages;
}

function getMetadata(file) {
if (!file || !fs.existsSync(file)) {
return null;
}
const result = metadataUtils.extractMetadata(
fs.readFileSync(file, {encoding: 'utf8'})
);
const metadata = Object.assign(
{path: fileToUrl(file), content: result.rawContent},
result.metadata
);
metadata.id = metadata.title;
return metadata;
}

function getPostMarkup(file, config) {
const metadata = getMetadata(file);
if (!metadata) {
return null;
}
const BlogPostLayout = require('../core/BlogPostLayout.js');
const blogPostComp = (
<BlogPostLayout metadata={metadata} language="en" config={config}>
{metadata.content}
</BlogPostLayout>
);
return renderToStaticMarkupWithDoctype(blogPostComp);
}

module.exports = {
fileToUrl,
getMetadata,
getPagesMarkup,
getPostMarkup,
urlToSource,
};
24 changes: 21 additions & 3 deletions lib/server/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -82,7 +83,7 @@ function replaceAssetsLink(oldContent) {
return lines.join('\n');
}

function getComponent(rawContent, mdToHtml, metadata) {
function getMarkup(rawContent, mdToHtml, metadata) {
// generate table of contents
let content = insertTOC(rawContent);

Expand All @@ -93,7 +94,7 @@ function getComponent(rawContent, mdToHtml, metadata) {
content = replaceAssetsLink(content);

const DocsLayout = require('../core/DocsLayout.js');
return (
return renderToStaticMarkupWithDoctype(
<DocsLayout
metadata={metadata}
language={metadata.language}
Expand All @@ -103,9 +104,26 @@ function getComponent(rawContent, mdToHtml, metadata) {
);
}

function getRedirectMarkup(metadata) {
if (!env.translation.enabled || !metadata.permalink.includes('docs/en')) {
return null;
}
const Redirect = require('../core/Redirect.js');
const redirectlink = getPath(metadata.permalink, siteConfig.cleanUrl);
return renderToStaticMarkupWithDoctype(
<Redirect
metadata={metadata}
language={metadata.language}
config={siteConfig}
redirect={siteConfig.baseUrl + redirectlink}
/>
);
}

module.exports = {
getComponent,
getMarkup,
getFile,
getRedirectMarkup,
mdToHtmlify,
replaceAssetsLink,
};
Loading