fix(content-docs): restore functionality when a category only has index page (#7385)

* fix(content-docs): restore functionality when a category only has index page

* use this internally
This commit is contained in:
Joshua Chen 2022-05-10 14:50:43 +08:00 committed by GitHub
parent c3880cc342
commit 6e10a48059
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 258 additions and 46 deletions

View file

@ -0,0 +1,23 @@
{
"docs": [
{
"label": "Tutorials",
"type": "category",
"items": [
{
"type": "autogenerated",
"dirName": "tutorials"
}
]
},
{
"label": "index-only",
"type": "category",
"link": {
"type": "doc",
"id": "tutorials/tutorial-basics"
},
"items": []
}
]
}

View file

@ -0,0 +1,60 @@
{
"sidebar": [
"draft1",
{
"type": "category",
"label": "all drafts",
"items": [
"draft2",
"draft3"
]
},
{
"type": "category",
"label": "all drafts",
"link": {
"type": "generated-index"
},
"items": [
"draft2",
"draft3"
]
},
{
"type": "category",
"label": "all drafts",
"link": {
"type": "doc",
"id": "draft1"
},
"items": [
"draft2",
"draft3"
]
},
{
"type": "category",
"label": "index not draft",
"link": {
"type": "doc",
"id": "not-draft"
},
"items": [
"draft2",
"draft3"
]
},
{
"type": "category",
"label": "subitem not draft",
"link": {
"type": "doc",
"id": "draft1"
},
"items": [
"not-draft",
"draft3"
]
}
]
}

View file

@ -1,5 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`loadSidebars loads sidebars with index-only categories 1`] = `
{
"docs": [
{
"collapsed": true,
"collapsible": true,
"items": [
{
"id": "tutorials/tutorial-basics",
"label": "tutorial-basics",
"type": "doc",
},
],
"label": "Tutorials",
"link": undefined,
"type": "category",
},
{
"id": "tutorials/tutorial-basics",
"label": "index-only",
"type": "doc",
},
],
}
`;
exports[`loadSidebars loads sidebars with interspersed draft items 1`] = `
{
"sidebar": [
{
"id": "not-draft",
"label": "index not draft",
"type": "doc",
},
{
"collapsed": true,
"collapsible": true,
"items": [
{
"id": "not-draft",
"type": "doc",
},
],
"label": "subitem not draft",
"link": undefined,
"type": "category",
},
],
}
`;
exports[`loadSidebars sidebars link 1`] = `
{
"docs": [

View file

@ -60,14 +60,21 @@ exports[`postProcess corrects collapsed state inconsistencies 3`] = `
}
`;
exports[`postProcess transforms category without subitems 1`] = `
exports[`postProcess filters draft items 1`] = `
{
"sidebar": [
{
"href": "version/generated/permalink",
"id": "another",
"label": "Category",
"type": "link",
"type": "doc",
},
],
}
`;
exports[`postProcess transforms category without subitems 1`] = `
{
"sidebar": [
{
"id": "doc ID",
"label": "Category 2",

View file

@ -7,6 +7,7 @@
import {jest} from '@jest/globals';
import path from 'path';
import {createSlugger} from '@docusaurus/utils';
import {loadSidebars, DisabledSidebars} from '../index';
import type {SidebarProcessorParams} from '../types';
import {DefaultSidebarItemsGenerator} from '../generator';
@ -27,6 +28,7 @@ describe('loadSidebars', () => {
],
drafts: [],
version: {
path: 'version',
contentPath: path.join(fixtureDir, 'docs'),
contentPathLocalized: path.join(fixtureDir, 'docs'),
},
@ -124,6 +126,32 @@ describe('loadSidebars', () => {
expect(result).toMatchSnapshot();
});
it('loads sidebars with index-only categories', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-category-index.json');
const result = await loadSidebars(sidebarPath, {
...params,
docs: [
{
id: 'tutorials/tutorial-basics',
source: '@site/docs/tutorials/tutorial-basics/index.md',
sourceDirName: 'tutorials/tutorial-basics',
frontMatter: {},
},
],
});
expect(result).toMatchSnapshot();
});
it('loads sidebars with interspersed draft items', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-drafts.json');
const result = await loadSidebars(sidebarPath, {
...params,
drafts: [{id: 'draft1'}, {id: 'draft2'}, {id: 'draft3'}],
categoryLabelSlugger: createSlugger(),
});
expect(result).toMatchSnapshot();
});
it('duplicate category metadata files', async () => {
const sidebarPath = path.join(
fixtureDir,

View file

@ -35,6 +35,7 @@ describe('postProcess', () => {
{
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [],
},
);
@ -54,6 +55,7 @@ describe('postProcess', () => {
{
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [],
},
);
}).toThrowErrorMatchingInlineSnapshot(
@ -79,6 +81,7 @@ describe('postProcess', () => {
{
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [],
},
),
).toMatchSnapshot();
@ -99,6 +102,7 @@ describe('postProcess', () => {
{
sidebarOptions: {sidebarCollapsed: false, sidebarCollapsible: false},
version: {path: 'version'},
drafts: [],
},
),
).toMatchSnapshot();
@ -118,6 +122,37 @@ describe('postProcess', () => {
{
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: false},
version: {path: 'version'},
drafts: [],
},
),
).toMatchSnapshot();
});
it('filters draft items', () => {
expect(
postProcessSidebars(
{
sidebar: [
{
type: 'category',
label: 'Category',
items: [{type: 'doc', id: 'foo'}],
},
{
type: 'category',
label: 'Category',
link: {
type: 'doc',
id: 'another',
},
items: [{type: 'doc', id: 'foo'}],
},
],
},
{
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
version: {path: 'version'},
drafts: [{id: 'foo', unversionedId: 'foo'}],
},
),
).toMatchSnapshot();