Skip to content

Commit

Permalink
Merge branch 'master' of github.com:facebook/docusaurus into lex111/r…
Browse files Browse the repository at this point in the history
…eplace-classnames-with-clsx
  • Loading branch information
lex111 committed Jun 6, 2020
2 parents 93c6fcd + 1003a15 commit 4011037
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 58 deletions.
5 changes: 2 additions & 3 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import readingTime from 'reading-time';
import {Feed} from 'feed';
import {PluginOptions, BlogPost, DateLink} from './types';
import {
parse,
parseMarkdownFile,
normalizeUrl,
aliasedSitePath,
getEditUrl,
Expand Down Expand Up @@ -120,8 +120,7 @@ export async function generateBlogPosts(

const editBlogUrl = getEditUrl(relativePath, editUrl);

const fileString = await fs.readFile(source, 'utf-8');
const {frontMatter, content, excerpt} = parse(fileString);
const {frontMatter, content, excerpt} = await parseMarkdownFile(source);

if (frontMatter.draft && process.env.NODE_ENV === 'production') {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ Array [
]
`;

exports[`site with wrong sidebar file 1`] = `
[Error: Bad sidebars file. The document id 'goku' was used in the sidebar, but no document with this id could be found.
Available document ids=
- foo/bar
- foo/baz
- hello
- ipsum
- lorem]
`;

exports[`versioned website content 1`] = `
Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ test('site with wrong sidebar file', async () => {
const plugin = pluginContentDocs(context, {
sidebarPath,
});
return plugin
.loadContent()
.catch((e) =>
expect(e).toMatchInlineSnapshot(
`[Error: Improper sidebars file, document with id 'goku' not found.]`,
),
);
return plugin.loadContent().catch((e) => expect(e).toMatchSnapshot());
});

describe('empty/no docs website', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('loadSidebars', () => {
expect(() =>
loadSidebars([sidebarPath]),
).toThrowErrorMatchingInlineSnapshot(
`"Unknown sidebar item type: superman"`,
`"Unknown sidebar item type [superman]. Sidebar item={\\"type\\":\\"superman\\"} "`,
);
});

Expand Down
12 changes: 7 additions & 5 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,18 @@ export default function pluginContentDocs(
});

const convertDocLink = (item: SidebarItemDoc): SidebarItemLink => {
const linkID = item.id;
const linkMetadata = docsMetadataRaw[linkID];
const docId = item.id;
const docMetadata = docsMetadataRaw[docId];

if (!linkMetadata) {
if (!docMetadata) {
throw new Error(
`Improper sidebars file, document with id '${linkID}' not found.`,
`Bad sidebars file. The document id '${docId}' was used in the sidebar, but no document with this id could be found.
Available document ids=
- ${Object.keys(docsMetadataRaw).sort().join('\n- ')}`,
);
}

const {title, permalink, sidebar_label} = linkMetadata;
const {title, permalink, sidebar_label} = docMetadata;

return {
type: 'link',
Expand Down
7 changes: 3 additions & 4 deletions packages/docusaurus-plugin-content-docs/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import fs from 'fs-extra';
import path from 'path';
import {
parse,
parseMarkdownFile,
aliasedSitePath,
normalizeUrl,
getEditUrl,
Expand Down Expand Up @@ -65,7 +64,7 @@ export default async function processMetadata({
const {versioning} = env;
const filePath = path.join(refDir, source);

const fileStringPromise = fs.readFile(filePath, 'utf-8');
const fileMarkdownPromise = parseMarkdownFile(filePath);
const lastUpdatedPromise = lastUpdated(filePath, options);

let version;
Expand All @@ -92,7 +91,7 @@ export default async function processMetadata({

const docsEditUrl = getEditUrl(relativePath, editUrl);

const {frontMatter = {}, excerpt} = parse(await fileStringPromise);
const {frontMatter = {}, excerpt} = await fileMarkdownPromise;
const {sidebar_label, custom_edit_url} = frontMatter;

// Default base id is the file name.
Expand Down
10 changes: 9 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ function normalizeItem(item: SidebarItemRaw): SidebarItem[] {
assertIsDoc(item);
return [item];
default:
throw new Error(`Unknown sidebar item type: ${item.type}`);
const extraMigrationError =
item.type === 'subcategory'
? "Docusaurus v2: 'subcategory' has been renamed as 'category'"
: '';
throw new Error(
`Unknown sidebar item type [${
item.type
}]. Sidebar item=${JSON.stringify(item)} ${extraMigrationError}`,
);
}
}

Expand Down
59 changes: 40 additions & 19 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import useLogo from '@theme/hooks/useLogo';

import styles from './styles.module.css';

// retrocompatible with v1
const DefaultNavItemPosition = 'right';

function NavLink({
activeBasePath,
activeBaseRegex,
Expand Down Expand Up @@ -61,8 +64,13 @@ function NavLink({
);
}

function NavItem({items, position, className, ...props}) {
const navLinkclsx = (extraClassName, isDropdownItem = false) =>
function NavItem({
items,
position = DefaultNavItemPosition,
className,
...props
}) {
const navLinkClassNames = (extraClassName, isDropdownItem = false) =>
clsx(
{
'navbar__item navbar__link': !isDropdownItem,
Expand All @@ -72,7 +80,7 @@ function NavItem({items, position, className, ...props}) {
);

if (!items) {
return <NavLink className={navLinkclsx(className)} {...props} />;
return <NavLink className={navLinkClassNames(className)} {...props} />;
}

return (
Expand All @@ -82,7 +90,7 @@ function NavItem({items, position, className, ...props}) {
'dropdown--right': position === 'right',
})}>
<NavLink
className={navLinkclsx(className)}
className={navLinkClassNames(className)}
{...props}
onClick={(e) => e.preventDefault()}
onKeyDown={(e) => {
Expand All @@ -97,7 +105,7 @@ function NavItem({items, position, className, ...props}) {
<li key={i}>
<NavLink
activeClassName="dropdown__link--active"
className={navLinkclsx(childItemClassName, true)}
className={navLinkClassNames(childItemClassName, true)}
{...childItemProps}
/>
</li>
Expand All @@ -109,7 +117,7 @@ function NavItem({items, position, className, ...props}) {

function MobileNavItem({items, position, className, ...props}) {
// Need to destructure position from props so that it doesn't get passed on.
const navLinkclsx = (extraClassName, isSubList = false) =>
const navLinkClassNames = (extraClassName, isSubList = false) =>
clsx(
'menu__link',
{
Expand All @@ -121,22 +129,22 @@ function MobileNavItem({items, position, className, ...props}) {
if (!items) {
return (
<li className="menu__list-item">
<NavLink className={navLinkclsx(className)} {...props} />
<NavLink className={navLinkClassNames(className)} {...props} />
</li>
);
}

return (
<li className="menu__list-item">
<NavLink className={navLinkclsx(className, true)} {...props}>
<NavLink className={navLinkClassNames(className, true)} {...props}>
{props.label}
</NavLink>
<ul className="menu__list">
{items.map(({className: childItemClassName, ...childItemProps}, i) => (
<li className="menu__list-item" key={i}>
<NavLink
activeClassName="menu__link--active"
className={navLinkclsx(childItemClassName)}
className={navLinkClassNames(childItemClassName)}
{...childItemProps}
onClick={props.onClick}
/>
Expand All @@ -147,6 +155,21 @@ function MobileNavItem({items, position, className, ...props}) {
);
}

// If split links by left/right
// if position is unspecified, fallback to right (as v1)
function splitLinks(links) {
const leftLinks = links.filter(
(linkItem) => (linkItem.position ?? DefaultNavItemPosition) === 'left',
);
const rightLinks = links.filter(
(linkItem) => (linkItem.position ?? DefaultNavItemPosition) === 'right',
);
return {
leftLinks,
rightLinks,
};
}

function Navbar() {
const {
siteConfig: {
Expand Down Expand Up @@ -178,6 +201,8 @@ function Navbar() {
[setLightTheme, setDarkTheme],
);

const {leftLinks, rightLinks} = splitLinks(links);

return (
<nav
ref={navbarRef}
Expand Down Expand Up @@ -232,18 +257,14 @@ function Navbar() {
</strong>
)}
</Link>
{links
.filter((linkItem) => linkItem.position === 'left')
.map((linkItem, i) => (
<NavItem {...linkItem} key={i} />
))}
{leftLinks.map((linkItem, i) => (
<NavItem {...linkItem} key={i} />
))}
</div>
<div className="navbar__items navbar__items--right">
{links
.filter((linkItem) => linkItem.position === 'right')
.map((linkItem, i) => (
<NavItem {...linkItem} key={i} />
))}
{rightLinks.map((linkItem, i) => (
<NavItem {...linkItem} key={i} />
))}
{!disableDarkMode && (
<Toggle
className={styles.displayOnlyInLargeViewport}
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-theme-classic/src/theme/Tabs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function Tabs(props) {
const relevantTabGroupChoice = tabGroupChoices[groupId];
if (
relevantTabGroupChoice != null &&
relevantTabGroupChoice !== selectedValue
relevantTabGroupChoice !== selectedValue &&
values.some((value) => value.value === relevantTabGroupChoice)
) {
setSelectedValue(relevantTabGroupChoice);
}
Expand Down
34 changes: 28 additions & 6 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,14 @@ export function createExcerpt(fileString: string): string | undefined {
return undefined;
}

export function parse(
fileString: string,
): {
type ParsedMarkdown = {
frontMatter: {
[key: string]: any;
};
content: string;
excerpt: string | undefined;
} {
};
export function parseMarkdownString(markdownString: string): ParsedMarkdown {
const options: {} = {
excerpt: (file: matter.GrayMatterFile<string>): void => {
// Hacky way of stripping out import statements from the excerpt
Expand All @@ -246,8 +245,31 @@ export function parse(
},
};

const {data: frontMatter, content, excerpt} = matter(fileString, options);
return {frontMatter, content, excerpt};
try {
const {data: frontMatter, content, excerpt} = matter(
markdownString,
options,
);
return {frontMatter, content, excerpt};
} catch (e) {
throw new Error(`Error while parsing markdown front matter.
This can happen if you use special characteres like : in frontmatter values (try using "" around that value)
${e.message}`);
}
}

export async function parseMarkdownFile(
source: string,
): Promise<ParsedMarkdown> {
const markdownString = await fs.readFile(source, 'utf-8');
try {
return parseMarkdownString(markdownString);
} catch (e) {
throw new Error(
`Error while parsing markdown file ${source}
${e.message}`,
);
}
}

export function normalizeUrl(rawUrls: string[]): string {
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export function loadConfig(siteDir: string): DocusaurusConfig {
throw new Error(
`The field(s) ${formatFields(
unrecognizedFields,
)} are not recognized in ${CONFIG_FILE_NAME}`,
)} are not recognized in ${CONFIG_FILE_NAME}.
If you still want these fields to be in your configuration, put them in the 'customFields' attribute.
See https://v2.docusaurus.io/docs/docusaurus.config.js/#customfields`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion website/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ npm install
To check that that the update occurred successfully, run:

```bash npm2yarn
npm docusaurus --version
npx docusaurus --version
```

You should see the correct version as output.
Expand Down
30 changes: 30 additions & 0 deletions website/docs/markdown-features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,36 @@ You may want choices of the same kind of tabs to sync with each other. For examp
<TabItem value="mac">Use Command + V to paste.</TabItem>
</Tabs>

For all tab groups that have the same `groupId`, the possible values do not need to be the same. If one tab group with chooses an value that does not exist in another tab group with the same `groupId`, the tab group with the missing value won't change its tab. You can see that from the following example. Try to select Linux, and the above tab groups doesn't change.

```jsx
<Tabs
groupId="operating-systems"
defaultValue="win"
values={[
{label: 'Windows', value: 'win'},
{label: 'macOS', value: 'mac'},
{label: 'Linux', value: 'linux'},
]}>
<TabItem value="win">I am Windows.</TabItem>
<TabItem value="mac">I am macOS.</TabItem>
<TabItem value="linux">I am Linux.</TabItem>
</Tabs>
```

<Tabs
groupId="operating-systems"
defaultValue="win"
values={[
{label: 'Windows', value: 'win'},
{label: 'macOS', value: 'mac'},
{label: 'Linux', value: 'linux'},
]}>
<TabItem value="win">I am Windows.</TabItem>
<TabItem value="mac">I am macOS.</TabItem>
<TabItem value="linux">I am Linux.</TabItem>
</Tabs>

---

Tab choices with different `groupId`s will not interfere with each other:
Expand Down
Loading

0 comments on commit 4011037

Please sign in to comment.