-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(server): share logic between server.js & generate.js (#856)
* nits too many comments * Refactor to blog.getPages * Refactor to getPost, fileToUrl, urlToFile * Refactor redirectcomponent generation for docs * nits & fix typo * Refactor to blog.getMetadata * Add test for getMetadata, fileToSUrl and urlToSource * use includes() and add 'markup' naming for function
- Loading branch information
Showing
8 changed files
with
248 additions
and
213 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
lib/server/__tests__/__fixtures__/2018-08-17-docusaurus.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.