Skip to content

Commit 485848b

Browse files
committed
Add Dummy test plugin using docu-notion.config.ts
There was a drama around hooking up cosmicconfig that lead to using tsc instead of ts-node. See https://github.com/Codex-/cosmiconfig-typescript-loader/issues/70
1 parent 9de184f commit 485848b

File tree

8 files changed

+277
-74
lines changed

8 files changed

+277
-74
lines changed

docu-notion.config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
IDocuNotionConfig,
3+
IPlugin,
4+
NotionBlock,
5+
} from "./dist/config/configuration";
6+
import { verbose } from "./dist/log";
7+
8+
const dummyBlockModifier: IPlugin = {
9+
name: "dummyBlockModifier",
10+
11+
notionBlockModifications: [
12+
{
13+
modify: (block: NotionBlock) => {
14+
verbose("dummyBlockModifier was called");
15+
},
16+
},
17+
],
18+
};
19+
20+
const config: IDocuNotionConfig = {
21+
plugins: [dummyBlockModifier],
22+
};
23+
24+
export default config;

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
"typecheck": "tsc --noEmit",
1616
"notion-download": "node dist/index.js",
1717
"cmdhelp": "ts-node --compiler-options \"{\\\"module\\\": \\\"commonjs\\\"}\" src/index.ts",
18+
"// note that we're not using ts-node at the moment because of ": "https://github.com/Codex-/cosmiconfig-typescript-loader/issues/70",
19+
"ts": "yarn tsc && rm -rf ./docs/ && cross-var node dist/index.js",
1820
"// test out with a private sample notion db": "",
19-
"pull-test-tagged": "rm -rf ./docs/ && cross-var ts-node src/index.ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_TEST_ROOT_PAGE_ID --log-level debug --status-tag test",
20-
"pull-sample-site": "rm -rf ./docs/ && cross-var ts-node src/index.ts -n %DOCU_NOTION_INTEGRATION_TOKEN% -r $DOCU_NOTION_SAMPLE_ROOT_PAGE --log-level debug",
21-
"pull-test-tagged": "rm -rf ./docs/ && cross-var ts-node src/index.ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_TEST_ROOT_PAGE_ID --log-level debug --status-tag test",
21+
"pull-test-tagged": "yarn ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_TEST_ROOT_PAGE_ID --log-level debug --status-tag test",
22+
"pull-sample-site": "yarn ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_SAMPLE_ROOT_PAGE --log-level debug",
2223
"// test with a semi-stable/public site:": "",
23-
"pull-sample": "cross-var ts-node --compiler-options \"{\\\"module\\\": \\\"commonjs\\\"}\" src/index.ts -n %DOCU_NOTION_INTEGRATION_TOKEN% -r %DOCU_NOTION_SAMPLE_ROOT_PAGE% -m ./sample --locales en,es,fr,de --log-level verbose",
24-
"pull-sample-with-paths": "cross-var ts-node --compiler-options \"{\\\"module\\\": \\\"commonjs\\\"}\" src/index.ts -n %DOCU_NOTION_INTEGRATION_TOKEN% -r %DOCU_NOTION_SAMPLE_ROOT_PAGE% -m ./sample --img-output-path ./sample_img",
24+
"pull-sample": "yarn ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_SAMPLE_ROOT_PAGE -m ./sample --locales en,es,fr,de --log-level verbose",
25+
"pull-sample-with-paths": "yarn ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_SAMPLE_ROOT_PAGE -m ./sample --img-output-path ./sample_img",
2526
"postinstall": "patch-package"
2627
},
2728
"repository": {
@@ -55,6 +56,7 @@
5556
"chalk": "^4.1.2",
5657
"commander": "^9.2.0",
5758
"cosmiconfig": "^8.0.0",
59+
"cosmiconfig-typescript-loader": "^4.3.0",
5860
"file-type": "16.5.1",
5961
"fs-extra": "^10.1.0",
6062
"limiter": "^2.1.0",

src/config/configuration.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types";
55
import * as Cosmic from "cosmiconfig";
6-
import { CosmiconfigResult } from "cosmiconfig/dist/types";
76
import { NotionPage } from "../NotionPage";
87
import { NotionToMarkdown } from "notion-to-md";
98
import { BlockObjectResponse } from "@notionhq/client/build/src/api-endpoints";
109
import { DocuNotionOptions } from "../pull";
1110
import { LayoutStrategy } from "../LayoutStrategy";
1211
import defaultConfig from "./default.docunotion.config";
12+
import { error, info, verbose } from "../log";
13+
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";
1314

1415
// wrap this into something with a bit better name than the raw thing
1516
export type NotionBlock = BlockObjectResponse;
@@ -80,12 +81,39 @@ export type IDocuNotionContext = {
8081

8182
// read the plugins from the config file
8283
// and add them to the map
83-
export function loadConfig(): IDocuNotionConfig {
84-
// return Cosmic.cosmiconfigSync("docunotion").search()
84+
export async function loadConfigAsync(): Promise<IDocuNotionConfig> {
85+
let config: IDocuNotionConfig = defaultConfig;
86+
try {
87+
const cosmic = Cosmic.cosmiconfig("docu-notion", {
88+
loaders: {
89+
".ts": TypeScriptLoader(),
90+
},
91+
searchPlaces: [`docu-notion.config.ts`],
92+
});
93+
const found = await cosmic.search();
94+
if (found) {
95+
verbose(`Loading config from ${found.filepath}`);
96+
} else {
97+
verbose(`Did not find configuration file, using defaults.`);
98+
}
8599

86-
return defaultConfig;
100+
// for now, all we have is plugins
101+
config = {
102+
plugins: defaultConfig.plugins.concat(found?.config?.plugins || []),
103+
};
104+
} catch (e: any) {
105+
error(e.message);
106+
}
107+
verbose(`Active plugins: [${config.plugins.map(p => p.name).join(", ")}]`);
108+
return config;
87109
}
88110

111+
export interface IPlug {
112+
name: string;
113+
sayHello: (block: IFoo) => string;
114+
}
115+
export interface IFoo {}
116+
89117
// export function getMDConversions(): Array<{
90118
// type: string;
91119
// transformer: ICustomNotionToMarkdownConversion;

src/images.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export async function initImageHandling(
5555
imagePrefix = prefix.replace(/\/$/, "");
5656
imageOutputPath = outputPath;
5757
locales = incomingLocales;
58-
console.log("locales:" + JSON.stringify(locales));
5958

6059
// Currently we don't delete the image directory, because if an image
6160
// changes, it gets a new id. This way can then prevent downloading

src/pull.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import { initImageHandling, cleanupOldImages } from "./images";
99
import * as Path from "path";
1010
import { error, heading, info, logDebug, verbose, warning } from "./log";
1111
import {
12+
IDocuNotionConfig,
1213
IDocuNotionContext,
13-
loadConfig,
14+
loadConfigAsync,
1415
NotionBlock,
1516
} from "./config/configuration";
1617
import { getMarkdownForPage } from "./transform";
@@ -51,7 +52,9 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
5152
optionsForLogging.notionToken =
5253
optionsForLogging.notionToken.substring(0, 3) + "...";
5354

54-
verbose(JSON.stringify(optionsForLogging, null, 2));
55+
const config = await loadConfigAsync();
56+
57+
verbose(`Options:${JSON.stringify(optionsForLogging, null, 2)}`);
5558
await initImageHandling(
5659
options.imgPrefixInMarkdown || options.imgOutputPath || "",
5760
options.imgOutputPath || "",
@@ -80,7 +83,7 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
8083
heading(
8184
`Stage 2: convert ${pages.length} Notion pages to markdown and save locally...`
8285
);
83-
await outputPages(options, pages);
86+
await outputPages(options, config, pages);
8487
info(``);
8588
heading("Stage 3: clean up old files & images...");
8689
await layoutStrategy.cleanupOldFiles();
@@ -89,9 +92,9 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
8992

9093
async function outputPages(
9194
options: DocuNotionOptions,
95+
config: IDocuNotionConfig,
9296
pages: Array<NotionPage>
9397
) {
94-
const config = loadConfig();
9598
const context: IDocuNotionContext = {
9699
getBlockChildren: getBlockChildren,
97100
directoryContainingMarkdown: "", // this changes with each page

tsconfig.json

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
{
22
"compilerOptions": {
33
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4-
54
/* Basic Options */
65
// "incremental": true, /* Enable incremental compilation */
7-
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8-
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9-
"lib": ["ES2021"], /* Specify library files to be included in the compilation. */
6+
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
7+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
8+
"lib": [
9+
"ES2021"
10+
], /* Specify library files to be included in the compilation. */
1011
// "allowJs": true, /* Allow javascript files to be compiled. */
1112
// "checkJs": true, /* Report errors in .js files. */
1213
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
13-
"declaration": true, /* Generates corresponding '.d.ts' file. */
14+
"declaration": true, /* Generates corresponding '.d.ts' file. */
1415
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
1516
// "sourceMap": true, /* Generates corresponding '.map' file. */
1617
// "outFile": "./", /* Concatenate and emit output to single file. */
17-
"outDir": "dist", /* Redirect output structure to the directory. */
18+
"outDir": "dist", /* Redirect output structure to the directory. */
1819
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
1920
// "composite": true, /* Enable project compilation */
2021
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
@@ -23,25 +24,22 @@
2324
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
2425
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
2526
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26-
2727
/* Strict Type-Checking Options */
28-
"strict": true, /* Enable all strict type-checking options. */
28+
"strict": true, /* Enable all strict type-checking options. */
2929
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
3030
// "strictNullChecks": true, /* Enable strict null checks. */
3131
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
3232
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
3333
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
3434
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
3535
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36-
3736
/* Additional Checks */
3837
// "noUnusedLocals": true, /* Report errors on unused locals. */
3938
// "noUnusedParameters": true, /* Report errors on unused parameters. */
4039
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
4140
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
4241
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
4342
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
44-
4543
/* Module Resolution Options */
4644
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4745
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
@@ -50,24 +48,28 @@
5048
// "typeRoots": [], /* List of folders to include type definitions from. */
5149
// "types": [], /* Type declaration files to be included in compilation. */
5250
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
53-
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
51+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5452
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
5553
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
56-
5754
/* Source Map Options */
5855
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
5956
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
6057
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
6158
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
62-
6359
/* Experimental Options */
6460
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
6561
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
66-
6762
/* Advanced Options */
68-
"skipLibCheck": true, /* Skip type checking of declaration files. */
69-
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
63+
"skipLibCheck": true, /* Skip type checking of declaration files. */
64+
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
7065
"resolveJsonModule": true, /* include our version.json file */
7166
},
72-
"include": ["src/**/*.ts", "src/**/*.json", "test/**/*.ts"],
73-
}
67+
"include": [
68+
"src/**/*.ts",
69+
"src/**/*.json",
70+
"test/**/*.ts"
71+
],
72+
"exclude": [
73+
"*.config.ts"
74+
]
75+
}

0 commit comments

Comments
 (0)