mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-16 10:37:20 +02:00
chore(loader): prettier & eslint
This commit is contained in:
parent
d2e12a8e61
commit
7116374adf
5 changed files with 108 additions and 118 deletions
|
@ -1,52 +1,47 @@
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fm = require('front-matter');
|
const fm = require('front-matter');
|
||||||
const globby = require('globby');
|
const globby = require('globby');
|
||||||
const indexRE = /(^|.*\/)index\.md$/i;
|
const indexRE = /(^|.*\/)index\.md$/i;
|
||||||
const extRE = /\.md$/;
|
const mdRE = /\.md$/;
|
||||||
|
|
||||||
function fileToPath(file) {
|
function fileToPath(file) {
|
||||||
if (indexRE.test(file)) {
|
if (indexRE.test(file)) {
|
||||||
// index.md -> /
|
return file.replace(indexRE, '/$1');
|
||||||
// foo/index.md -> /foo/
|
}
|
||||||
return file.replace(indexRE, '/$1');
|
return `/${file.replace(mdRE, '').replace(/\\/g, '/')}.html`;
|
||||||
} else {
|
}
|
||||||
// foo.md -> /foo.html
|
|
||||||
// foo/bar.md -> /foo/bar.html
|
function parse(fileString) {
|
||||||
return `/${file.replace(extRE, '').replace(/\\/g, '/')}.html`;
|
if (!fm.test(fileString)) {
|
||||||
}
|
return {metadata: null, content: fileString};
|
||||||
}
|
}
|
||||||
|
const {attributes: metadata, body: content} = fm(fileString);
|
||||||
function parse(fileString) {
|
|
||||||
if (!fm.test(fileString)) {
|
return {metadata, content};
|
||||||
return {metadata: null, content: fileString};
|
}
|
||||||
}
|
|
||||||
const {attributes: metadata, body: content} = fm(fileString);
|
async function loadBlog(sourceDir) {
|
||||||
|
const blogFiles = await globby(['**/*.md', '!.blogi', '!node_modules'], {
|
||||||
return {metadata, content};
|
cwd: sourceDir
|
||||||
}
|
});
|
||||||
|
|
||||||
async function loadBlog(sourceDir) {
|
const blogDatas = await Promise.all(
|
||||||
const blogFiles = await globby(['**/*.md', '!.blogi', '!node_modules'], {
|
blogFiles.map(async file => {
|
||||||
cwd: sourceDir
|
const filepath = path.resolve(sourceDir, file);
|
||||||
});
|
const fileString = await fs.readFile(filepath, 'utf-8');
|
||||||
|
const {metadata, content} = parse(fileString);
|
||||||
const blogDatas = await Promise.all(
|
|
||||||
blogFiles.map(async file => {
|
return {
|
||||||
const filepath = path.resolve(sourceDir, file);
|
path: fileToPath(file),
|
||||||
const fileString = await fs.readFile(filepath, 'utf-8');
|
content,
|
||||||
const {metadata, content} = parse(fileString);
|
title: metadata.title,
|
||||||
|
date: metadata.date
|
||||||
return {
|
};
|
||||||
path: fileToPath(file),
|
})
|
||||||
content: content,
|
);
|
||||||
title: metadata.title,
|
blogDatas.sort((a, b) => b.date - a.date);
|
||||||
date: metadata.date
|
return blogDatas;
|
||||||
};
|
}
|
||||||
})
|
|
||||||
);
|
module.exports = loadBlog;
|
||||||
blogDatas.sort((a, b) => b.date - a.date);
|
|
||||||
return blogDatas;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = loadBlog;
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = function loadConfig(sourceDir, deleteCache = true) {
|
module.exports = function loadConfig(sourceDir, deleteCache = true) {
|
||||||
const configPath = path.resolve(sourceDir, '.blogi', 'config.js');
|
const configPath = path.resolve(sourceDir, '.blogi', 'config.js');
|
||||||
if (deleteCache) {
|
if (deleteCache) {
|
||||||
delete require.cache[configPath];
|
delete require.cache[configPath];
|
||||||
}
|
}
|
||||||
let config = {};
|
let config = {};
|
||||||
if (fs.existsSync(configPath)) {
|
if (fs.existsSync(configPath)) {
|
||||||
config = require(configPath);
|
config = require(configPath); // eslint-disable-line
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
const path = require('path');
|
const loadConfig = require('./config');
|
||||||
const loadConfig = require('./config');
|
const loadBlog = require('./blog');
|
||||||
const loadBlog = require('./blog');
|
|
||||||
|
module.exports = async function load(sourceDir) {
|
||||||
module.exports = async function load(sourceDir) {
|
// 1. load siteConfig
|
||||||
// 1. load siteConfig
|
const siteConfig = loadConfig(sourceDir);
|
||||||
const siteConfig = loadConfig(sourceDir);
|
|
||||||
|
// 2. extract data from all blog files
|
||||||
// 2. extract data from all blog files
|
const blogDatas = await loadBlog(sourceDir);
|
||||||
const blogDatas = await loadBlog(sourceDir);
|
|
||||||
|
return {
|
||||||
// 3. TODO
|
siteConfig,
|
||||||
|
blogDatas
|
||||||
return {
|
};
|
||||||
siteConfig,
|
};
|
||||||
blogDatas
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
|
@ -1,18 +1,17 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const loadBlog = require('../../lib/loader/blog');
|
||||||
const loadBlog = require('../../lib/loader/blog');
|
|
||||||
|
describe('loadBlog', () => {
|
||||||
describe('loadBlog', () => {
|
const simpleDir = path.join(__dirname, '__fixtures__', 'simple');
|
||||||
const simpleDir = path.join(__dirname, '__fixtures__', 'simple');
|
const customDir = path.join(__dirname, '__fixtures__', 'custom');
|
||||||
const customDir = path.join(__dirname, '__fixtures__', 'custom');
|
|
||||||
|
test('simple', async () => {
|
||||||
test('simple', async () => {
|
const blogDatas = await loadBlog(simpleDir);
|
||||||
const blogDatas = await loadBlog(simpleDir);
|
expect(blogDatas).toMatchSnapshot();
|
||||||
expect(blogDatas).toMatchSnapshot();
|
});
|
||||||
});
|
|
||||||
|
test('custom', async () => {
|
||||||
test('custom', async () => {
|
const blogDatas = await loadBlog(customDir);
|
||||||
const blogDatas = await loadBlog(customDir);
|
expect(blogDatas).toMatchSnapshot();
|
||||||
expect(blogDatas).toMatchSnapshot();
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const loadConfig = require('../../lib/loader/config');
|
||||||
const loadConfig = require('../../lib/loader/config');
|
|
||||||
|
describe('loadConfig', () => {
|
||||||
describe('loadConfig', () => {
|
const simpleDir = path.join(__dirname, '__fixtures__', 'simple');
|
||||||
const simpleDir = path.join(__dirname, '__fixtures__', 'simple');
|
const customDir = path.join(__dirname, '__fixtures__', 'custom');
|
||||||
const customDir = path.join(__dirname, '__fixtures__', 'custom');
|
|
||||||
|
test('simple', () => {
|
||||||
test('simple', () => {
|
expect(loadConfig(simpleDir)).toMatchSnapshot();
|
||||||
expect(loadConfig(simpleDir)).toMatchSnapshot();
|
});
|
||||||
});
|
|
||||||
|
test('custom', () => {
|
||||||
test('custom', () => {
|
expect(loadConfig(customDir)).toMatchSnapshot();
|
||||||
expect(loadConfig(customDir)).toMatchSnapshot();
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue