mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-14 01:27:35 +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
|
@ -11,6 +11,8 @@ const fs = require('fs');
|
|||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
|
||||
const metadataUtils = require('./metadataUtils');
|
||||
|
||||
const env = require('./env.js');
|
||||
const utils = require('./utils.js');
|
||||
const siteConfig = require(CWD + '/siteConfig.js');
|
||||
|
@ -37,50 +39,6 @@ if (fs.existsSync(CWD + '/languages.js')) {
|
|||
];
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
// included to prevent cyclical dependency with readMetadata.js
|
||||
|
||||
function splitHeader(content) {
|
||||
const lines = content.split(/\r?\n/);
|
||||
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 no content returned, then that means there was no header, and both.header is the content
|
||||
if (!both.content) {
|
||||
return {metadata, rawContent: both.header};
|
||||
}
|
||||
const lines = both.header.split(/\r?\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};
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
const versionFolder = CWD + '/versioned_docs/';
|
||||
|
||||
// available stores doc ids of documents that are available for
|
||||
|
@ -95,7 +53,7 @@ files.forEach(file => {
|
|||
if (ext !== '.md' && ext !== '.markdown') {
|
||||
return;
|
||||
}
|
||||
const res = extractMetadata(fs.readFileSync(file, 'utf8'));
|
||||
const res = metadataUtils.extractMetadata(fs.readFileSync(file, 'utf8'));
|
||||
const metadata = res.metadata;
|
||||
|
||||
if (!metadata.original_id) {
|
||||
|
@ -186,15 +144,20 @@ function diffLatestDoc(file, id) {
|
|||
}
|
||||
|
||||
return (
|
||||
extractMetadata(fs.readFileSync(latestFile, 'utf8')).rawContent.trim() !==
|
||||
extractMetadata(fs.readFileSync(file, 'utf8')).rawContent.trim()
|
||||
metadataUtils
|
||||
.extractMetadata(fs.readFileSync(latestFile, 'utf8'))
|
||||
.rawContent.trim() !==
|
||||
metadataUtils
|
||||
.extractMetadata(fs.readFileSync(file, 'utf8'))
|
||||
.rawContent.trim()
|
||||
);
|
||||
}
|
||||
|
||||
// return metadata for a versioned file given the file, its version (requested),
|
||||
// the version of the file to be used, and its language
|
||||
function processVersionMetadata(file, version, useVersion, language) {
|
||||
const metadata = extractMetadata(fs.readFileSync(file, 'utf8')).metadata;
|
||||
const metadata = metadataUtils.extractMetadata(fs.readFileSync(file, 'utf8'))
|
||||
.metadata;
|
||||
|
||||
// Add subdirectory information to versioned_doc metadata
|
||||
// Example: `versioned_docs/version-1.1.6/projectA/readme.md` file with id `version-1.1.6-readme`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue