-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a638cc
commit f99d269
Showing
15 changed files
with
261 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
packages/notion-downloader/src/config/default.plugin.config.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { standardInternalLinkConversion } from "../plugins/internalLinks" | ||
import { IPlugin } from "../plugins/pluginTypes" | ||
|
||
// TODO: Create a section in the documentation that explains all plugins. | ||
|
||
export const defaultPlugins: IPlugin[] = [standardInternalLinkConversion] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { z } from "zod" | ||
|
||
import { IPlugin } from "../plugins/pluginTypes" | ||
|
||
const NotionBlockModification = z.object({ | ||
modify: z.function().args(z.any()).returns(z.void()), | ||
}) | ||
|
||
const NotionToMarkdownTransform = z.object({ | ||
type: z.string(), | ||
getStringFromBlock: z | ||
.function() | ||
.args(z.any(), z.any()) | ||
.returns(z.union([z.string(), z.promise(z.string())])), | ||
}) | ||
|
||
const LinkModifier = z.object({ | ||
match: z.instanceof(RegExp), | ||
convert: z.function().args(z.any(), z.string()).returns(z.string()), | ||
}) | ||
|
||
const RegexMarkdownModification = z.object({ | ||
regex: z.instanceof(RegExp), | ||
replacementPattern: z.string().optional(), | ||
getReplacement: z | ||
.function() | ||
.args(z.any(), z.any()) | ||
.returns(z.promise(z.string())) | ||
.optional(), | ||
includeCodeBlocks: z.boolean().optional(), | ||
imports: z.array(z.string()).optional(), | ||
}) | ||
|
||
export const NotionToMdPlugin = z.object({ | ||
name: z.string(), | ||
notionBlockModifications: z.array(NotionBlockModification).optional(), | ||
notionToMarkdownTransforms: z.array(NotionToMarkdownTransform).optional(), | ||
linkModifier: LinkModifier.optional(), | ||
regexMarkdownModifications: z.array(RegexMarkdownModification).optional(), | ||
init: z.function().args(z.any()).returns(z.promise(z.void())).optional(), | ||
}) | ||
|
||
export type NotionToMdPlugin = z.infer<typeof NotionToMdPlugin> | ||
|
||
// Asserting that IPlugin satisfies NotionToMdPlugin | ||
// NOTE: We maintain a zod type for verification and a typescript type for the interface | ||
// this is because some of the arguments are too complex for a zod type, like `NotionBlock` | ||
export const _: NotionToMdPlugin = {} as IPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { exit } from "process" | ||
import * as Cosmic from "cosmiconfig" | ||
import { TypeScriptLoader } from "cosmiconfig-typescript-loader" | ||
|
||
import { error, verbose } from "../log" | ||
import { IPlugin } from "../plugins/pluginTypes" | ||
import { standardPluginsDict } from "../plugins/standardPlugins" | ||
import { handleError } from "../utils/handle-error" | ||
import { defaultPlugins } from "./defaultPlugins" | ||
import { NotionToMdPlugin } from "./pluginSchema" | ||
import { PluginsConfig } from "./schema" | ||
|
||
// TODO: Remove IPluginsConfig | ||
export type IPluginsConfig = { | ||
plugins: IPlugin[] | ||
} | ||
|
||
function loadOfficialPlugin(pluginName: string): NotionToMdPlugin { | ||
if (!(pluginName in standardPluginsDict)) { | ||
throw new Error(`Official plugin "${pluginName}" not found`) | ||
} | ||
return NotionToMdPlugin.parse(standardPluginsDict[pluginName]) | ||
} | ||
|
||
async function initializePlugin( | ||
plugin: NotionToMdPlugin | ||
): Promise<NotionToMdPlugin> { | ||
if (plugin.init) { | ||
verbose(`Initializing plugin ${plugin.name}...`) | ||
await plugin.init(plugin) | ||
} | ||
return plugin | ||
} | ||
|
||
export async function loadPlugins( | ||
configPlugins: PluginsConfig | ||
): Promise<IPlugin[]> { | ||
let resultingPlugins: IPlugin[] = [] | ||
|
||
try { | ||
const loadedPlugins: IPlugin[] = await Promise.all( | ||
configPlugins.map(async (plugin) => { | ||
if (typeof plugin === "string") { | ||
return await initializePlugin(loadOfficialPlugin(plugin)) | ||
} else { | ||
return await initializePlugin(plugin) | ||
} | ||
}) | ||
) | ||
|
||
resultingPlugins = defaultPlugins.concat(loadedPlugins) | ||
} catch (e: any) { | ||
handleError(e.message) | ||
} | ||
|
||
verbose(`Active plugins: [${resultingPlugins.map((p) => p.name).join(", ")}]`) | ||
return resultingPlugins | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { standardCalloutTransformer } from "./CalloutTransformer" | ||
import { standardColumnListTransformer } from "./ColumnListTransformer" | ||
import { standardColumnTransformer } from "./ColumnTransformer" | ||
import { standardEscapeHtmlBlockModifier } from "./EscapeHtmlBlockModifier" | ||
import { standardHeadingTransformer } from "./HeadingTransformer" | ||
import { standardTableTransformer } from "./TableTransformer" | ||
import { standardVideoTransformer } from "./VideoTransformer" | ||
import { gifEmbed, imgurGifEmbed } from "./embedTweaks" | ||
import { standardExternalLinkConversion } from "./externalLinks" | ||
import { standardInternalLinkConversion } from "./internalLinks" | ||
import { IPlugin } from "./pluginTypes" | ||
|
||
// TODO: Create a section in the documentation that explains all plugins. | ||
|
||
const standardPlugins = [ | ||
// Notion "Block" JSON modifiers | ||
standardEscapeHtmlBlockModifier, | ||
standardHeadingTransformer, // does operations on both the Notion JSON and then later, on the notion to markdown transform | ||
|
||
// Notion to Markdown transformers. Most things get transformed correctly by the notion-to-markdown library, | ||
// but some things need special handling. | ||
standardColumnTransformer, // STandard column transformer uses notion unofficial API | ||
standardColumnListTransformer, | ||
standardCalloutTransformer, | ||
standardTableTransformer, | ||
|
||
standardVideoTransformer, | ||
|
||
// Link modifiers, which are special because they can read metadata from all the pages in order to figure out the correct url | ||
standardInternalLinkConversion, | ||
standardExternalLinkConversion, | ||
|
||
// Regexps plus javascript `import`s that operate on the Markdown output | ||
imgurGifEmbed, | ||
gifEmbed, | ||
] | ||
|
||
export const standardPluginsDict: Record<string, IPlugin> = Object.fromEntries( | ||
standardPlugins.map((plugin) => [plugin.name, plugin]) | ||
) |
Oops, something went wrong.