|
1 | | -// an extension manager that uses cosmicconfig to get a list of plugin functions |
2 | | -// and then runs them in sequence |
3 | | - |
4 | | -import { ListBlockChildrenResponseResult } from "notion-to-md/build/types"; |
5 | 1 | import * as Cosmic from "cosmiconfig"; |
6 | | -import { NotionPage } from "../NotionPage"; |
7 | | -import { NotionToMarkdown } from "notion-to-md"; |
8 | | -import { BlockObjectResponse } from "@notionhq/client/build/src/api-endpoints"; |
9 | | -import { DocuNotionOptions } from "../pull"; |
10 | | -import { LayoutStrategy } from "../LayoutStrategy"; |
11 | 2 | import defaultConfig from "./default.docunotion.config"; |
12 | | -import { error, info, logDebug, verbose } from "../log"; |
| 3 | +import { error, verbose } from "../log"; |
13 | 4 | import { TypeScriptLoader } from "cosmiconfig-typescript-loader"; |
14 | | - |
15 | | -// wrap this into something with a bit better name than the raw thing |
16 | | -export type NotionBlock = BlockObjectResponse; |
17 | | - |
18 | | -type linkConversionFunction = ( |
19 | | - context: IDocuNotionContext, |
20 | | - markdownLink: string |
21 | | -) => string; |
22 | | - |
23 | | -export type IPlugin = { |
24 | | - // this is just for display when debugging |
25 | | - name: string; |
26 | | - // operations on notion blocks before they are converted to markdown |
27 | | - notionBlockModifications?: { |
28 | | - modify: (block: NotionBlock) => void; |
29 | | - }[]; |
30 | | - // overrides for the default notion-to-markdown conversions |
31 | | - notionToMarkdownTransforms?: { |
32 | | - type: string; |
33 | | - getStringFromBlock: ( |
34 | | - context: IDocuNotionContext, |
35 | | - block: NotionBlock |
36 | | - ) => string | Promise<string>; |
37 | | - }[]; |
38 | | - |
39 | | - // corrections to links after they are converted to markdown |
40 | | - linkModifier?: { |
41 | | - match: RegExp; // does this plugin apply to this link? |
42 | | - convert: linkConversionFunction; |
43 | | - }; |
44 | | - |
45 | | - // simple regex replacements on the markdown output |
46 | | - regexMarkdownModifications?: IRegexMarkdownModification[]; |
47 | | - |
48 | | - // Allow a plugin to perform an async operation at the start of docu-notion. |
49 | | - // Notice that the plugin itself is given, so you can add things to it. |
50 | | - init?(plugin: IPlugin): Promise<void>; |
51 | | -}; |
52 | | - |
53 | | -export type IRegexMarkdownModification = { |
54 | | - // Should match on markdown that you want to replace |
55 | | - regex: RegExp; |
56 | | - // Based on that regex, the outputPattern will be used to replace the matched text |
57 | | - replacementPattern?: string; |
58 | | - // Instead of a pattern, you can use this if you have to ask a server somewhere for help in getting the new markdown |
59 | | - getReplacement?(s: string): Promise<string>; |
60 | | - |
61 | | - // If the output is creating things like react elements, you can import their definitions here |
62 | | - imports?: string[]; |
63 | | -}; |
64 | | - |
65 | | -export type IDocuNotionConfig = { |
66 | | - plugins: IPlugin[]; |
67 | | -}; |
68 | | -export type ICustomNotionToMarkdownConversion = ( |
69 | | - block: ListBlockChildrenResponseResult, |
70 | | - context: IDocuNotionContext |
71 | | -) => () => Promise<string>; |
72 | | - |
73 | | -export type ICounts = { |
74 | | - output_normally: number; |
75 | | - skipped_because_empty: number; |
76 | | - skipped_because_status: number; |
77 | | - skipped_because_level_cannot_have_content: number; |
78 | | -}; |
79 | | -export type IGetBlockChildrenFn = (id: string) => Promise<NotionBlock[]>; |
80 | | - |
81 | | -export type IDocuNotionContext = { |
82 | | - layoutStrategy: LayoutStrategy; |
83 | | - options: DocuNotionOptions; |
84 | | - getBlockChildren: IGetBlockChildrenFn; |
85 | | - notionToMarkdown: NotionToMarkdown; |
86 | | - directoryContainingMarkdown: string; |
87 | | - relativeFilePathToFolderContainingPage: string; |
88 | | - pages: NotionPage[]; |
89 | | - counts: ICounts; |
90 | | -}; |
| 5 | +import { IDocuNotionConfig, IPlugin } from "../plugins/pluginTypes"; |
91 | 6 |
|
92 | 7 | // read the plugins from the config file |
93 | 8 | // and add them to the map |
@@ -132,19 +47,3 @@ export async function loadConfigAsync(): Promise<IDocuNotionConfig> { |
132 | 47 | verbose(`Active plugins: [${config.plugins.map(p => p.name).join(", ")}]`); |
133 | 48 | return config; |
134 | 49 | } |
135 | | - |
136 | | -// export function getMDConversions(): Array<{ |
137 | | -// type: string; |
138 | | -// transformer: ICustomNotionToMarkdownConversion; |
139 | | -// }> { |
140 | | -// if (!config || !config.plugins) { |
141 | | -// return []; |
142 | | -// } |
143 | | -// // for each plugin that has a markdownConversion property, return the array of conversions |
144 | | -// return config.plugins.reduce((acc, plugin) => { |
145 | | -// if (plugin.notionToMarkdownTransforms) { |
146 | | -// acc.push(...plugin.notionToMarkdownTransforms); |
147 | | -// } |
148 | | -// return acc; |
149 | | -// }, [] as Array<{ type: string; transformer: ICustomNotionToMarkdownConversion }>); |
150 | | -// } |
0 commit comments