丰富Admonition类型
自定义Admonitions的官方文档:https://docusaurus.io/docs/markdown-features/admonitions#customizing-admonitions
本文中增加了success、bug、abstract、question、example 5种类型的Admonition。效果如下
success
Some content with Markdown syntax
. Check this api
.
bug
Some content with Markdown syntax
. Check this api
.
abstract
Some content with Markdown syntax
. Check this api
.
question
Some content with Markdown syntax
. Check this api
.
example
Some content with Markdown syntax
. Check this api
.
实现
我们借助Docusaurus已经存在的组件AdmonitionLayout
来实现。
import AdmonitionLayout from '@theme/Admonition/Layout';
实现方式参考了 node_modules/@docusaurus/theme-classic/src/theme/Admonition/Type/Info.tsx
,这是官方的实现。
配置
首先我们需要在docusaurus.config.ts
中增加Admonition类型。
docusaurus.config.js
export default {
// ...
presets: [
[
'classic',
{
// ...
docs: {
admonitions: {
keywords: ['success', 'abstract', 'question', 'bug', 'example'],
extendDefaults: true,
},
},
},
],
],
};
代码实现
首先在 src/theme
文件夹下创建一个 theme.d.ts
文件,用于声明Props
模块。
declare module '@theme/Admonition/Type/Success' {
import type {Props as AdmonitionProps} from '@theme/Admonition';
export interface Props extends AdmonitionProps {}
export default function AdmonitionTypSuccess(props: Props): JSX.Element;
}
declare module '@theme/Admonition/Type/Abstract' {
import type {Props as AdmonitionProps} from '@theme/Admonition';
export interface Props extends AdmonitionProps {}
export default function AdmonitionTypAbstract(props: Props): JSX.Element;
}
declare module '@theme/Admonition/Type/Question' {
import type {Props as AdmonitionProps} from '@theme/Admonition';
export interface Props extends AdmonitionProps {}
export default function AdmonitionTypQuestion(props: Props): JSX.Element;
}
declare module '@theme/Admonition/Type/Bug' {
import type {Props as AdmonitionProps} from '@theme/Admonition';
export interface Props extends AdmonitionProps {}
export default function AdmonitionTypBug(props: Props): JSX.Element;
}
declare module '@theme/Admonition/Type/Example' {
import type {Props as AdmonitionProps} from '@theme/Admonition';
export interface Props extends AdmonitionProps {}
export default function AdmonitionTypExample(props: Props): JSX.Element;
}
declare module '@theme/Admonition/Type/Memo' {
import type {Props as AdmonitionProps} from '@theme/Admonition';
export interface Props extends AdmonitionProps {}
export default function AdmonitionTypMemo(props: Props): JSX.Element;
}
declare module '@theme/Admonition/Type/Translate' {
import type {Props as AdmonitionProps} from '@theme/Admonition';
export interface Props extends AdmonitionProps {}
export default function AdmonitionTypeTranslate(props: Props): JSX.Element;
}
组件代码实现
- Success
- Abstract
- Question
- Bug
- Example
/src/theme/Admonition/Type/Success.tsx
/// <reference path="../../theme.d.ts" />
import React from 'react';
import clsx from 'clsx';
import Translate from '@docusaurus/Translate';
import type {Props} from '@theme/Admonition/Type/Success';
import AdmonitionLayout from '@theme/Admonition/Layout';
import SuccessIcon from '@site/static/img/admonition/success.svg';
const infimaClassName = 'alert alert--success';
const defaultProps = {
icon: <SuccessIcon />,
title: (
<Translate
id="theme.admonition.success"
description="The default label used for the Success admonition (:::success)">
success
</Translate>
),
};
export default function AdmonitionTypeSuccess(props: Props): JSX.Element {
return (
<AdmonitionLayout
{...defaultProps}
{...props}
className={clsx(infimaClassName, props.className)}>
{props.children}
</AdmonitionLayout>
);
}
/src/theme/Admonition/Type/Abstract.tsx
/// <reference path="../../theme.d.ts" />
import React from 'react';
import clsx from 'clsx';
import Translate from '@docusaurus/Translate';
import type {Props} from '@theme/Admonition/Type/Success';
import AdmonitionLayout from '@theme/Admonition/Layout';
import AbstractIcon from '@site/static/img/admonition/abstract.svg';
const infimaClassName = 'alert alert--info';
const defaultProps = {
icon: <AbstractIcon />,
title: (
<Translate
id="theme.admonition.success"
description="The default label used for the Abstrac admonition (:::abstract)">
abstract
</Translate>
),
};
export default function AdmonitionTypeAbstract(props: Props): JSX.Element {
return (
<AdmonitionLayout
{...defaultProps}
{...props}
className={clsx(infimaClassName, props.className)}>
{props.children}
</AdmonitionLayout>
);
}
/src/theme/Admonition/Type/Question.tsx
/// <reference path="../../theme.d.ts" />
import React from 'react';
import clsx from 'clsx';
import Translate from '@docusaurus/Translate';
import type {Props} from '@theme/Admonition/Type/Success';
import AdmonitionLayout from '@theme/Admonition/Layout';
import QuestionIcon from '@site/static/img/admonition/question.svg';
const infimaClassName = 'alert alert--info';
const defaultProps = {
icon: <QuestionIcon />,
title: (
<Translate
id="theme.admonition.success"
description="The default label used for the Question admonition (:::question)">
question
</Translate>
),
};
export default function AdmonitionTypeQuestion(props: Props): JSX.Element {
return (
<AdmonitionLayout
{...defaultProps}
{...props}
className={clsx(infimaClassName, props.className)}>
{props.children}
</AdmonitionLayout>
);
}
/src/theme/Admonition/Type/Bug.tsx
/// <reference path="../../theme.d.ts" />
import React from 'react';
import clsx from 'clsx';
import Translate from '@docusaurus/Translate';
import type {Props} from '@theme/Admonition/Type/Success';
import AdmonitionLayout from '@theme/Admonition/Layout';
import BugIcon from '@site/static/img/admonition/bug.svg';
const infimaClassName = 'alert alert--danger';
const defaultProps = {
icon: <BugIcon />,
title: (
<Translate
id="theme.admonition.bug"
description="The default label used for the Bug admonition (:::bug)">
bug
</Translate>
),
};
export default function AdmonitionTypeBug(props: Props): JSX.Element {
return (
<AdmonitionLayout
{...defaultProps}
{...props}
className={clsx(infimaClassName, props.className)}>
{props.children}
</AdmonitionLayout>
);
}
/src/theme/Admonition/Type/Example.tsx
/// <reference path="../../theme.d.ts" />
import React from 'react';
import clsx from 'clsx';
import Translate from '@docusaurus/Translate';
import type {Props} from '@theme/Admonition/Type/Success';
import AdmonitionLayout from '@theme/Admonition/Layout';
import ExampleIcon from '@site/static/img/admonition/example.svg';
const infimaClassName = 'alert alert--primary';
const defaultProps = {
icon: <ExampleIcon />,
title: (
<Translate
id="theme.admonition.bug"
description="The default label used for the Example admonition (:::example)">
example
</Translate>
),
};
export default function AdmonitionTypeExample(props: Props): JSX.Element {
return (
<AdmonitionLayout
{...defaultProps}
{...props}
className={clsx(infimaClassName, props.className)}>
{props.children}
</AdmonitionLayout>
);
}
配置.d.ts文件
还需要在tsconfig.json
中加入theme.d.ts
。
tsconfig.json
{
// This file is not used in compilation. It is here just for a nice editor experience.
"extends": "@docusaurus/tsconfig",
"include": ["src/theme/theme.d.ts"],
"compilerOptions": {
"baseUrl": "."
}
}
或者在文档中使用三斜线指令引入.d.ts
。