丰富Admonition类型
自定义Admonitions的官方文档:https://docusaurus.io/docs/markdown-features/admonitions#customizing-admonitions
本文中增加了success、bug、abstract、question、example 5种类型的Admonition。效果如下
Some content with Markdown syntax
. Check this api
.
Some content with Markdown syntax
. Check this api
.
Some content with Markdown syntax
. Check this api
.
Some content with Markdown syntax
. Check this api
.
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类型。
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
/// <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>
);
}
/// <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>
);
}
/// <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>
);
}
/// <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>
);
}
/// <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
。
{
// 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
。
两种方式任选一种即可,本文中两种方式都提供了。
如何修改默认样式
根据上面的方法生成的组件会包含一个theme-admonition-<type>
的样式,如success类型的组件会包含theme-admonition-success
。要想自定义其样式,只需要在src/css/customer.css
中定义其样式即可。
本示例中的bug组件的样式如下:
.theme-admonition-bug {
--ifm-alert-background-color: #ffebec;
--ifm-alert-background-color-highlight: rgba(250, 56, 62, .15);
--ifm-alert-foreground-color: var(--ifm-color-danger-contrast-foreground);
--ifm-alert-border-color: #e13238;
}
同样的方法也可以修改官方的默认样式。
使用
与官方的Admonition使用方式一样
:::success
Some **content** with _Markdown_ `syntax`. Check [this `api`](#).
:::
Some content with Markdown syntax
. Check this api
.
:::success[执行成功]
Some **content** with _Markdown_ `syntax`. Check [this `api`](#).
:::
Some content with Markdown syntax
. Check this api
.