mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-01 10:22:30 +02:00
fix: change subcategory format (#1026)
* fix: change subcategory format * Fix sidebars * Refactor * Fix implementation * Change format
This commit is contained in:
parent
c277f46a60
commit
fe500dea82
19 changed files with 493 additions and 15047 deletions
|
@ -12,7 +12,7 @@ module.exports = {
|
|||
next_id: 'doc2',
|
||||
next: 'en-doc2',
|
||||
next_title: 'Document 2',
|
||||
sub_category: 'Sub Cat 1',
|
||||
subcategory: 'Sub Cat 1',
|
||||
sort: 1,
|
||||
},
|
||||
'en-doc2': {
|
||||
|
@ -28,7 +28,7 @@ module.exports = {
|
|||
previous_id: 'doc1',
|
||||
previous: 'en-doc1',
|
||||
previous_title: 'Document 1',
|
||||
sub_category: 'Sub Cat 1',
|
||||
subcategory: 'Sub Cat 1',
|
||||
sort: 2,
|
||||
},
|
||||
'en-doc3': {
|
||||
|
@ -44,7 +44,7 @@ module.exports = {
|
|||
previous_id: 'doc2',
|
||||
previous: 'en-doc2',
|
||||
previous_title: 'Document 2',
|
||||
sub_category: 'Sub Cat 2',
|
||||
subcategory: 'Sub Cat 2',
|
||||
sort: 3,
|
||||
},
|
||||
'en-doc4': {
|
||||
|
|
|
@ -1,11 +1,26 @@
|
|||
module.exports = {
|
||||
docs: {
|
||||
'First Category': {
|
||||
'Sub Cat One': ['doc2', 'doc1'],
|
||||
'Sub Cat Two': ['doc3', 'doc5'],
|
||||
},
|
||||
'Second Category': {
|
||||
Hello: ['doc4'],
|
||||
},
|
||||
'First Category': ['doc1', 'doc2'],
|
||||
'Second Category': [
|
||||
'doc3',
|
||||
{
|
||||
type: 'subcategory',
|
||||
label: 'First Subcategory',
|
||||
ids: ['doc4'],
|
||||
},
|
||||
'doc5',
|
||||
],
|
||||
'Third Category': [
|
||||
{
|
||||
type: 'subcategory',
|
||||
label: 'Second Subcategory',
|
||||
ids: ['doc6', 'doc7'],
|
||||
},
|
||||
{
|
||||
type: 'subcategory',
|
||||
label: 'Third Subcategory',
|
||||
ids: ['doc8'],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
|
|
@ -0,0 +1,176 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`readCategories should return proper categories and their pages 1`] = `
|
||||
Array [
|
||||
Object {
|
||||
"children": Array [
|
||||
Object {
|
||||
"item": Object {
|
||||
"category": "Test",
|
||||
"id": "en-doc1",
|
||||
"language": "en",
|
||||
"localized_id": "doc1",
|
||||
"next": "en-doc2",
|
||||
"next_id": "doc2",
|
||||
"next_title": "Document 2",
|
||||
"permalink": "docs/en/next/doc1.html",
|
||||
"sidebar": "docs",
|
||||
"sort": 1,
|
||||
"source": "doc1.md",
|
||||
"title": "Document 1",
|
||||
"version": "next",
|
||||
},
|
||||
"type": "LINK",
|
||||
},
|
||||
Object {
|
||||
"item": Object {
|
||||
"category": "Test",
|
||||
"id": "en-doc2",
|
||||
"language": "en",
|
||||
"localized_id": "doc2",
|
||||
"permalink": "docs/en/next/doc2.html",
|
||||
"previous": "en-doc1",
|
||||
"previous_id": "doc1",
|
||||
"previous_title": "Document 1",
|
||||
"sidebar": "docs",
|
||||
"sort": 2,
|
||||
"source": "doc2.md",
|
||||
"title": "Document 2",
|
||||
"version": "next",
|
||||
},
|
||||
"type": "LINK",
|
||||
},
|
||||
],
|
||||
"title": "Test",
|
||||
"type": "CATEGORY",
|
||||
},
|
||||
Object {
|
||||
"children": Array [
|
||||
Object {
|
||||
"item": Object {
|
||||
"category": "Test 2",
|
||||
"id": "en-doc3",
|
||||
"language": "en",
|
||||
"localized_id": "doc3",
|
||||
"permalink": "docs/en/next/doc3.html",
|
||||
"previous": "en-doc2",
|
||||
"previous_id": "doc2",
|
||||
"previous_title": "Document 2",
|
||||
"sidebar": "docs",
|
||||
"sort": 3,
|
||||
"source": "doc3.md",
|
||||
"title": "Document 3",
|
||||
"version": "next",
|
||||
},
|
||||
"type": "LINK",
|
||||
},
|
||||
],
|
||||
"title": "Test 2",
|
||||
"type": "CATEGORY",
|
||||
},
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`readCategories should return proper data with categories and sub categories 1`] = `
|
||||
Array [
|
||||
Object {
|
||||
"children": Array [
|
||||
Object {
|
||||
"children": Array [
|
||||
Object {
|
||||
"item": Object {
|
||||
"category": "Test",
|
||||
"id": "en-doc1",
|
||||
"language": "en",
|
||||
"localized_id": "doc1",
|
||||
"next": "en-doc2",
|
||||
"next_id": "doc2",
|
||||
"next_title": "Document 2",
|
||||
"permalink": "docs/en/next/doc1.html",
|
||||
"sidebar": "docs",
|
||||
"sort": 1,
|
||||
"source": "doc1.md",
|
||||
"subcategory": "Sub Cat 1",
|
||||
"title": "Document 1",
|
||||
"version": "next",
|
||||
},
|
||||
"type": "LINK",
|
||||
},
|
||||
Object {
|
||||
"item": Object {
|
||||
"category": "Test",
|
||||
"id": "en-doc2",
|
||||
"language": "en",
|
||||
"localized_id": "doc2",
|
||||
"permalink": "docs/en/next/doc2.html",
|
||||
"previous": "en-doc1",
|
||||
"previous_id": "doc1",
|
||||
"previous_title": "Document 1",
|
||||
"sidebar": "docs",
|
||||
"sort": 2,
|
||||
"source": "doc2.md",
|
||||
"subcategory": "Sub Cat 1",
|
||||
"title": "Document 2",
|
||||
"version": "next",
|
||||
},
|
||||
"type": "LINK",
|
||||
},
|
||||
],
|
||||
"title": "Sub Cat 1",
|
||||
"type": "SUBCATEGORY",
|
||||
},
|
||||
Object {
|
||||
"children": Array [
|
||||
Object {
|
||||
"item": Object {
|
||||
"category": "Test",
|
||||
"id": "en-doc3",
|
||||
"language": "en",
|
||||
"localized_id": "doc3",
|
||||
"permalink": "docs/en/next/doc3.html",
|
||||
"previous": "en-doc2",
|
||||
"previous_id": "doc2",
|
||||
"previous_title": "Document 2",
|
||||
"sidebar": "docs",
|
||||
"sort": 3,
|
||||
"source": "doc3.md",
|
||||
"subcategory": "Sub Cat 2",
|
||||
"title": "Document 3",
|
||||
"version": "next",
|
||||
},
|
||||
"type": "LINK",
|
||||
},
|
||||
],
|
||||
"title": "Sub Cat 2",
|
||||
"type": "SUBCATEGORY",
|
||||
},
|
||||
],
|
||||
"title": "Test",
|
||||
"type": "CATEGORY",
|
||||
},
|
||||
Object {
|
||||
"children": Array [
|
||||
Object {
|
||||
"item": Object {
|
||||
"category": "Test 2",
|
||||
"id": "en-doc4",
|
||||
"language": "en",
|
||||
"localized_id": "doc4",
|
||||
"permalink": "docs/en/next/doc4.html",
|
||||
"previous": "en-doc3",
|
||||
"previous_id": "doc3",
|
||||
"previous_title": "Document 3",
|
||||
"sidebar": "docs",
|
||||
"sort": 4,
|
||||
"source": "doc4.md",
|
||||
"title": "Document 4",
|
||||
"version": "next",
|
||||
},
|
||||
"type": "LINK",
|
||||
},
|
||||
],
|
||||
"title": "Test 2",
|
||||
"type": "CATEGORY",
|
||||
},
|
||||
]
|
||||
`;
|
|
@ -0,0 +1,70 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`readMetadata readSidebar should verify sub category data and verify order 1`] = `
|
||||
Object {
|
||||
"doc1": Object {
|
||||
"category": "First Category",
|
||||
"next": "doc2",
|
||||
"order": 1,
|
||||
"previous": null,
|
||||
"sidebar": "docs",
|
||||
"subcategory": null,
|
||||
},
|
||||
"doc2": Object {
|
||||
"category": "First Category",
|
||||
"next": "doc3",
|
||||
"order": 2,
|
||||
"previous": "doc1",
|
||||
"sidebar": "docs",
|
||||
"subcategory": null,
|
||||
},
|
||||
"doc3": Object {
|
||||
"category": "Second Category",
|
||||
"next": "doc4",
|
||||
"order": 3,
|
||||
"previous": "doc2",
|
||||
"sidebar": "docs",
|
||||
"subcategory": null,
|
||||
},
|
||||
"doc4": Object {
|
||||
"category": "Second Category",
|
||||
"next": "doc5",
|
||||
"order": 4,
|
||||
"previous": "doc3",
|
||||
"sidebar": "docs",
|
||||
"subcategory": "First Subcategory",
|
||||
},
|
||||
"doc5": Object {
|
||||
"category": "Second Category",
|
||||
"next": "doc6",
|
||||
"order": 5,
|
||||
"previous": "doc4",
|
||||
"sidebar": "docs",
|
||||
"subcategory": null,
|
||||
},
|
||||
"doc6": Object {
|
||||
"category": "Third Category",
|
||||
"next": "doc7",
|
||||
"order": 6,
|
||||
"previous": "doc5",
|
||||
"sidebar": "docs",
|
||||
"subcategory": "Second Subcategory",
|
||||
},
|
||||
"doc7": Object {
|
||||
"category": "Third Category",
|
||||
"next": "doc8",
|
||||
"order": 7,
|
||||
"previous": "doc6",
|
||||
"sidebar": "docs",
|
||||
"subcategory": "Second Subcategory",
|
||||
},
|
||||
"doc8": Object {
|
||||
"category": "Third Category",
|
||||
"next": null,
|
||||
"order": 8,
|
||||
"previous": "doc7",
|
||||
"sidebar": "docs",
|
||||
"subcategory": "Third Subcategory",
|
||||
},
|
||||
}
|
||||
`;
|
|
@ -41,15 +41,7 @@ describe('readCategories', () => {
|
|||
|
||||
expect(categories.en).toBeDefined();
|
||||
expect(categories.en.length).toBe(2);
|
||||
|
||||
expect(categories.en[0].name).toBe('Test');
|
||||
expect(categories.en[0].links.length).toBe(2);
|
||||
expect(categories.en[0].links[0].id).toBe('en-doc1');
|
||||
expect(categories.en[0].links[1].id).toBe('en-doc2');
|
||||
|
||||
expect(categories.en[1].name).toBe('Test 2');
|
||||
expect(categories.en[1].links.length).toBe(1);
|
||||
expect(categories.en[1].links[0].id).toBe('en-doc3');
|
||||
expect(categories.en).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('should return proper data with categories and sub categories', () => {
|
||||
|
@ -58,24 +50,7 @@ describe('readCategories', () => {
|
|||
expect(categories.en).toBeDefined();
|
||||
expect(categories.ko).toBeDefined();
|
||||
expect(categories.en.length).toBe(2);
|
||||
|
||||
expect(categories.en[0].name).toBe('Test');
|
||||
expect(categories.en[0].links.length).toBe(0);
|
||||
expect(categories.en[0].sub_categories.length).toBe(2);
|
||||
|
||||
expect(categories.en[0].sub_categories[0].name).toBe('Sub Cat 1');
|
||||
expect(categories.en[0].sub_categories[0].links.length).toBe(2);
|
||||
expect(categories.en[0].sub_categories[0].links[0].id).toBe('en-doc1');
|
||||
expect(categories.en[0].sub_categories[0].links[1].id).toBe('en-doc2');
|
||||
|
||||
expect(categories.en[0].sub_categories[1].name).toBe('Sub Cat 2');
|
||||
expect(categories.en[0].sub_categories[1].links.length).toBe(1);
|
||||
expect(categories.en[0].sub_categories[1].links[0].id).toBe('en-doc3');
|
||||
|
||||
expect(categories.en[1].name).toBe('Test 2');
|
||||
expect(categories.en[1].links.length).toBe(1);
|
||||
expect(categories.en[1].links[0].id).toBe('en-doc4');
|
||||
expect(categories.en[1].sub_categories).not.toBeDefined();
|
||||
expect(categories.en).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('should return proper languages when not enabled', () => {
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
*/
|
||||
|
||||
const {readSidebar} = require('../readMetadata');
|
||||
const sidebar = require('./__fixtures__/sidebar');
|
||||
const sidebarSubCategories = require('./__fixtures__/sidebar-subcategories');
|
||||
const sidebarSubcategories = require('./__fixtures__/sidebar-subcategories');
|
||||
|
||||
jest.mock('../env', () => ({
|
||||
translation: {
|
||||
|
@ -36,42 +35,9 @@ jest.mock(`${process.cwd()}/sidebar.json`, () => true, {virtual: true});
|
|||
|
||||
describe('readMetadata', () => {
|
||||
describe('readSidebar', () => {
|
||||
it('should verify regular category data and verify sort', () => {
|
||||
const order = readSidebar(sidebar);
|
||||
|
||||
// Put in this order to verify sort
|
||||
['doc1', 'doc2', 'doc4', 'doc3'].forEach((id, index) => {
|
||||
expect(order[id]).toBeDefined();
|
||||
expect(order[id].sort).toBe(index + 1);
|
||||
});
|
||||
|
||||
expect(order.doc1.previous).toBeUndefined();
|
||||
expect(order.doc2.previous).toBe('doc1');
|
||||
|
||||
expect(order.doc1.next).toBe('doc2');
|
||||
expect(order.doc2.next).toBe('doc4');
|
||||
|
||||
expect(order.doc1.sub_category).toBeFalsy();
|
||||
});
|
||||
|
||||
test('should verify sub category data and verify sort', () => {
|
||||
const order = readSidebar(sidebarSubCategories);
|
||||
|
||||
// Put in this order to verify sort
|
||||
['doc2', 'doc1', 'doc3', 'doc5', 'doc4'].forEach((id, index) => {
|
||||
expect(order[id]).toBeDefined();
|
||||
expect(order[id].sort).toBe(index + 1);
|
||||
});
|
||||
|
||||
expect(order.doc2.sidebar).toBe('docs');
|
||||
expect(order.doc2.category).toBe('First Category');
|
||||
expect(order.doc2.sub_category).toBe('Sub Cat One');
|
||||
|
||||
expect(order.doc1.category).toBe('First Category');
|
||||
expect(order.doc1.sub_category).toBe('Sub Cat One');
|
||||
|
||||
expect(order.doc3.category).toBe('First Category');
|
||||
expect(order.doc3.sub_category).toBe('Sub Cat Two');
|
||||
test('should verify sub category data and verify order', () => {
|
||||
const items = readSidebar(sidebarSubcategories);
|
||||
expect(items).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -88,63 +88,4 @@ describe('server utils', () => {
|
|||
expect(utils.getSubDir(docE, docsDir)).toBeNull();
|
||||
expect(utils.getSubDir(docE, translatedDir)).toEqual('lol/lah');
|
||||
});
|
||||
|
||||
describe('validateSidebar', () => {
|
||||
test('should throw an error for invalid sidebarMetadatas', () => {
|
||||
const metadata = {
|
||||
id: 'doc1',
|
||||
sidebar: 'docs',
|
||||
next_id: 'doc2',
|
||||
next: 'doc2',
|
||||
};
|
||||
|
||||
const sidebarMetadatas = {
|
||||
doc1: {},
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
utils.validateSidebar(metadata, sidebarMetadatas);
|
||||
}).toThrow(
|
||||
`Improper sidebars.json file, document with id 'doc2' not found. Make sure that documents with the ids specified in sidebars.json exist and that no ids are repeated.`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw an error for invalid version sidebarMetadatas', () => {
|
||||
const metadata = {
|
||||
id: 'doc1',
|
||||
version: 'foo',
|
||||
sidebar: 'docs',
|
||||
next_id: 'doc2',
|
||||
next: 'doc2',
|
||||
};
|
||||
|
||||
const sidebarMetadatas = {
|
||||
doc1: {},
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
utils.validateSidebar(metadata, sidebarMetadatas);
|
||||
}).toThrow(
|
||||
`Improper sidebars file for version foo, document with id 'doc2' not found. Make sure that all documents with ids specified in this version's sidebar file exist and that no ids are repeated.`,
|
||||
);
|
||||
});
|
||||
|
||||
test('should pass validate', () => {
|
||||
const metadata = {
|
||||
id: 'doc1',
|
||||
sidebar: 'docs',
|
||||
next_id: 'doc2',
|
||||
next: 'doc2',
|
||||
};
|
||||
|
||||
const sidebarMetadatas = {
|
||||
doc1: {},
|
||||
doc2: {},
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
utils.validateSidebar(metadata, sidebarMetadatas);
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue