diff --git a/lib/loader/blog.js b/lib/loader/blog.js index c654e67096..6bb413ef7a 100644 --- a/lib/loader/blog.js +++ b/lib/loader/blog.js @@ -1,52 +1,47 @@ -const fs = require('fs-extra'); -const path = require('path'); -const fm = require('front-matter'); -const globby = require('globby'); -const indexRE = /(^|.*\/)index\.md$/i; -const extRE = /\.md$/; - -function fileToPath(file) { - if (indexRE.test(file)) { - // index.md -> / - // foo/index.md -> /foo/ - return file.replace(indexRE, '/$1'); - } else { - // foo.md -> /foo.html - // foo/bar.md -> /foo/bar.html - return `/${file.replace(extRE, '').replace(/\\/g, '/')}.html`; - } -} - -function parse(fileString) { - if (!fm.test(fileString)) { - return {metadata: null, content: fileString}; - } - const {attributes: metadata, body: content} = fm(fileString); - - return {metadata, content}; -} - -async function loadBlog(sourceDir) { - const blogFiles = await globby(['**/*.md', '!.blogi', '!node_modules'], { - cwd: sourceDir - }); - - const blogDatas = await Promise.all( - blogFiles.map(async file => { - const filepath = path.resolve(sourceDir, file); - const fileString = await fs.readFile(filepath, 'utf-8'); - const {metadata, content} = parse(fileString); - - return { - path: fileToPath(file), - content: content, - title: metadata.title, - date: metadata.date - }; - }) - ); - blogDatas.sort((a, b) => b.date - a.date); - return blogDatas; -} - -module.exports = loadBlog; +const fs = require('fs-extra'); +const path = require('path'); +const fm = require('front-matter'); +const globby = require('globby'); +const indexRE = /(^|.*\/)index\.md$/i; +const mdRE = /\.md$/; + +function fileToPath(file) { + if (indexRE.test(file)) { + return file.replace(indexRE, '/$1'); + } + return `/${file.replace(mdRE, '').replace(/\\/g, '/')}.html`; +} + +function parse(fileString) { + if (!fm.test(fileString)) { + return {metadata: null, content: fileString}; + } + const {attributes: metadata, body: content} = fm(fileString); + + return {metadata, content}; +} + +async function loadBlog(sourceDir) { + const blogFiles = await globby(['**/*.md', '!.blogi', '!node_modules'], { + cwd: sourceDir + }); + + const blogDatas = await Promise.all( + blogFiles.map(async file => { + const filepath = path.resolve(sourceDir, file); + const fileString = await fs.readFile(filepath, 'utf-8'); + const {metadata, content} = parse(fileString); + + return { + path: fileToPath(file), + content, + title: metadata.title, + date: metadata.date + }; + }) + ); + blogDatas.sort((a, b) => b.date - a.date); + return blogDatas; +} + +module.exports = loadBlog; diff --git a/lib/loader/config.js b/lib/loader/config.js index 78ce9260d3..38fd0201f9 100644 --- a/lib/loader/config.js +++ b/lib/loader/config.js @@ -1,14 +1,14 @@ -const fs = require('fs-extra'); -const path = require('path'); - -module.exports = function loadConfig(sourceDir, deleteCache = true) { - const configPath = path.resolve(sourceDir, '.blogi', 'config.js'); - if (deleteCache) { - delete require.cache[configPath]; - } - let config = {}; - if (fs.existsSync(configPath)) { - config = require(configPath); - } - return config; -}; +const fs = require('fs-extra'); +const path = require('path'); + +module.exports = function loadConfig(sourceDir, deleteCache = true) { + const configPath = path.resolve(sourceDir, '.blogi', 'config.js'); + if (deleteCache) { + delete require.cache[configPath]; + } + let config = {}; + if (fs.existsSync(configPath)) { + config = require(configPath); // eslint-disable-line + } + return config; +}; diff --git a/lib/loader/index.js b/lib/loader/index.js index d983770936..3953b03a45 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -1,18 +1,15 @@ -const path = require('path'); -const loadConfig = require('./config'); -const loadBlog = require('./blog'); - -module.exports = async function load(sourceDir) { - // 1. load siteConfig - const siteConfig = loadConfig(sourceDir); - - // 2. extract data from all blog files - const blogDatas = await loadBlog(sourceDir); - - // 3. TODO - - return { - siteConfig, - blogDatas - }; -}; +const loadConfig = require('./config'); +const loadBlog = require('./blog'); + +module.exports = async function load(sourceDir) { + // 1. load siteConfig + const siteConfig = loadConfig(sourceDir); + + // 2. extract data from all blog files + const blogDatas = await loadBlog(sourceDir); + + return { + siteConfig, + blogDatas + }; +}; diff --git a/test/loader/blog.test.js b/test/loader/blog.test.js index df36780dbc..28f61e0b90 100644 --- a/test/loader/blog.test.js +++ b/test/loader/blog.test.js @@ -1,18 +1,17 @@ -const path = require('path'); -const fs = require('fs'); -const loadBlog = require('../../lib/loader/blog'); - -describe('loadBlog', () => { - const simpleDir = path.join(__dirname, '__fixtures__', 'simple'); - const customDir = path.join(__dirname, '__fixtures__', 'custom'); - - test('simple', async () => { - const blogDatas = await loadBlog(simpleDir); - expect(blogDatas).toMatchSnapshot(); - }); - - test('custom', async () => { - const blogDatas = await loadBlog(customDir); - expect(blogDatas).toMatchSnapshot(); - }); -}); +const path = require('path'); +const loadBlog = require('../../lib/loader/blog'); + +describe('loadBlog', () => { + const simpleDir = path.join(__dirname, '__fixtures__', 'simple'); + const customDir = path.join(__dirname, '__fixtures__', 'custom'); + + test('simple', async () => { + const blogDatas = await loadBlog(simpleDir); + expect(blogDatas).toMatchSnapshot(); + }); + + test('custom', async () => { + const blogDatas = await loadBlog(customDir); + expect(blogDatas).toMatchSnapshot(); + }); +}); diff --git a/test/loader/config.test.js b/test/loader/config.test.js index 6965ac2dd6..f1b137dbdf 100644 --- a/test/loader/config.test.js +++ b/test/loader/config.test.js @@ -1,16 +1,15 @@ -const path = require('path'); -const fs = require('fs'); -const loadConfig = require('../../lib/loader/config'); - -describe('loadConfig', () => { - const simpleDir = path.join(__dirname, '__fixtures__', 'simple'); - const customDir = path.join(__dirname, '__fixtures__', 'custom'); - - test('simple', () => { - expect(loadConfig(simpleDir)).toMatchSnapshot(); - }); - - test('custom', () => { - expect(loadConfig(customDir)).toMatchSnapshot(); - }); -}); +const path = require('path'); +const loadConfig = require('../../lib/loader/config'); + +describe('loadConfig', () => { + const simpleDir = path.join(__dirname, '__fixtures__', 'simple'); + const customDir = path.join(__dirname, '__fixtures__', 'custom'); + + test('simple', () => { + expect(loadConfig(simpleDir)).toMatchSnapshot(); + }); + + test('custom', () => { + expect(loadConfig(customDir)).toMatchSnapshot(); + }); +});