chore(loader): prettier & eslint

This commit is contained in:
endiliey 2018-07-28 18:48:37 +08:00
parent d2e12a8e61
commit 7116374adf
5 changed files with 108 additions and 118 deletions

View file

@ -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;

View file

@ -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;
};

View file

@ -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
};
};

View file

@ -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();
});
});

View file

@ -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();
});
});