mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-03 03:12:35 +02:00
refactor: rename loader to load as not to confuse webpack loader
This commit is contained in:
parent
36eee2941a
commit
9fef99cb18
15 changed files with 4 additions and 131 deletions
47
lib/load/blog.js
Normal file
47
lib/load/blog.js
Normal file
|
@ -0,0 +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 mdRE = /\.md$/;
|
||||
|
||||
function fileToPath(file) {
|
||||
if (indexRE.test(file)) {
|
||||
return file.replace(indexRE, '/$1');
|
||||
}
|
||||
return `/${file.replace(mdRE, '').replace(/\\/g, '/')}`;
|
||||
}
|
||||
|
||||
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(blogDir) {
|
||||
const blogFiles = await globby(['**/*.md'], {
|
||||
cwd: blogDir
|
||||
});
|
||||
|
||||
const blogDatas = await Promise.all(
|
||||
blogFiles.map(async file => {
|
||||
const filepath = path.resolve(blogDir, file);
|
||||
const fileString = await fs.readFile(filepath, 'utf-8');
|
||||
const {metadata, content} = parse(fileString);
|
||||
|
||||
return {
|
||||
path: fileToPath(file),
|
||||
content,
|
||||
...metadata
|
||||
};
|
||||
})
|
||||
);
|
||||
blogDatas.sort((a, b) => b.date - a.date);
|
||||
return blogDatas;
|
||||
}
|
||||
|
||||
module.exports = loadBlog;
|
14
lib/load/config.js
Normal file
14
lib/load/config.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function loadConfig(siteDir, deleteCache = true) {
|
||||
const configPath = path.resolve(siteDir, 'siteConfig.js');
|
||||
if (deleteCache) {
|
||||
delete require.cache[configPath];
|
||||
}
|
||||
let config = {};
|
||||
if (fs.existsSync(configPath)) {
|
||||
config = require(configPath); // eslint-disable-line
|
||||
}
|
||||
return config;
|
||||
};
|
48
lib/load/docs.js
Normal file
48
lib/load/docs.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
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, '/')}`;
|
||||
}
|
||||
|
||||
function parse(fileString) {
|
||||
if (!fm.test(fileString)) {
|
||||
return {metadata: null, content: fileString};
|
||||
}
|
||||
const {attributes: metadata, body: content} = fm(fileString);
|
||||
|
||||
return {metadata, content};
|
||||
}
|
||||
|
||||
// still TODO. still copy paste from blog logic
|
||||
async function loadDocs(siteDir) {
|
||||
const blogFiles = await globby(['**/*.md'], {
|
||||
cwd: siteDir
|
||||
});
|
||||
|
||||
const blogDatas = await Promise.all(
|
||||
blogFiles.map(async file => {
|
||||
const filepath = path.resolve(siteDir, file);
|
||||
const fileString = await fs.readFile(filepath, 'utf-8');
|
||||
const {metadata, content} = parse(fileString);
|
||||
|
||||
return {
|
||||
path: fileToPath(file),
|
||||
content,
|
||||
...metadata
|
||||
};
|
||||
})
|
||||
);
|
||||
blogDatas.sort((a, b) => b.date - a.date);
|
||||
return blogDatas;
|
||||
}
|
||||
|
||||
module.exports = loadDocs;
|
58
lib/load/index.js
Normal file
58
lib/load/index.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const loadConfig = require('./config');
|
||||
const loadBlog = require('./blog');
|
||||
const loadDocs = require('./docs');
|
||||
const {generate} = require('../helpers');
|
||||
|
||||
module.exports = async function load(siteDir) {
|
||||
// load siteConfig
|
||||
const siteConfig = loadConfig(siteDir);
|
||||
|
||||
// docs
|
||||
const docsRelativeDir = siteConfig.customDocsPath || 'docs';
|
||||
const docsMetadata = await loadDocs(
|
||||
path.resolve(siteDir, '..', docsRelativeDir)
|
||||
);
|
||||
await generate(
|
||||
'docsMetadata.js',
|
||||
`${'/**\n * @generated\n */\n' + 'module.exports = '}${JSON.stringify(
|
||||
docsMetadata,
|
||||
null,
|
||||
2
|
||||
)};\n`
|
||||
);
|
||||
|
||||
// blog
|
||||
const blogMetadata = await loadBlog(path.resolve(siteDir, 'blog'));
|
||||
await generate(
|
||||
'blogMetadata.js',
|
||||
`${'/**\n * @generated\n */\n' + 'module.exports = '}${JSON.stringify(
|
||||
blogMetadata,
|
||||
null,
|
||||
2
|
||||
)};\n`
|
||||
);
|
||||
|
||||
// resolve outDir
|
||||
const outDir = siteConfig.dest
|
||||
? path.resolve(siteConfig.dest)
|
||||
: path.resolve(siteDir, '.munseo/dist');
|
||||
|
||||
// resolve the path of our app user interface layout
|
||||
const uiPath =
|
||||
!siteConfig.uiPath ||
|
||||
!fs.existsSync(path.resolve(siteDir, siteConfig.uiPath))
|
||||
? path.resolve(__dirname, '../ui')
|
||||
: siteConfig.uiPath;
|
||||
|
||||
const baseUrl = siteConfig.baseUrl || '/';
|
||||
|
||||
return {
|
||||
siteConfig,
|
||||
siteDir,
|
||||
outDir,
|
||||
uiPath,
|
||||
baseUrl
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue