Skip to content

Commit 841f351

Browse files
yannbolligerGuille
and
Guille
authored
Add new config options & flags (#8)
* feat: augmented feats Added two features which are allowed by the Notion endpoint API and can be useful to further customize the export: - recursive, allows to export one block and alla the block children - no-files, to avoid downloading files like pdf or images (as it is optional from the UI) Also, addeda function `getMdFiles` to dump the files in a folder (this is useful if we want to use a function from langchain, for example, to load all files in a folder). * chore: Readme updated * chore: remove comment * feat: added new flags * Clean-up * Introduce config object. * Adapt readme. --------- Co-authored-by: Guille <guillermo.c.martinez@dcsl.com>
1 parent c7c353b commit 841f351

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ most accurate information.
4343
#### Constructor
4444

4545
Provide the [required Cookies](#needed-cookies) as authentification to create a
46-
new exporter client.
46+
new exporter client. For configuration options,
47+
[refer to the definition](./src/config.ts).
4748

4849
```ts
49-
const exporter = new NotionExporter(tokenV2: string, fileToken: string)
50+
const exporter = new NotionExporter(tokenV2: string, fileToken: string, config?: Config)
5051
```
5152

5253
#### Methods
@@ -130,4 +131,5 @@ use-case? Please submit issues and PRs on Github.
130131

131132
### Contributors
132133

133-
- Yann Bolliger, [@yannbolliger](https://github.com/yannbolliger).
134+
- Yann Bolliger, [@yannbolliger](https://github.com/yannbolliger)
135+
- Guillermo C. Martínez, [@telekosmos](https://github.com/telekosmos)

src/NotionExporter.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import axios, { AxiosInstance } from "axios"
22
import AdmZip from "adm-zip"
33

44
import { blockIdFromUrl, validateUuid } from "./blockId"
5+
import { Config, defaultConfig } from "./config"
56

67
interface Task {
78
id: string
@@ -12,6 +13,7 @@ interface Task {
1213
/** Lightweight client to export ZIP, Markdown or CSV files from a Notion block/page. */
1314
export class NotionExporter {
1415
protected readonly client: AxiosInstance
16+
private readonly config: Config
1517

1618
/**
1719
* Create a new NotionExporter client. To export any blocks/pages from
@@ -21,13 +23,14 @@ export class NotionExporter {
2123
* @param tokenV2 – the Notion `token_v2` Cookie value
2224
* @param fileToken – the Notion `file_token` Cookie value
2325
*/
24-
constructor(tokenV2: string, fileToken: string) {
26+
constructor(tokenV2: string, fileToken: string, config?: Config) {
2527
this.client = axios.create({
2628
baseURL: "https://www.notion.so/api/v3/",
2729
headers: {
2830
Cookie: `token_v2=${tokenV2};file_token=${fileToken}`,
2931
},
3032
})
33+
this.config = Object.assign(defaultConfig, config)
3134
}
3235

3336
/**
@@ -40,17 +43,17 @@ export class NotionExporter {
4043
const id = validateUuid(blockIdFromUrl(idOrUrl))
4144
if (!id) return Promise.reject(`Invalid URL or blockId: ${idOrUrl}`)
4245

46+
const { recursive, includeContents, ...config } = this.config
4347
const res = await this.client.post("enqueueTask", {
4448
task: {
4549
eventName: "exportBlock",
4650
request: {
4751
block: { id },
48-
recursive: false,
52+
recursive,
4953
exportOptions: {
5054
exportType: "markdown",
51-
timeZone: "Europe/Zurich",
52-
locale: "en",
53-
collectionViewExportType: "all",
55+
includeContents: !includeContents ? "no_files" : undefined,
56+
...config,
5457
},
5558
},
5659
},

src/action.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import rl from "readline"
22
import { AxiosError } from "axios"
33

44
import { NotionExporter } from "./NotionExporter"
5+
import { Config } from "./config"
56

67
export const FileType = ["md", "csv"] as const
78
type FileType = (typeof FileType)[number]
@@ -25,15 +26,15 @@ const askToken = (tokenName: string): Promise<string> => {
2526
const envOrAskToken = async (tokenName: string) =>
2627
process.env[tokenName] || (await askToken(tokenName))
2728

28-
const action = async (blockId: string, fileType: string) => {
29+
const action = async (blockId: string, fileType: string, config?: Config) => {
2930
if (!isFileType(fileType)) {
3031
console.log(`File type (-t, --type) has to be one of: ${FileType}`)
3132
process.exit(1)
3233
}
3334

3435
const tokenV2 = await envOrAskToken("NOTION_TOKEN")
3536
const fileToken = await envOrAskToken("NOTION_FILE_TOKEN")
36-
const exporter = new NotionExporter(tokenV2, fileToken)
37+
const exporter = new NotionExporter(tokenV2, fileToken, config)
3738

3839
const outputStr =
3940
fileType === "csv"

src/config.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** Configuration options that are passed to the Notion API. */
2+
export interface Config {
3+
/** Export embedded image and pdf files, not only the (text) content. Default: false */
4+
includeContents?: boolean
5+
/** Export children subpages recursively. Default: false */
6+
recursive?: boolean
7+
/** Default: UTC */
8+
timeZone?: string
9+
/** Default: en */
10+
locale?: string
11+
/** Export all blocks of the DB/page or just the ones in the current view. Default: "all" */
12+
collectionViewExportType?: "currentView" | "all"
13+
}
14+
15+
export const defaultConfig: Config = {
16+
timeZone: "UTC",
17+
locale: "en",
18+
collectionViewExportType: "all",
19+
}

src/index.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { NotionExporter } from "./NotionExporter"
44
import action, { FileType } from "./action"
55

66
export default NotionExporter
7+
export { Config, defaultConfig } from "./config"
78

89
export const cli = (args: string[]) => {
910
const pkg = require("../package.json")
@@ -21,14 +22,24 @@ export const cli = (args: string[]) => {
2122
2223
© ${pkg.author}, 2022.`
2324
)
25+
2426
.option("-t, --type", `File type to be exported: ${FileType}`, "md")
25-
// .option("-o, --output", "Output path of the exported file, stdin if empty")
27+
.option("-r, --recursive", "Export children subpages", false)
28+
.option(
29+
"-a, --all-files",
30+
"Export image and pdf files, not only content",
31+
false
32+
)
2633
.example(
2734
"https://www.notion.so/Notion-Official-83715d7703ee4b8699b5e659a4712dd8"
2835
)
2936
.example("83715d7703ee4b8699b5e659a4712dd8 -t md")
3037
.example("3af0a1e347dd40c5ba0a2c91e234b2a5 -t csv > list.csv")
31-
// .example("83715d7703ee4b8699b5e659a4712dd8 -t md -o blog.md")
32-
.action((blockId, opts) => action(blockId, opts.type))
38+
.action((blockId, opts) =>
39+
action(blockId, opts.type, {
40+
includeContents: opts["all-files"],
41+
recursive: opts.recursive,
42+
})
43+
)
3344
.parse(args)
3445
}

0 commit comments

Comments
 (0)