mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 16:17:25 +02:00
Update doc metadata to use file name as id fallback and id as title fallback
This commit is contained in:
parent
db7a7394b3
commit
964e9e2108
5 changed files with 56 additions and 27 deletions
|
@ -11,7 +11,6 @@ const CWD = process.cwd();
|
|||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const os = require("os");
|
||||
const glob = require("glob");
|
||||
const siteConfig = require(CWD + "/siteConfig.js");
|
||||
const versionFallback = require("./versionFallback.js");
|
||||
|
@ -71,7 +70,7 @@ function readSidebar() {
|
|||
}
|
||||
|
||||
function splitHeader(content) {
|
||||
const lines = content.split(os.EOL);
|
||||
const lines = content.split("\n");
|
||||
let i = 1;
|
||||
for (; i < lines.length - 1; ++i) {
|
||||
if (lines[i] === "---") {
|
||||
|
@ -88,12 +87,15 @@ function splitHeader(content) {
|
|||
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("\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();
|
||||
// Handle the case where you have "Community #10"
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (e) {}
|
||||
|
@ -105,9 +107,6 @@ function extractMetadata(content) {
|
|||
// process the metadata for a document found in the docs folder
|
||||
function processMetadata(file) {
|
||||
const result = extractMetadata(fs.readFileSync(file, "utf8"));
|
||||
if (!result.metadata || !result.rawContent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const regexSubFolder = /docs\/(.*)\/.*/;
|
||||
|
||||
|
@ -121,6 +120,16 @@ function processMetadata(file) {
|
|||
const rawContent = result.rawContent;
|
||||
metadata.source = path.basename(file);
|
||||
|
||||
if (!metadata.id) {
|
||||
metadata.id = path.basename(file, path.extname(file));
|
||||
}
|
||||
if (metadata.id.includes("/") || metadata.id.includes(".")) {
|
||||
throw new Error('Document id cannot include "/" or ".".');
|
||||
}
|
||||
if (!metadata.title) {
|
||||
metadata.title = metadata.id;
|
||||
}
|
||||
|
||||
if (languages.length === 1 && !siteConfig.useEnglishUrl) {
|
||||
metadata.permalink = "docs/" + metadata.id + ".html";
|
||||
} else {
|
||||
|
@ -221,21 +230,23 @@ function generateDocsMetadata() {
|
|||
const versionData = versionFallback.docData();
|
||||
versionData.forEach(metadata => {
|
||||
const id = metadata.localized_id;
|
||||
metadata.sidebar = order[id].sidebar;
|
||||
metadata.category = order[id].category;
|
||||
if (order[id].next) {
|
||||
metadata.next_id = order[id].next.replace(
|
||||
"version-" + metadata.version + "-",
|
||||
""
|
||||
);
|
||||
metadata.next = metadata.language + "-" + order[id].next;
|
||||
}
|
||||
if (order[id].previous) {
|
||||
metadata.previous_id = order[id].previous.replace(
|
||||
"version-" + metadata.version + "-",
|
||||
""
|
||||
);
|
||||
metadata.previous = metadata.language + "-" + order[id].previous;
|
||||
if (order[id]) {
|
||||
metadata.sidebar = order[id].sidebar;
|
||||
metadata.category = order[id].category;
|
||||
if (order[id].next) {
|
||||
metadata.next_id = order[id].next.replace(
|
||||
"version-" + metadata.version + "-",
|
||||
""
|
||||
);
|
||||
metadata.next = metadata.language + "-" + order[id].next;
|
||||
}
|
||||
if (order[id].previous) {
|
||||
metadata.previous_id = order[id].previous.replace(
|
||||
"version-" + metadata.version + "-",
|
||||
""
|
||||
);
|
||||
metadata.previous = metadata.language + "-" + order[id].previous;
|
||||
}
|
||||
}
|
||||
metadatas[metadata.id] = metadata;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue