mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
feat(v2): support external links and linking to docs from other sidebars (#1052)
* feat(sidebar): support external links and linking to docs from other sidebars * Update styles.css
This commit is contained in:
parent
edde297504
commit
a2d3f26722
8 changed files with 466 additions and 139 deletions
|
@ -1,52 +1,66 @@
|
|||
// build the docs meta such as next, previous, category and sidebar
|
||||
|
||||
module.exports = function createOrder(allSidebars = {}) {
|
||||
const order = {};
|
||||
if (!allSidebars) {
|
||||
return order;
|
||||
}
|
||||
Object.keys(allSidebars).forEach(sidebar => {
|
||||
const categories = allSidebars[sidebar];
|
||||
|
||||
let ids = [];
|
||||
Object.keys(allSidebars).forEach(sidebarId => {
|
||||
const sidebar = allSidebars[sidebarId];
|
||||
|
||||
const ids = [];
|
||||
const categoryOrder = [];
|
||||
const subCategoryOrder = [];
|
||||
Object.keys(categories).forEach(category => {
|
||||
if (Array.isArray(categories[category])) {
|
||||
ids = ids.concat(categories[category]);
|
||||
|
||||
// eslint-disable-next-line
|
||||
for (let i = 0; i < categories[category].length; i++) {
|
||||
categoryOrder.push(category);
|
||||
subCategoryOrder.push(undefined);
|
||||
const indexItems = ({items, categoryLabel, subCategoryLabel}) => {
|
||||
items.forEach(item => {
|
||||
switch (item.type) {
|
||||
case 'category':
|
||||
indexItems({
|
||||
items: item.items,
|
||||
categoryLabel: categoryLabel || item.label,
|
||||
subCategoryLabel: categoryLabel && item.label,
|
||||
});
|
||||
break;
|
||||
case 'ref':
|
||||
case 'link':
|
||||
// refs and links should not be shown in navigation
|
||||
break;
|
||||
case 'doc':
|
||||
ids.push(item.id);
|
||||
categoryOrder.push(categoryLabel);
|
||||
subCategoryOrder.push(subCategoryLabel);
|
||||
break;
|
||||
default:
|
||||
throw new Error(
|
||||
`Unknown item type: ${item.type}. Item: ${JSON.stringify(item)}`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
Object.keys(categories[category]).forEach(subCategory => {
|
||||
ids = ids.concat(categories[category][subCategory]);
|
||||
});
|
||||
};
|
||||
|
||||
// eslint-disable-next-line
|
||||
for (let i = 0; i < categories[category][subCategory].length; i++) {
|
||||
categoryOrder.push(category);
|
||||
subCategoryOrder.push(subCategory);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
indexItems({items: sidebar});
|
||||
|
||||
// eslint-disable-next-line
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
const id = ids[i];
|
||||
let previous;
|
||||
let next;
|
||||
if (i > 0) previous = ids[i - 1];
|
||||
if (i < ids.length - 1) next = ids[i + 1];
|
||||
|
||||
if (i > 0) {
|
||||
previous = ids[i - 1];
|
||||
}
|
||||
|
||||
if (i < ids.length - 1) {
|
||||
next = ids[i + 1];
|
||||
}
|
||||
|
||||
order[id] = {
|
||||
previous,
|
||||
next,
|
||||
sidebar,
|
||||
sidebar: sidebarId,
|
||||
category: categoryOrder[i],
|
||||
subCategory: subCategoryOrder[i],
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return order;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue