Skip to content

Commit a101982

Browse files
committed
Add example of using closure with plugins
1 parent ec19c27 commit a101982

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

docu-notion.config.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55

66
import { IPlugin, IDocuNotionConfig, Log, NotionBlock } from "./dist";
77

8-
const dummyBlockModifier: IPlugin = {
9-
name: "dummyBlockModifier",
8+
// This is an example of a plugin that needs customization by the end user.
9+
// It uses a closure to supply the plugin with the customization parameter.
10+
function dummyBlockModifier(customParameter: string): IPlugin {
11+
return {
12+
name: "dummyBlockModifier",
1013

11-
notionBlockModifications: [
12-
{
13-
modify: (block: NotionBlock) => {
14-
Log.verbose("dummyBlockModifier was called");
14+
notionBlockModifications: [
15+
{
16+
modify: (block: NotionBlock) => {
17+
Log.verbose(
18+
`dummyBlockModifier has customParameter:${customParameter}.`
19+
);
20+
},
1521
},
16-
},
17-
],
18-
};
22+
],
23+
};
24+
}
1925

2026
const dummyMarkdownModifier: IPlugin = {
2127
name: "dummyMarkdownModifier",
@@ -29,7 +35,12 @@ const dummyMarkdownModifier: IPlugin = {
2935
};
3036

3137
const config: IDocuNotionConfig = {
32-
plugins: [dummyBlockModifier, dummyMarkdownModifier],
38+
plugins: [
39+
// here we're adding a plugin that needs a parameter for customization
40+
dummyBlockModifier("foobar"),
41+
// here's we're adding a plugin that doesn't take any customization
42+
dummyMarkdownModifier,
43+
],
3344
};
3445

3546
export default config;

src/plugins/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ const config: IDocuNotionConfig = {
2323
plugins: [dummyMarkdownModifier],
2424
};
2525
export default config;
26-
2726
```
2827

2928
For other available plugin points, see [pluginTypes.ts](pluginTypes.ts). All of the built-in processing is also done via built-in plugins, so those files and their unit tests should serve as good examples.
3029

30+
If your plugin needs custom parameters, just supply the user with a function that will return the `IPlugin` in a closure containing the parameters. See [docu-notion-config.ts](docu-notion-config.ts) for an example of this.
31+
3132
Once you have your plugin working, you have three options:
3233

3334
- just keep it as part of your Docusaurus project.

0 commit comments

Comments
 (0)