Skip to content

Commit e4a8795

Browse files
committed
Enable plugins to have an async Initialization
1 parent 8f47cee commit e4a8795

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

docu-notion.config.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,32 @@ const dummyBlockModifier: IPlugin = {
1717
],
1818
};
1919

20+
const dummyBlockModifier2: IPlugin = {
21+
name: "dummyBlockModifier2",
22+
23+
notionBlockModifications: [
24+
{
25+
modify: (block: NotionBlock) => {
26+
verbose("dummyBlockModifier2 was called");
27+
},
28+
},
29+
],
30+
};
31+
32+
const dummyBlockModifier2Plugin: IPlugin = {
33+
name: "dummyBlockModifier2Plugin",
34+
35+
init: (p: IPlugin): Promise<void> => {
36+
return new Promise((resolve, reject) => {
37+
verbose("****dummyBlockModifier2Plugin init was called");
38+
p.notionBlockModifications = dummyBlockModifier2.notionBlockModifications;
39+
resolve();
40+
});
41+
},
42+
};
43+
2044
const config: IDocuNotionConfig = {
21-
plugins: [dummyBlockModifier],
45+
plugins: [dummyBlockModifier, dummyBlockModifier2Plugin],
2246
};
2347

2448
export default config;

src/config/configuration.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { BlockObjectResponse } from "@notionhq/client/build/src/api-endpoints";
99
import { DocuNotionOptions } from "../pull";
1010
import { LayoutStrategy } from "../LayoutStrategy";
1111
import defaultConfig from "./default.docunotion.config";
12-
import { error, info, verbose } from "../log";
12+
import { error, info, logDebug, verbose } from "../log";
1313
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";
1414

1515
// wrap this into something with a bit better name than the raw thing
@@ -44,6 +44,9 @@ export type IPlugin = {
4444

4545
// simple regex replacements on the markdown output
4646
regexMarkdownModifications?: IRegexMarkdownModification[];
47+
48+
// allow a plugin to perform an async operation before it can deliver its operations
49+
init?(plugin: IPlugin): Promise<void>;
4750
};
4851

4952
export type IRegexMarkdownModification = {
@@ -97,6 +100,21 @@ export async function loadConfigAsync(): Promise<IDocuNotionConfig> {
97100
verbose(`Did not find configuration file, using defaults.`);
98101
}
99102

103+
const pluginsWithInitializers = found?.config?.plugins?.filter(
104+
(p: IPlugin) => p.init !== undefined
105+
);
106+
const initializers = pluginsWithInitializers?.map(
107+
(p: IPlugin) => () => p!.init!(p)
108+
);
109+
110+
await Promise.all(initializers || []);
111+
112+
found?.config?.plugins?.forEach(async (plugin: IPlugin) => {
113+
if (plugin.init !== undefined) {
114+
verbose(`Initializing plugin ${plugin.name}...`);
115+
await plugin.init(plugin);
116+
}
117+
});
100118
// for now, all we have is plugins
101119
config = {
102120
plugins: defaultConfig.plugins.concat(found?.config?.plugins || []),
@@ -108,12 +126,6 @@ export async function loadConfigAsync(): Promise<IDocuNotionConfig> {
108126
return config;
109127
}
110128

111-
export interface IPlug {
112-
name: string;
113-
sayHello: (block: IFoo) => string;
114-
}
115-
export interface IFoo {}
116-
117129
// export function getMDConversions(): Array<{
118130
// type: string;
119131
// transformer: ICustomNotionToMarkdownConversion;

0 commit comments

Comments
 (0)