mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-25 05:57:53 +02:00
* Duplicate code in readMetadata.js & versionFallback.js #725 * Putting back package-lock.json * Rename lib/server/utilsMetadata.js -> lib/server/metadataUtils.js * Update splitHeader + extractMetadata * Update prettier
This commit is contained in:
parent
de8da1ca54
commit
a77ae958db
7 changed files with 100 additions and 101 deletions
|
@ -12,6 +12,8 @@ const fs = require('fs');
|
|||
const glob = require('glob');
|
||||
const chalk = require('chalk');
|
||||
|
||||
const metadataUtils = require('./metadataUtils');
|
||||
|
||||
const env = require('./env.js');
|
||||
const siteConfig = require(CWD + '/siteConfig.js');
|
||||
const versionFallback = require('./versionFallback.js');
|
||||
|
@ -79,52 +81,11 @@ function readSidebar() {
|
|||
return order;
|
||||
}
|
||||
|
||||
// split markdown header
|
||||
function splitHeader(content) {
|
||||
// New line characters need to handle all operating systems.
|
||||
const lines = content.split(/\r?\n/);
|
||||
if (lines[0] !== '---') {
|
||||
return {};
|
||||
}
|
||||
let i = 1;
|
||||
for (; i < lines.length - 1; ++i) {
|
||||
if (lines[i] === '---') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return {
|
||||
header: lines.slice(1, i + 1).join('\n'),
|
||||
content: lines.slice(i + 1).join('\n'),
|
||||
};
|
||||
}
|
||||
|
||||
// Extract markdown metadata header
|
||||
function extractMetadata(content) {
|
||||
const metadata = {};
|
||||
const both = splitHeader(content);
|
||||
if (Object.keys(both).length === 0) {
|
||||
return {metadata, rawContent: content};
|
||||
}
|
||||
const lines = both.header.split('\n');
|
||||
for (let i = 0; i < lines.length - 1; ++i) {
|
||||
const keyvalue = lines[i].split(':');
|
||||
const key = keyvalue[0].trim();
|
||||
let value = keyvalue
|
||||
.slice(1)
|
||||
.join(':')
|
||||
.trim();
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (e) {}
|
||||
metadata[key] = value;
|
||||
}
|
||||
return {metadata, rawContent: both.content};
|
||||
}
|
||||
|
||||
// process the metadata for a document found in either 'docs' or 'translated_docs'
|
||||
function processMetadata(file, refDir) {
|
||||
const result = extractMetadata(fs.readFileSync(file, 'utf8'));
|
||||
const result = metadataUtils.extractMetadata(fs.readFileSync(file, 'utf8'));
|
||||
const language = utils.getLanguage(file, refDir) || 'en';
|
||||
|
||||
const metadata = {};
|
||||
for (const fieldName of Object.keys(result.metadata)) {
|
||||
if (SupportedHeaderFields.has(fieldName)) {
|
||||
|
@ -367,7 +328,9 @@ function generateMetadataBlog() {
|
|||
.replace('-', '/')
|
||||
.replace('-', '/')
|
||||
.replace(/\.md$/, '.html');
|
||||
const result = extractMetadata(fs.readFileSync(file, {encoding: 'utf8'}));
|
||||
const result = metadataUtils.extractMetadata(
|
||||
fs.readFileSync(file, {encoding: 'utf8'})
|
||||
);
|
||||
const rawContent = result.rawContent;
|
||||
const metadata = Object.assign(
|
||||
{path: filePath, content: rawContent},
|
||||
|
@ -414,7 +377,6 @@ function generateMetadataBlog() {
|
|||
module.exports = {
|
||||
getDocsPath,
|
||||
readSidebar,
|
||||
extractMetadata,
|
||||
processMetadata,
|
||||
generateMetadataDocs,
|
||||
generateMetadataBlog,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue