Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): markdown pages #3158

Merged
merged 14 commits into from
Jul 31, 2020
Merged
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages/docusaurus-1.x/lib/core/metadata.js
packages/docusaurus-1.x/lib/core/MetadataBlog.js
packages/docusaurus-1.x/lib/core/__tests__/split-tab.test.js
packages/docusaurus-utils/lib/
packages/docusaurus-utils-validation/lib/
packages/docusaurus/lib/
packages/docusaurus-init/lib/
packages/docusaurus-plugin-client-redirects/lib/
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ coverage
types
test-website
packages/docusaurus-utils/lib/
packages/docusaurus-utils-validation/lib/
packages/docusaurus/lib/
packages/docusaurus-init/lib/
packages/docusaurus-plugin-client-redirects/lib/
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build
coverage
.docusaurus
packages/docusaurus-utils/lib/
packages/docusaurus-utils-validation/lib/
packages/docusaurus/lib/
packages/docusaurus-init/lib/
packages/docusaurus-plugin-client-redirects/lib/
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ignorePatterns = [
'__fixtures__',
'/packages/docusaurus/lib',
'/packages/docusaurus-utils/lib',
'/packages/docusaurus-utils-validation/lib',
'/packages/docusaurus-plugin-content-blog/lib',
'/packages/docusaurus-plugin-content-docs/lib',
'/packages/docusaurus-plugin-content-pages/lib',
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@docusaurus/mdx-loader": "2.0.0-alpha.60",
"@docusaurus/types": "2.0.0-alpha.60",
"@docusaurus/utils": "2.0.0-alpha.60",
"@docusaurus/utils-validation": "2.0.0-alpha.60",
"@hapi/joi": "^17.1.1",
"feed": "^4.1.0",
"fs-extra": "^8.1.0",
Expand Down
25 changes: 8 additions & 17 deletions packages/docusaurus-plugin-content-blog/src/pluginOptionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/

import * as Joi from '@hapi/joi';
import {
RemarkPluginsSchema,
RehypePluginsSchema,
AdmonitionsSchema,
} from '@docusaurus/utils-validation';

export const DEFAULT_OPTIONS = {
feedOptions: {},
Expand Down Expand Up @@ -47,25 +52,11 @@ export const PluginOptionSchema = Joi.object({
.allow('')
.default(DEFAULT_OPTIONS.blogDescription),
showReadingTime: Joi.bool().default(DEFAULT_OPTIONS.showReadingTime),
remarkPlugins: Joi.array()
.items(
Joi.array()
.items(Joi.function().required(), Joi.object().required())
.length(2),
Joi.function(),
)
.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: Joi.array()
.items(
Joi.array()
.items(Joi.function().required(), Joi.object().required())
.length(2),
Joi.function(),
)
.default(DEFAULT_OPTIONS.rehypePlugins),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins),
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
editUrl: Joi.string().uri(),
truncateMarker: Joi.object().default(DEFAULT_OPTIONS.truncateMarker),
admonitions: Joi.object().default(DEFAULT_OPTIONS.admonitions),
beforeDefaultRemarkPlugins: Joi.array()
.items(
Joi.array()
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@docusaurus/mdx-loader": "2.0.0-alpha.60",
"@docusaurus/types": "2.0.0-alpha.60",
"@docusaurus/utils": "2.0.0-alpha.60",
"@docusaurus/utils-validation": "2.0.0-alpha.60",
"@hapi/joi": "17.1.1",
"execa": "^3.4.0",
"fs-extra": "^8.1.0",
Expand Down
25 changes: 8 additions & 17 deletions packages/docusaurus-plugin-content-docs/src/pluginOptionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/
import * as Joi from '@hapi/joi';
import {PluginOptions} from './types';
import {
RemarkPluginsSchema,
RehypePluginsSchema,
AdmonitionsSchema,
} from '@docusaurus/utils-validation';

const REVERSED_DOCS_HOME_PAGE_ID = '_index';

Expand Down Expand Up @@ -35,27 +40,13 @@ export const PluginOptionSchema = Joi.object({
sidebarPath: Joi.string().default(DEFAULT_OPTIONS.sidebarPath),
docLayoutComponent: Joi.string().default(DEFAULT_OPTIONS.docLayoutComponent),
docItemComponent: Joi.string().default(DEFAULT_OPTIONS.docItemComponent),
remarkPlugins: Joi.array()
.items(
Joi.array()
.items(Joi.function().required(), Joi.object().required())
.length(2),
Joi.function(),
)
.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: Joi.array()
.items(
Joi.array()
.items(Joi.function().required(), Joi.object().required())
.length(2),
Joi.function(),
)
.default(DEFAULT_OPTIONS.rehypePlugins),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
rehypePlugins: RehypePluginsSchema.default(DEFAULT_OPTIONS.rehypePlugins),
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
showLastUpdateTime: Joi.bool().default(DEFAULT_OPTIONS.showLastUpdateTime),
showLastUpdateAuthor: Joi.bool().default(
DEFAULT_OPTIONS.showLastUpdateAuthor,
),
admonitions: Joi.object().default(DEFAULT_OPTIONS.admonitions),
excludeNextVersionDocs: Joi.bool().default(
DEFAULT_OPTIONS.excludeNextVersionDocs,
),
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-plugin-content-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
"@types/hapi__joi": "^17.1.2"
},
"dependencies": {
"@docusaurus/mdx-loader": "2.0.0-alpha.60",
"@docusaurus/types": "2.0.0-alpha.60",
"@docusaurus/utils": "2.0.0-alpha.60",
"@docusaurus/utils-validation": "2.0.0-alpha.60",
"@hapi/joi": "17.1.1",
"globby": "^10.0.1"
"loader-utils": "^1.2.3",
"globby": "^10.0.1",
"remark-admonitions": "^1.2.1"
},
"peerDependencies": {
"@docusaurus/core": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 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 = {
title: 'My Site',
tagline: 'The tagline of my site',
url: 'https://your-docusaurus-test-site.com',
baseUrl: '/',
favicon: 'img/favicon.ico',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Markdown index page
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: mdx page
description: my mdx page
---
MDX page
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@
*/

import path from 'path';
import {loadContext} from '@docusaurus/core/lib/server';

import pluginContentPages from '../index';
import {LoadContext} from '@docusaurus/types';
import normalizePluginOptions from './pluginOptionSchema.test';

describe('docusaurus-plugin-content-pages', () => {
test('simple pages', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const siteConfig = {
title: 'Hello',
baseUrl: '/',
url: 'https://docusaurus.io',
};
const context = {
siteDir,
siteConfig,
} as LoadContext;
const context = loadContext(siteDir);
const pluginPath = 'src/pages';
const plugin = pluginContentPages(
context,
Expand All @@ -34,14 +26,27 @@ describe('docusaurus-plugin-content-pages', () => {

expect(pagesMetadatas).toEqual([
{
type: 'jsx',
permalink: '/',
source: path.join('@site', pluginPath, 'index.js'),
},
{
type: 'jsx',
permalink: '/typescript',
source: path.join('@site', pluginPath, 'typescript.tsx'),
},
{
type: 'mdx',
permalink: '/hello/',
source: path.join('@site', pluginPath, 'hello', 'index.md'),
},
{
type: 'mdx',
permalink: '/hello/mdxPage',
source: path.join('@site', pluginPath, 'hello', 'mdxPage.mdx'),
},
{
type: 'jsx',
permalink: '/hello/world',
source: path.join('@site', pluginPath, 'hello', 'world.js'),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
*/

import {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';
import {PluginOptions} from '../types';

export default function normalizePluginOptions(options) {
export default function normalizePluginOptions(
options: Partial<PluginOptions>,
) {
const {value, error} = PluginOptionSchema.validate(options, {
convert: false,
});
Expand All @@ -19,29 +22,30 @@ export default function normalizePluginOptions(options) {
}

describe('normalizePagesPluginOptions', () => {
test('should return default options for undefined user options', async () => {
const {value} = await PluginOptionSchema.validate({});
test('should return default options for undefined user options', () => {
const value = normalizePluginOptions({});
expect(value).toEqual(DEFAULT_OPTIONS);
});

test('should fill in default options for partially defined user options', async () => {
const {value} = await PluginOptionSchema.validate({path: 'src/pages'});
test('should fill in default options for partially defined user options', () => {
const value = normalizePluginOptions({path: 'src/pages'});
expect(value).toEqual(DEFAULT_OPTIONS);
});

test('should accept correctly defined user options', async () => {
test('should accept correctly defined user options', () => {
const userOptions = {
path: 'src/my-pages',
routeBasePath: 'my-pages',
include: ['**/*.{js,jsx,ts,tsx}'],
};
const {value} = await PluginOptionSchema.validate(userOptions);
expect(value).toEqual(userOptions);
const value = normalizePluginOptions(userOptions);
expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
});

test('should reject bad path inputs', () => {
expect(() => {
normalizePluginOptions({
// @ts-expect-error: bad attribute
path: 42,
});
}).toThrowErrorMatchingInlineSnapshot(`"\\"path\\" must be a string"`);
Expand Down
Loading