mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
Revert "Add ability to specify sub categories in sidebar.json (#891)"
This reverts commit 16087b4428
.
This commit is contained in:
parent
16087b4428
commit
d2b30dc3ed
14 changed files with 86 additions and 533 deletions
|
@ -5,113 +5,102 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const {validateSidebar} = require('./utils');
|
||||
const fs = require('fs');
|
||||
|
||||
const Metadata = require('../core/metadata.js');
|
||||
|
||||
const CWD = process.cwd();
|
||||
let languages;
|
||||
if (fs.existsSync(`${CWD}/languages.js`)) {
|
||||
languages = require(`${CWD}/languages.js`);
|
||||
} else {
|
||||
languages = [
|
||||
{
|
||||
enabled: true,
|
||||
name: 'English',
|
||||
tag: 'en',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// returns data broken up into categories for a sidebar
|
||||
function readCategories(sidebar, allMetadata, languages) {
|
||||
function readCategories(sidebar) {
|
||||
const enabledLanguages = languages
|
||||
.filter(lang => lang.enabled)
|
||||
.map(lang => lang.tag);
|
||||
|
||||
const allCategories = {};
|
||||
|
||||
// Go through each language that might be defined
|
||||
for (let k = 0; k < enabledLanguages.length; ++k) {
|
||||
const language = enabledLanguages[k];
|
||||
const metadatas = [];
|
||||
const categories = [];
|
||||
const pages = {};
|
||||
|
||||
// Get the metadata for the current sidebar
|
||||
Object.keys(allMetadata).forEach(id => {
|
||||
const metadata = allMetadata[id];
|
||||
const metadatas = [];
|
||||
Object.keys(Metadata).forEach(id => {
|
||||
const metadata = Metadata[id];
|
||||
if (metadata.sidebar === sidebar && metadata.language === language) {
|
||||
metadatas.push(metadata);
|
||||
pages[metadata.id] = metadata;
|
||||
}
|
||||
});
|
||||
|
||||
// Sort the metadata
|
||||
metadatas.sort((a, b) => a.sort - b.sort);
|
||||
|
||||
// Store the correct sort of categories and sub categories for later
|
||||
const sortedCategories = [];
|
||||
const sortedSubCategories = [];
|
||||
// Build a hashmap of article_id -> metadata
|
||||
const articles = {};
|
||||
for (let i = 0; i < metadatas.length; ++i) {
|
||||
const metadata = metadatas[i];
|
||||
const category = metadata.category;
|
||||
const subCategory = metadata.sub_category;
|
||||
|
||||
if (!sortedCategories.includes(category)) {
|
||||
sortedCategories.push(category);
|
||||
}
|
||||
|
||||
if (subCategory && !sortedSubCategories.includes(subCategory)) {
|
||||
sortedSubCategories.push(subCategory);
|
||||
}
|
||||
articles[metadata.id] = metadata;
|
||||
}
|
||||
|
||||
// Index categories and sub categories with all of their documents
|
||||
const indexedCategories = {};
|
||||
const indexedSubCategories = {};
|
||||
for (let i = 0; i < metadatas.length; i++) {
|
||||
// Build a hashmap of article_id -> previous_id
|
||||
const previous = {};
|
||||
for (let i = 0; i < metadatas.length; ++i) {
|
||||
const metadata = metadatas[i];
|
||||
const category = metadata.category;
|
||||
const subCategory = metadata.sub_category;
|
||||
|
||||
// Validate pages in the sidebar
|
||||
validateSidebar(metadata, pages);
|
||||
|
||||
if (!indexedCategories[category]) {
|
||||
indexedCategories[category] = [];
|
||||
}
|
||||
|
||||
if (!subCategory) {
|
||||
indexedCategories[category].push(metadata);
|
||||
}
|
||||
|
||||
if (subCategory) {
|
||||
if (!indexedSubCategories[category]) {
|
||||
indexedSubCategories[category] = {};
|
||||
if (metadata.next) {
|
||||
if (!articles[metadata.next]) {
|
||||
throw new Error(
|
||||
metadata.version
|
||||
? `Improper sidebars file for version ${
|
||||
metadata.version
|
||||
}, document with id '${
|
||||
metadata.next
|
||||
}' not found. Make sure that all documents with ids specified in this version's sidebar file exist and that no ids are repeated.`
|
||||
: `Improper sidebars.json file, document with id '${
|
||||
metadata.next
|
||||
}' not found. Make sure that documents with the ids specified in sidebars.json exist and that no ids are repeated.`
|
||||
);
|
||||
}
|
||||
|
||||
if (!indexedSubCategories[category][subCategory]) {
|
||||
indexedSubCategories[category][subCategory] = [];
|
||||
}
|
||||
|
||||
indexedSubCategories[category][subCategory].push(metadata);
|
||||
previous[articles[metadata.next].id] = metadata.id;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate data for each category and sub categories
|
||||
for (let i = 0; i < sortedCategories.length; i++) {
|
||||
const category = sortedCategories[i];
|
||||
const currentCategory = {
|
||||
name: category,
|
||||
links: indexedCategories[category],
|
||||
};
|
||||
|
||||
for (let ii = 0; ii < sortedSubCategories.length; ii++) {
|
||||
const subCategory = sortedSubCategories[ii];
|
||||
|
||||
if (
|
||||
indexedSubCategories[category] &&
|
||||
indexedSubCategories[category][subCategory]
|
||||
) {
|
||||
if (!currentCategory.sub_categories) {
|
||||
currentCategory.sub_categories = [];
|
||||
}
|
||||
|
||||
currentCategory.sub_categories.push({
|
||||
name: subCategory,
|
||||
links: indexedSubCategories[category][subCategory],
|
||||
});
|
||||
}
|
||||
// Find the first element which doesn't have any previous
|
||||
let first = null;
|
||||
for (let i = 0; i < metadatas.length; ++i) {
|
||||
const metadata = metadatas[i];
|
||||
if (!previous[metadata.id]) {
|
||||
first = metadata;
|
||||
break;
|
||||
}
|
||||
|
||||
categories.push(currentCategory);
|
||||
}
|
||||
|
||||
const categories = [];
|
||||
let currentCategory = null;
|
||||
|
||||
let metadata = first;
|
||||
let i = 0;
|
||||
while (metadata && i++ < 1000) {
|
||||
if (!currentCategory || metadata.category !== currentCategory.name) {
|
||||
if (currentCategory) {
|
||||
categories.push(currentCategory);
|
||||
}
|
||||
currentCategory = {
|
||||
name: metadata.category,
|
||||
links: [],
|
||||
};
|
||||
}
|
||||
currentCategory.links.push(metadata);
|
||||
metadata = articles[metadata.next];
|
||||
}
|
||||
categories.push(currentCategory);
|
||||
|
||||
allCategories[language] = categories;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue