Skip to content

Commit

Permalink
Merge branch 'master' into feat-comment-code-highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
elviswolcott authored Mar 25, 2020
2 parents 8d57f55 + 5e0d11d commit 49319b2
Show file tree
Hide file tree
Showing 37 changed files with 630 additions and 244 deletions.
1 change: 1 addition & 0 deletions .watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ module.exports = {
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
setupFiles: ['./jest/stylelint-rule-test.js'],
setupFiles: ['./jest/stylelint-rule-test.js', 'array-flat-polyfill'],
};
30 changes: 25 additions & 5 deletions packages/docusaurus-init/templates/classic/docs/doc1.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,32 @@ This line is also a separate paragraph, but... This line is only separated by a

## Admonitions

:::note This is a note :::
:::note

:::tip This is a tip :::
This is a note

:::important This is important :::
:::

:::caution This is a caution :::
:::tip

:::warning This is a warning :::
This is a tip

:::

:::important

This is important

:::

:::caution

This is a caution

:::

:::warning

This is a warning

:::
30 changes: 25 additions & 5 deletions packages/docusaurus-init/templates/facebook/docs/doc1.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,32 @@ This line is also a separate paragraph, but... This line is only separated by a

## Admonitions

:::note This is a note :::
:::note

:::tip This is a tip :::
This is a note

:::important This is important :::
:::

:::caution This is a caution :::
:::tip

:::warning This is a warning :::
This is a tip

:::

:::important

This is important

:::

:::caution

This is a caution

:::

:::warning

This is a warning

:::
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"loader-utils": "^1.2.3",
"lodash": "^4.17.15"
"lodash.kebabcase": "^4.1.1"
},
"peerDependencies": {
"@docusaurus/core": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import fs from 'fs-extra';
import _ from 'lodash';
import kebabCase from 'lodash.kebabcase';
import path from 'path';
import {normalizeUrl, docuHash, aliasedSitePath} from '@docusaurus/utils';

Expand Down Expand Up @@ -167,7 +167,7 @@ export default function pluginContentBlog(
// eslint-disable-next-line no-param-reassign
blogPost.metadata.tags = tags.map(tag => {
if (typeof tag === 'string') {
const normalizedTag = _.kebabCase(tag);
const normalizedTag = kebabCase(tag);
const permalink = normalizeUrl([tagsPath, normalizedTag]);
if (!blogTags[normalizedTag]) {
blogTags[normalizedTag] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
docs: [
{
'level 1': [
'a',
{
'level 2': [
{
'level 3': [
'c',
{
'level 4': [
'd',
{
'deeper more more': ['e'],
},
],
},
],
},
'f',
],
},
],
},
],
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ describe('loadSidebars', () => {
expect(result).toMatchSnapshot();
});

test('sidebars shortand and longform lead to exact same sidebar', async () => {
const sidebarPath1 = path.join(fixtureDir, 'sidebars-category.js');
const sidebarPath2 = path.join(
fixtureDir,
'sidebars-category-shorthand.js',
);
const sidebar1 = loadSidebars([sidebarPath1]);
const sidebar2 = loadSidebars([sidebarPath2]);
expect(sidebar1).toEqual(sidebar2);
});

test('sidebars with category but category.items is not an array', async () => {
const sidebarPath = path.join(
fixtureDir,
Expand Down Expand Up @@ -93,15 +104,6 @@ describe('loadSidebars', () => {
);
});

test('sidebars with invalid sidebar item', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-invalid-item.json');
expect(() =>
loadSidebars([sidebarPath]),
).toThrowErrorMatchingInlineSnapshot(
`"Unknown sidebar item \\"{\\"a\\":\\"b\\",\\"c\\":\\"d\\"}\\"."`,
);
});

test('sidebars with unknown sidebar item type', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json');
expect(() =>
Expand Down
61 changes: 37 additions & 24 deletions packages/docusaurus-plugin-content-docs/src/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,28 @@ import {
SidebarItemRaw,
SidebarItemLink,
SidebarItemDoc,
SidebarCategoryShorthandRaw,
} from './types';

function isCategoryShorthand(
item: SidebarItemRaw,
): item is SidebarCategoryShorthandRaw {
return typeof item !== 'string' && !item.type;
}

/**
* Convert {category1: [item1,item2]} shorthand syntax to long-form syntax
*/
function normalizeCategoryShorthand(
sidebar: SidebarCategoryShorthandRaw,
): SidebarItemCategoryRaw[] {
return Object.entries(sidebar).map(([label, items]) => ({
type: 'category',
label,
items,
}));
}

/**
* Check that item contains only allowed keys.
*/
Expand Down Expand Up @@ -75,27 +95,29 @@ function assertIsLink(item: any): asserts item is SidebarItemLink {
* Normalizes recursively item and all its children. Ensures that at the end
* each item will be an object with the corresponding type.
*/
function normalizeItem(item: SidebarItemRaw): SidebarItem {
function normalizeItem(item: SidebarItemRaw): SidebarItem[] {
if (typeof item === 'string') {
return {
type: 'doc',
id: item,
};
return [
{
type: 'doc',
id: item,
},
];
}
if (!item.type) {
throw new Error(`Unknown sidebar item "${JSON.stringify(item)}".`);
if (isCategoryShorthand(item)) {
return normalizeCategoryShorthand(item).flatMap(normalizeItem);
}
switch (item.type) {
case 'category':
assertIsCategory(item);
return {...item, items: item.items.map(normalizeItem)};
return [{...item, items: item.items.flatMap(normalizeItem)}];
case 'link':
assertIsLink(item);
return item;
return [item];
case 'ref':
case 'doc':
assertIsDoc(item);
return item;
return [item];
default:
throw new Error(`Unknown sidebar item type: ${item.type}`);
}
Expand All @@ -107,20 +129,11 @@ function normalizeItem(item: SidebarItemRaw): SidebarItem {
function normalizeSidebar(sidebars: SidebarRaw): Sidebar {
return Object.entries(sidebars).reduce(
(acc: Sidebar, [sidebarId, sidebar]) => {
let normalizedSidebar: SidebarItemRaw[];

if (!Array.isArray(sidebar)) {
// Convert sidebar to a more generic structure.
normalizedSidebar = Object.entries(sidebar).map(([label, items]) => ({
type: 'category',
label,
items,
}));
} else {
normalizedSidebar = sidebar;
}

acc[sidebarId] = normalizedSidebar.map(normalizeItem);
const normalizedSidebar: SidebarItemRaw[] = Array.isArray(sidebar)
? sidebar
: normalizeCategoryShorthand(sidebar);

acc[sidebarId] = normalizedSidebar.flatMap(normalizeItem);

return acc;
},
Expand Down
11 changes: 6 additions & 5 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type SidebarItem =

export type SidebarItemRaw =
| string
| SidebarCategoryShorthandRaw
| SidebarItemDoc
| SidebarItemLink
| SidebarItemCategoryRaw
Expand All @@ -63,13 +64,13 @@ export type SidebarItemRaw =
[key: string]: any;
};

export interface SidebarCategoryShorthandRaw {
[sidebarCategory: string]: SidebarItemRaw[];
}

// Sidebar given by user that is not normalized yet. e.g: sidebars.json
export interface SidebarRaw {
[sidebarId: string]:
| {
[sidebarCategory: string]: SidebarItemRaw[];
}
| SidebarItemRaw[];
[sidebarId: string]: SidebarCategoryShorthandRaw | SidebarItemRaw[];
}

export interface Sidebar {
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-preset-classic/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const addAdmonitions = pluginOptions => {

const admonitionsOptions = {
remarkPlugins: (pluginOptions.remarkPlugins || []).concat([
admonitions,
pluginOptions.admonitions || {},
[admonitions, pluginOptions.admonitions || {}],
]),
};

Expand Down
Loading

0 comments on commit 49319b2

Please sign in to comment.