mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-07 05:12:31 +02:00
feat(content-pages): front matter validation, include front matter in metadata (#6400)
This commit is contained in:
parent
e5801e49f6
commit
fdf59f30f0
7 changed files with 103 additions and 37 deletions
|
@ -1,2 +1,3 @@
|
||||||
|
# Index
|
||||||
|
|
||||||
Markdown index page
|
Markdown index page
|
|
@ -39,11 +39,20 @@ describe('docusaurus-plugin-content-pages', () => {
|
||||||
type: 'mdx',
|
type: 'mdx',
|
||||||
permalink: '/hello/',
|
permalink: '/hello/',
|
||||||
source: path.posix.join('@site', pluginPath, 'hello', 'index.md'),
|
source: path.posix.join('@site', pluginPath, 'hello', 'index.md'),
|
||||||
|
description: 'Markdown index page',
|
||||||
|
frontMatter: {},
|
||||||
|
title: 'Index',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'mdx',
|
type: 'mdx',
|
||||||
permalink: '/hello/mdxPage',
|
permalink: '/hello/mdxPage',
|
||||||
source: path.posix.join('@site', pluginPath, 'hello', 'mdxPage.mdx'),
|
source: path.posix.join('@site', pluginPath, 'hello', 'mdxPage.mdx'),
|
||||||
|
description: 'my mdx page',
|
||||||
|
title: 'mdx page',
|
||||||
|
frontMatter: {
|
||||||
|
description: 'my mdx page',
|
||||||
|
title: 'mdx page',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'jsx',
|
type: 'jsx',
|
||||||
|
@ -64,6 +73,9 @@ describe('docusaurus-plugin-content-pages', () => {
|
||||||
'hello',
|
'hello',
|
||||||
'translatedMd.md',
|
'translatedMd.md',
|
||||||
),
|
),
|
||||||
|
description: 'translated markdown page',
|
||||||
|
frontMatter: {},
|
||||||
|
title: undefined,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'jsx',
|
type: 'jsx',
|
||||||
|
@ -113,11 +125,20 @@ describe('docusaurus-plugin-content-pages', () => {
|
||||||
type: 'mdx',
|
type: 'mdx',
|
||||||
permalink: '/hello/',
|
permalink: '/hello/',
|
||||||
source: path.posix.join('@site', pluginPath, 'hello', 'index.md'),
|
source: path.posix.join('@site', pluginPath, 'hello', 'index.md'),
|
||||||
|
description: 'Markdown index page',
|
||||||
|
frontMatter: {},
|
||||||
|
title: 'Index',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'mdx',
|
type: 'mdx',
|
||||||
permalink: '/hello/mdxPage',
|
permalink: '/hello/mdxPage',
|
||||||
source: path.posix.join('@site', pluginPath, 'hello', 'mdxPage.mdx'),
|
source: path.posix.join('@site', pluginPath, 'hello', 'mdxPage.mdx'),
|
||||||
|
description: 'my mdx page',
|
||||||
|
title: 'mdx page',
|
||||||
|
frontMatter: {
|
||||||
|
description: 'my mdx page',
|
||||||
|
title: 'mdx page',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'jsx',
|
type: 'jsx',
|
||||||
|
@ -128,6 +149,9 @@ describe('docusaurus-plugin-content-pages', () => {
|
||||||
type: 'mdx',
|
type: 'mdx',
|
||||||
permalink: '/hello/translatedMd',
|
permalink: '/hello/translatedMd',
|
||||||
source: path.posix.join(frTranslationsPath, 'hello', 'translatedMd.md'),
|
source: path.posix.join(frTranslationsPath, 'hello', 'translatedMd.md'),
|
||||||
|
description: 'translated markdown page (fr)',
|
||||||
|
frontMatter: {},
|
||||||
|
title: undefined,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'jsx',
|
type: 'jsx',
|
||||||
|
|
|
@ -19,6 +19,7 @@ import {
|
||||||
createAbsoluteFilePathMatcher,
|
createAbsoluteFilePathMatcher,
|
||||||
normalizeUrl,
|
normalizeUrl,
|
||||||
DEFAULT_PLUGIN_ID,
|
DEFAULT_PLUGIN_ID,
|
||||||
|
parseMarkdownString,
|
||||||
} from '@docusaurus/utils';
|
} from '@docusaurus/utils';
|
||||||
import type {
|
import type {
|
||||||
LoadContext,
|
LoadContext,
|
||||||
|
@ -30,9 +31,10 @@ import type {
|
||||||
import type {Configuration} from 'webpack';
|
import type {Configuration} from 'webpack';
|
||||||
import admonitions from 'remark-admonitions';
|
import admonitions from 'remark-admonitions';
|
||||||
import {PluginOptionSchema} from './pluginOptionSchema';
|
import {PluginOptionSchema} from './pluginOptionSchema';
|
||||||
|
import {validatePageFrontMatter} from './pageFrontMatter';
|
||||||
|
|
||||||
import type {LoadedContent, Metadata, PagesContentPaths} from './types';
|
import type {LoadedContent, PagesContentPaths} from './types';
|
||||||
import type {PluginOptions} from '@docusaurus/plugin-content-pages';
|
import type {PluginOptions, Metadata} from '@docusaurus/plugin-content-pages';
|
||||||
|
|
||||||
export function getContentPathList(contentPaths: PagesContentPaths): string[] {
|
export function getContentPathList(contentPaths: PagesContentPaths): string[] {
|
||||||
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
||||||
|
@ -111,11 +113,20 @@ export default async function pluginContentPages(
|
||||||
encodePath(fileToPath(relativeSource)),
|
encodePath(fileToPath(relativeSource)),
|
||||||
]);
|
]);
|
||||||
if (isMarkdownSource(relativeSource)) {
|
if (isMarkdownSource(relativeSource)) {
|
||||||
// TODO: missing frontmatter validation/normalization here
|
const content = await fs.readFile(source, 'utf-8');
|
||||||
|
const {
|
||||||
|
frontMatter: unsafeFrontMatter,
|
||||||
|
contentTitle,
|
||||||
|
excerpt,
|
||||||
|
} = parseMarkdownString(content);
|
||||||
|
const frontMatter = validatePageFrontMatter(unsafeFrontMatter);
|
||||||
return {
|
return {
|
||||||
type: 'mdx',
|
type: 'mdx',
|
||||||
permalink,
|
permalink,
|
||||||
source: aliasedSourcePath,
|
source: aliasedSourcePath,
|
||||||
|
title: frontMatter.title ?? contentTitle,
|
||||||
|
description: frontMatter.description ?? excerpt,
|
||||||
|
frontMatter,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
Joi,
|
||||||
|
validateFrontMatter,
|
||||||
|
FrontMatterTOCHeadingLevels,
|
||||||
|
} from '@docusaurus/utils-validation';
|
||||||
|
import type {FrontMatter} from '@docusaurus/plugin-content-pages';
|
||||||
|
|
||||||
|
const PageFrontMatterSchema = Joi.object<FrontMatter>({
|
||||||
|
title: Joi.string(),
|
||||||
|
description: Joi.string(),
|
||||||
|
wrapperClassName: Joi.string(),
|
||||||
|
hide_table_of_contents: Joi.string(),
|
||||||
|
...FrontMatterTOCHeadingLevels,
|
||||||
|
});
|
||||||
|
|
||||||
|
export function validatePageFrontMatter(
|
||||||
|
frontMatter: Record<string, unknown>,
|
||||||
|
): FrontMatter {
|
||||||
|
return validateFrontMatter(frontMatter, PageFrontMatterSchema);
|
||||||
|
}
|
|
@ -19,22 +19,45 @@ declare module '@docusaurus/plugin-content-pages' {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Options = Partial<PluginOptions>;
|
export type Options = Partial<PluginOptions>;
|
||||||
}
|
|
||||||
|
|
||||||
declare module '@theme/MDXPage' {
|
export type FrontMatter = {
|
||||||
import type {TOCItem} from '@docusaurus/types';
|
readonly title?: string;
|
||||||
|
readonly description?: string;
|
||||||
export interface Props {
|
|
||||||
readonly content: {
|
|
||||||
readonly frontMatter: {
|
|
||||||
readonly title: string;
|
|
||||||
readonly description: string;
|
|
||||||
readonly wrapperClassName?: string;
|
readonly wrapperClassName?: string;
|
||||||
readonly hide_table_of_contents?: string;
|
readonly hide_table_of_contents?: string;
|
||||||
readonly toc_min_heading_level?: number;
|
readonly toc_min_heading_level?: number;
|
||||||
readonly toc_max_heading_level?: number;
|
readonly toc_max_heading_level?: number;
|
||||||
};
|
};
|
||||||
readonly metadata: {readonly permalink: string};
|
|
||||||
|
export type JSXPageMetadata = {
|
||||||
|
type: 'jsx';
|
||||||
|
permalink: string;
|
||||||
|
source: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type MDXPageMetadata = {
|
||||||
|
type: 'mdx';
|
||||||
|
permalink: string;
|
||||||
|
source: string;
|
||||||
|
frontMatter: FrontMatter & Record<string, unknown>;
|
||||||
|
title?: string;
|
||||||
|
description?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Metadata = JSXPageMetadata | MDXPageMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module '@theme/MDXPage' {
|
||||||
|
import type {TOCItem} from '@docusaurus/types';
|
||||||
|
import type {
|
||||||
|
MDXPageMetadata,
|
||||||
|
FrontMatter,
|
||||||
|
} from '@docusaurus/plugin-content-pages';
|
||||||
|
|
||||||
|
export interface Props {
|
||||||
|
readonly content: {
|
||||||
|
readonly frontMatter: FrontMatter;
|
||||||
|
readonly metadata: MDXPageMetadata;
|
||||||
readonly toc: readonly TOCItem[];
|
readonly toc: readonly TOCItem[];
|
||||||
(): JSX.Element;
|
(): JSX.Element;
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,19 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export type JSXPageMetadata = {
|
import type {Metadata} from '@docusaurus/plugin-content-pages';
|
||||||
type: 'jsx';
|
|
||||||
permalink: string;
|
|
||||||
source: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type MDXPageMetadata = {
|
|
||||||
type: 'mdx';
|
|
||||||
permalink: string;
|
|
||||||
source: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Metadata = JSXPageMetadata | MDXPageMetadata;
|
|
||||||
|
|
||||||
export type LoadedContent = Metadata[];
|
export type LoadedContent = Metadata[];
|
||||||
|
|
||||||
|
|
|
@ -19,18 +19,10 @@ import styles from './styles.module.css';
|
||||||
function MDXPage(props: Props): JSX.Element {
|
function MDXPage(props: Props): JSX.Element {
|
||||||
const {content: MDXPageContent} = props;
|
const {content: MDXPageContent} = props;
|
||||||
const {
|
const {
|
||||||
// TODO this frontmatter is not validated/normalized, it's the raw user-provided one. We should expose normalized one too!
|
metadata: {title, description, permalink, frontMatter},
|
||||||
frontMatter,
|
|
||||||
metadata,
|
|
||||||
} = MDXPageContent;
|
} = MDXPageContent;
|
||||||
|
const {wrapperClassName, hide_table_of_contents: hideTableOfContents} =
|
||||||
const {
|
frontMatter;
|
||||||
title,
|
|
||||||
description,
|
|
||||||
wrapperClassName,
|
|
||||||
hide_table_of_contents: hideTableOfContents,
|
|
||||||
} = frontMatter;
|
|
||||||
const {permalink} = metadata;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout
|
<Layout
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue