notion-to-md 2.0.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -55
- package/build/index.d.ts +2 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +14 -0
- package/build/index.js.map +1 -0
- package/build/notion-to-md.d.ts +44 -0
- package/build/notion-to-md.d.ts.map +1 -0
- package/build/notion-to-md.js +303 -0
- package/build/notion-to-md.js.map +1 -0
- package/build/types/index.d.ts +48 -0
- package/build/types/index.d.ts.map +1 -0
- package/build/types/index.js +3 -0
- package/build/types/index.js.map +1 -0
- package/build/utils/md.d.ts +22 -0
- package/build/utils/md.d.ts.map +1 -0
- package/build/utils/md.js +98 -0
- package/build/utils/md.js.map +1 -0
- package/build/utils/notion.d.ts +6 -0
- package/build/utils/notion.d.ts.map +1 -0
- package/build/utils/notion.js +26 -0
- package/build/utils/notion.js.map +1 -0
- package/package.json +16 -6
- package/notion-to-md.js +0 -151
- package/utils/md.js +0 -68
- package/utils/notion.js +0 -34
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
<img src="https://imgur.com/WgXdz9r.png" />
|
|
2
2
|
|
|
3
|
+
> 💡 For better readability and detailed instructions headover to the [wiki](https://github.com/souvikinator/notion-to-md/wiki)
|
|
4
|
+
|
|
3
5
|
# Notion to Markdown
|
|
4
6
|
|
|
5
7
|
Convert notion pages, block and list of blocks to markdown (supports nesting) using **[notion-sdk-js](https://github.com/makenotion/notion-sdk-js)**
|
|
@@ -20,10 +22,11 @@ Convert notion pages, block and list of blocks to markdown (supports nesting) us
|
|
|
20
22
|
- [x] nested blocks
|
|
21
23
|
- [ ] pages inside pages/child page
|
|
22
24
|
- [x] embeds, bookmarks, videos, files (converted to links)
|
|
23
|
-
- [
|
|
25
|
+
- [x] Simple tables
|
|
24
26
|
- [x] divider
|
|
25
27
|
- [x] equation block (converted to code blocks)
|
|
26
28
|
- [x] convert returned markdown object to string (`toMarkdownString()`)
|
|
29
|
+
- [x] typescript support
|
|
27
30
|
- [ ] add tests
|
|
28
31
|
|
|
29
32
|
## Install
|
|
@@ -34,6 +37,8 @@ $ npm install notion-to-md
|
|
|
34
37
|
|
|
35
38
|
## Usage
|
|
36
39
|
|
|
40
|
+
> **Note:** Details on methods can be found in [API section](https://github.com/souvikinator/notion-to-md#api)
|
|
41
|
+
|
|
37
42
|
### converting markdown objects to markdown string
|
|
38
43
|
|
|
39
44
|
This is how the notion page looks for this example:
|
|
@@ -42,14 +47,16 @@ This is how the notion page looks for this example:
|
|
|
42
47
|
|
|
43
48
|
```js
|
|
44
49
|
const { Client } = require("@notionhq/client");
|
|
45
|
-
const
|
|
50
|
+
const { NotionToMarkdown } = require("notion-to-md");
|
|
51
|
+
// or
|
|
52
|
+
// import {NotionToMarkdown} from "notion-to-md";
|
|
46
53
|
|
|
47
54
|
const notion = new Client({
|
|
48
55
|
auth: "your integration token",
|
|
49
56
|
});
|
|
50
57
|
|
|
51
58
|
// passing notion client to the option
|
|
52
|
-
const n2m = new
|
|
59
|
+
const n2m = new NotionToMarkdown({ notionClient: notion });
|
|
53
60
|
|
|
54
61
|
(async () => {
|
|
55
62
|
const mdblocks = await n2m.pageToMarkdown("target_page_id");
|
|
@@ -66,24 +73,22 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
66
73
|
|
|
67
74
|

|
|
68
75
|
|
|
69
|
-
> **Note:** Details on provided methods can be found in [API section](https://github.com/souvikinator/notion-to-md#api)
|
|
70
|
-
|
|
71
76
|
### converting page to markdown object
|
|
72
77
|
|
|
73
|
-
|
|
78
|
+
Example notion page:
|
|
74
79
|
|
|
75
80
|

|
|
76
81
|
|
|
77
82
|
```js
|
|
78
83
|
const { Client } = require("@notionhq/client");
|
|
79
|
-
const
|
|
84
|
+
const { NotionToMarkdown } = require("notion-to-md");
|
|
80
85
|
|
|
81
86
|
const notion = new Client({
|
|
82
87
|
auth: "your integration token",
|
|
83
88
|
});
|
|
84
89
|
|
|
85
90
|
// passing notion client to the option
|
|
86
|
-
const n2m = new
|
|
91
|
+
const n2m = new NotionToMarkdown({ notionClient: notion });
|
|
87
92
|
|
|
88
93
|
(async () => {
|
|
89
94
|
// notice second argument, totalPage.
|
|
@@ -92,11 +97,6 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
92
97
|
})();
|
|
93
98
|
```
|
|
94
99
|
|
|
95
|
-
> `totalPage`: Default value is `1` which means only `100` blocks will be converted to markdown and rest will be ignored (due to notion api limitations, ref: [#9](https://github.com/souvikinator/notion-to-md/pull/9)).
|
|
96
|
-
>
|
|
97
|
-
> - if the notion page contains less than or equal `100` blocks then `totalPage` arg is not required.
|
|
98
|
-
> - if the notion page contains `150` blocks then `totalPage` argument should be greater than or equal to `2` leading to `pageSize = 2 * 100` and rendering all `150` blocks.
|
|
99
|
-
|
|
100
100
|
**Output:**
|
|
101
101
|
|
|
102
102
|
```json
|
|
@@ -144,20 +144,18 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
144
144
|
|
|
145
145
|
### converting list of blocks to markdown object
|
|
146
146
|
|
|
147
|
-
> This example blocks from a single page is used. One construct array of blocks from different notion pages and pass in to the `blocksToMarkdown()`
|
|
148
|
-
|
|
149
147
|
same notion page as before
|
|
150
148
|
|
|
151
149
|
```js
|
|
152
150
|
const { Client } = require("@notionhq/client");
|
|
153
|
-
const
|
|
151
|
+
const { NotionToMarkdown } = require("notion-to-md");
|
|
154
152
|
|
|
155
153
|
const notion = new Client({
|
|
156
154
|
auth: "your integration token",
|
|
157
155
|
});
|
|
158
156
|
|
|
159
157
|
// passing notion client to the option
|
|
160
|
-
const n2m = new
|
|
158
|
+
const n2m = new NotionToMarkdown({ notionClient: notion });
|
|
161
159
|
|
|
162
160
|
(async () => {
|
|
163
161
|
// get all blocks in the page
|
|
@@ -177,13 +175,13 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
177
175
|
|
|
178
176
|
- only takes a single notion block and returns corresponding markdown string
|
|
179
177
|
- nesting is ignored
|
|
180
|
-
-
|
|
178
|
+
- depends on @notionhq/client
|
|
181
179
|
|
|
182
180
|
```js
|
|
183
|
-
const
|
|
181
|
+
const { NotionToMarkdown } = require("notion-to-md");
|
|
184
182
|
|
|
185
|
-
// notion client
|
|
186
|
-
const n2m = new
|
|
183
|
+
// passing notion client to the option
|
|
184
|
+
const n2m = new NotionToMarkdown({ notionClient: notion });
|
|
187
185
|
|
|
188
186
|
const result = n2m.blockToMarkdown(block);
|
|
189
187
|
console.log(result);
|
|
@@ -195,40 +193,6 @@ console.log(result);
|
|
|
195
193
|

|
|
196
194
|
```
|
|
197
195
|
|
|
198
|
-
## API
|
|
199
|
-
|
|
200
|
-
### `toMarkdownString(markdownObjects)`
|
|
201
|
-
|
|
202
|
-
- takes output of `pageToMarkdown` or `blocksToMarkdown` as argument
|
|
203
|
-
- convert to markdown string.
|
|
204
|
-
|
|
205
|
-
### `pageToMarkdown(page_id,totalPage)`
|
|
206
|
-
|
|
207
|
-
- Uses `blocksToMarkdown` internally.
|
|
208
|
-
- Takes `page_id` as input and converts all the blocks in the page to corresponding markdown object
|
|
209
|
-
- `totalPage` is the retrieve block children request number i.e `page_size Maximum = totalPage * 100`.
|
|
210
|
-
|
|
211
|
-
> `totalPage`: Default value is `1` which means only `100` blocks will be converted to markdown and rest will be ignored (due to notion api limitations, ref: [#9](https://github.com/souvikinator/notion-to-md/pull/9)).
|
|
212
|
-
>
|
|
213
|
-
> - if the notion page contains less than or equal `100` blocks then `totalPage` arg is not required.
|
|
214
|
-
> - if the notion page contains `150` blocks then `totalPage` argument should be greater than or equal to `2` leading to `pageSize = 2 * 100` and rendering all blocks withing limit `200`.
|
|
215
|
-
|
|
216
|
-
### `blocksToMarkdown(blocks,totalPage)`
|
|
217
|
-
|
|
218
|
-
> **Note**: requires <u>**notion-sdk-js**</u> unlike `blockToMarkdown`
|
|
219
|
-
|
|
220
|
-
- `blocks`: array of notion blocks
|
|
221
|
-
- `totalPage`: the retrieve block children request number i.e `page_size Maximum = totalPage * 100`.
|
|
222
|
-
- deals with <u>**nested blocks**</u>
|
|
223
|
-
- uses `blockToMarkdown` internally.
|
|
224
|
-
|
|
225
|
-
### `blockToMarkdown(block)`
|
|
226
|
-
|
|
227
|
-
- Takes single notion block and converts to markdown string
|
|
228
|
-
- does not deal with nested notion blocks
|
|
229
|
-
- This method doesn't require the `notion-sdk-js`.
|
|
230
|
-
- Refer docs to know more about [notion blocks](https://developers.notion.com/reference/block)
|
|
231
|
-
|
|
232
196
|
## Contribution
|
|
233
197
|
|
|
234
198
|
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./notion-to-md"), exports);
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAA+B","sourcesContent":["export * from \"./notion-to-md\";\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Annotations, ListBlockChildrenResponseResult, ListBlockChildrenResponseResults, MdBlock, NotionToMarkdownOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a Notion page to Markdown.
|
|
4
|
+
*/
|
|
5
|
+
export declare class NotionToMarkdown {
|
|
6
|
+
private notionClient;
|
|
7
|
+
constructor(options: NotionToMarkdownOptions);
|
|
8
|
+
/**
|
|
9
|
+
* Converts Markdown Blocks to string
|
|
10
|
+
* @param {MdBlock[]} mdBlocks - Array of markdown blocks
|
|
11
|
+
* @param {number} nestingLevel - Defines max depth of nesting
|
|
12
|
+
* @returns {string} - Returns markdown string
|
|
13
|
+
*/
|
|
14
|
+
toMarkdownString(mdBlocks?: MdBlock[], nestingLevel?: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves Notion Blocks based on ID and converts them to Markdown Blocks
|
|
17
|
+
* @param {string} id - notion page id (not database id)
|
|
18
|
+
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100 (Default=null)
|
|
19
|
+
* @returns {Promise<MdBlock[]>} - List of markdown blocks
|
|
20
|
+
*/
|
|
21
|
+
pageToMarkdown(id: string, totalPage?: number | null): Promise<MdBlock[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Converts list of Notion Blocks to Markdown Blocks
|
|
24
|
+
* @param {ListBlockChildrenResponseResults | undefined} blocks - List of notion blocks
|
|
25
|
+
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
|
|
26
|
+
* @param {MdBlock[]} mdBlocks - Defines max depth of nesting
|
|
27
|
+
* @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children
|
|
28
|
+
*/
|
|
29
|
+
blocksToMarkdown(blocks?: ListBlockChildrenResponseResults, totalPage?: number | null, mdBlocks?: MdBlock[]): Promise<MdBlock[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Converts a Notion Block to a Markdown Block
|
|
32
|
+
* @param {ListBlockChildrenResponseResult} block - single notion block
|
|
33
|
+
* @returns {string} corresponding markdown string of the passed block
|
|
34
|
+
*/
|
|
35
|
+
blockToMarkdown(block: ListBlockChildrenResponseResult): Promise<string>;
|
|
36
|
+
/**
|
|
37
|
+
* Annoate text using provided annotations
|
|
38
|
+
* @param {string} text - String to be annotated
|
|
39
|
+
* @param {Annotations} annotations - Annotation object of a notion block
|
|
40
|
+
* @returns {string} - Annotated text
|
|
41
|
+
*/
|
|
42
|
+
annotatePlainText(text: string, annotations: Annotations): string;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=notion-to-md.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notion-to-md.d.ts","sourceRoot":"","sources":["../src/notion-to-md.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,+BAA+B,EAC/B,gCAAgC,EAChC,OAAO,EAEP,uBAAuB,EACxB,MAAM,SAAS,CAAC;AAIjB;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,YAAY,CAAS;gBAEjB,OAAO,EAAE,uBAAuB;IAI5C;;;;;OAKG;IACH,gBAAgB,CAAC,QAAQ,GAAE,OAAO,EAAO,EAAE,YAAY,GAAE,MAAU,GAAG,MAAM;IAe5E;;;;;OAKG;IACG,cAAc,CAClB,EAAE,EAAE,MAAM,EACV,SAAS,GAAE,MAAM,GAAG,IAAW,GAC9B,OAAO,CAAC,OAAO,EAAE,CAAC;IAarB;;;;;;OAMG;IACG,gBAAgB,CACpB,MAAM,CAAC,EAAE,gCAAgC,EACzC,SAAS,GAAE,MAAM,GAAG,IAAW,EAC/B,QAAQ,GAAE,OAAO,EAAO,GACvB,OAAO,CAAC,OAAO,EAAE,CAAC;IAsCrB;;;;OAIG;IACG,eAAe,CAAC,KAAK,EAAE,+BAA+B;IA+L5D;;;;;OAKG;IACH,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,MAAM;CAsBlE"}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.NotionToMarkdown = void 0;
|
|
23
|
+
const md = __importStar(require("./utils/md"));
|
|
24
|
+
const notion_1 = require("./utils/notion");
|
|
25
|
+
/**
|
|
26
|
+
* Converts a Notion page to Markdown.
|
|
27
|
+
*/
|
|
28
|
+
class NotionToMarkdown {
|
|
29
|
+
constructor(options) {
|
|
30
|
+
this.notionClient = options.notionClient;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Converts Markdown Blocks to string
|
|
34
|
+
* @param {MdBlock[]} mdBlocks - Array of markdown blocks
|
|
35
|
+
* @param {number} nestingLevel - Defines max depth of nesting
|
|
36
|
+
* @returns {string} - Returns markdown string
|
|
37
|
+
*/
|
|
38
|
+
toMarkdownString(mdBlocks = [], nestingLevel = 0) {
|
|
39
|
+
let mdString = "";
|
|
40
|
+
mdBlocks.forEach((mdBlocks) => {
|
|
41
|
+
if (mdBlocks.parent) {
|
|
42
|
+
mdString += `
|
|
43
|
+
${md.addTabSpace(mdBlocks.parent, nestingLevel)}
|
|
44
|
+
`;
|
|
45
|
+
}
|
|
46
|
+
if (mdBlocks.children && mdBlocks.children.length > 0) {
|
|
47
|
+
mdString += this.toMarkdownString(mdBlocks.children, nestingLevel + 1);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return mdString;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Retrieves Notion Blocks based on ID and converts them to Markdown Blocks
|
|
54
|
+
* @param {string} id - notion page id (not database id)
|
|
55
|
+
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100 (Default=null)
|
|
56
|
+
* @returns {Promise<MdBlock[]>} - List of markdown blocks
|
|
57
|
+
*/
|
|
58
|
+
async pageToMarkdown(id, totalPage = null) {
|
|
59
|
+
if (!this.notionClient) {
|
|
60
|
+
throw new Error("notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md");
|
|
61
|
+
}
|
|
62
|
+
const blocks = await (0, notion_1.getBlockChildren)(this.notionClient, id, totalPage);
|
|
63
|
+
const parsedData = await this.blocksToMarkdown(blocks);
|
|
64
|
+
return parsedData;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Converts list of Notion Blocks to Markdown Blocks
|
|
68
|
+
* @param {ListBlockChildrenResponseResults | undefined} blocks - List of notion blocks
|
|
69
|
+
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
|
|
70
|
+
* @param {MdBlock[]} mdBlocks - Defines max depth of nesting
|
|
71
|
+
* @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children
|
|
72
|
+
*/
|
|
73
|
+
async blocksToMarkdown(blocks, totalPage = null, mdBlocks = []) {
|
|
74
|
+
if (!this.notionClient) {
|
|
75
|
+
throw new Error("notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md");
|
|
76
|
+
}
|
|
77
|
+
if (!blocks)
|
|
78
|
+
return mdBlocks;
|
|
79
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
80
|
+
let block = blocks[i];
|
|
81
|
+
if ("has_children" in block && block.has_children) {
|
|
82
|
+
let child_blocks = await (0, notion_1.getBlockChildren)(this.notionClient, block.id, totalPage);
|
|
83
|
+
mdBlocks.push({
|
|
84
|
+
parent: await this.blockToMarkdown(block),
|
|
85
|
+
children: [],
|
|
86
|
+
});
|
|
87
|
+
let l = mdBlocks.length;
|
|
88
|
+
await this.blocksToMarkdown(child_blocks, totalPage, mdBlocks[l - 1].children);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
let tmp = await this.blockToMarkdown(block);
|
|
92
|
+
mdBlocks.push({ parent: tmp, children: [] });
|
|
93
|
+
}
|
|
94
|
+
return mdBlocks;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Converts a Notion Block to a Markdown Block
|
|
98
|
+
* @param {ListBlockChildrenResponseResult} block - single notion block
|
|
99
|
+
* @returns {string} corresponding markdown string of the passed block
|
|
100
|
+
*/
|
|
101
|
+
async blockToMarkdown(block) {
|
|
102
|
+
if (!("type" in block))
|
|
103
|
+
return "";
|
|
104
|
+
let parsedData = "";
|
|
105
|
+
const { type } = block;
|
|
106
|
+
// console.log({ block });
|
|
107
|
+
switch (type) {
|
|
108
|
+
case "image":
|
|
109
|
+
{
|
|
110
|
+
let blockContent = block.image;
|
|
111
|
+
const image_caption_plain = blockContent.caption
|
|
112
|
+
.map((item) => item.plain_text)
|
|
113
|
+
.join("");
|
|
114
|
+
const image_type = blockContent.type;
|
|
115
|
+
if (image_type === "external")
|
|
116
|
+
return md.image(image_caption_plain, blockContent.external.url);
|
|
117
|
+
if (image_type === "file")
|
|
118
|
+
return md.image(image_caption_plain, blockContent.file.url);
|
|
119
|
+
}
|
|
120
|
+
break;
|
|
121
|
+
case "divider": {
|
|
122
|
+
return md.divider();
|
|
123
|
+
}
|
|
124
|
+
case "equation": {
|
|
125
|
+
return md.codeBlock(block.equation.expression);
|
|
126
|
+
}
|
|
127
|
+
case "video":
|
|
128
|
+
case "file":
|
|
129
|
+
case "pdf":
|
|
130
|
+
{
|
|
131
|
+
let blockContent;
|
|
132
|
+
if (type === "video")
|
|
133
|
+
blockContent = block.video;
|
|
134
|
+
if (type === "file")
|
|
135
|
+
blockContent = block.file;
|
|
136
|
+
if (type === "pdf")
|
|
137
|
+
blockContent = block.pdf;
|
|
138
|
+
if (blockContent) {
|
|
139
|
+
const file_type = blockContent.type;
|
|
140
|
+
if (file_type === "external")
|
|
141
|
+
return md.link("image", blockContent.external.url);
|
|
142
|
+
if (file_type === "file")
|
|
143
|
+
return md.link("image", blockContent.file.url);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
case "bookmark":
|
|
148
|
+
case "embed":
|
|
149
|
+
case "link_preview":
|
|
150
|
+
{
|
|
151
|
+
let blockContent;
|
|
152
|
+
if (type === "bookmark")
|
|
153
|
+
blockContent = block.bookmark;
|
|
154
|
+
if (type === "embed")
|
|
155
|
+
blockContent = block.embed;
|
|
156
|
+
if (type === "link_preview")
|
|
157
|
+
blockContent = block.link_preview;
|
|
158
|
+
if (blockContent)
|
|
159
|
+
return md.link(type, blockContent.url);
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
case "table": {
|
|
163
|
+
const { id, has_children } = block;
|
|
164
|
+
let tableArr = [];
|
|
165
|
+
if (has_children) {
|
|
166
|
+
const tableRows = await (0, notion_1.getBlockChildren)(this.notionClient, id, 100);
|
|
167
|
+
// console.log(">>", tableRows);
|
|
168
|
+
let rowsPromise = tableRows === null || tableRows === void 0 ? void 0 : tableRows.map(async (row) => {
|
|
169
|
+
const { type } = row;
|
|
170
|
+
const cells = row[type]["cells"];
|
|
171
|
+
/**
|
|
172
|
+
* this is more like a hack since matching the type text was
|
|
173
|
+
* difficult. So converting each cell to paragraph type to
|
|
174
|
+
* reuse the blockToMarkdown function
|
|
175
|
+
*/
|
|
176
|
+
let cellStringPromise = cells.map(async (cell) => await this.blockToMarkdown({
|
|
177
|
+
type: "paragraph",
|
|
178
|
+
paragraph: { text: cell },
|
|
179
|
+
}));
|
|
180
|
+
const cellStringArr = await Promise.all(cellStringPromise);
|
|
181
|
+
// console.log("~~", cellStringArr);
|
|
182
|
+
tableArr.push(cellStringArr);
|
|
183
|
+
// console.log(tableArr);
|
|
184
|
+
});
|
|
185
|
+
await Promise.all(rowsPromise || []);
|
|
186
|
+
}
|
|
187
|
+
parsedData = md.table(tableArr);
|
|
188
|
+
return parsedData;
|
|
189
|
+
}
|
|
190
|
+
// Rest of the types
|
|
191
|
+
// "paragraph"
|
|
192
|
+
// "heading_1"
|
|
193
|
+
// "heading_2"
|
|
194
|
+
// "heading_3"
|
|
195
|
+
// "bulleted_list_item"
|
|
196
|
+
// "numbered_list_item"
|
|
197
|
+
// "quote"
|
|
198
|
+
// "to_do"
|
|
199
|
+
// "toggle"
|
|
200
|
+
// "template"
|
|
201
|
+
// "synced_block"
|
|
202
|
+
// "child_page"
|
|
203
|
+
// "child_database"
|
|
204
|
+
// "code"
|
|
205
|
+
// "callout"
|
|
206
|
+
// "breadcrumb"
|
|
207
|
+
// "table_of_contents"
|
|
208
|
+
// "column_list"
|
|
209
|
+
// "column"
|
|
210
|
+
// "link_to_page"
|
|
211
|
+
// "audio"
|
|
212
|
+
// "unsupported"
|
|
213
|
+
default: {
|
|
214
|
+
// In this case typescript is not able to index the types properly, hence ignoring the error
|
|
215
|
+
// @ts-ignore
|
|
216
|
+
let blockContent = block[type].text || [];
|
|
217
|
+
blockContent.map((content) => {
|
|
218
|
+
const annotations = content.annotations;
|
|
219
|
+
let plain_text = content.plain_text;
|
|
220
|
+
plain_text = this.annotatePlainText(plain_text, annotations);
|
|
221
|
+
if (content["href"])
|
|
222
|
+
plain_text = md.link(plain_text, content["href"]);
|
|
223
|
+
parsedData += plain_text;
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
switch (type) {
|
|
228
|
+
case "code":
|
|
229
|
+
{
|
|
230
|
+
parsedData = md.codeBlock(parsedData, block[type].language);
|
|
231
|
+
}
|
|
232
|
+
break;
|
|
233
|
+
case "heading_1":
|
|
234
|
+
{
|
|
235
|
+
parsedData = md.heading1(parsedData);
|
|
236
|
+
}
|
|
237
|
+
break;
|
|
238
|
+
case "heading_2":
|
|
239
|
+
{
|
|
240
|
+
parsedData = md.heading2(parsedData);
|
|
241
|
+
}
|
|
242
|
+
break;
|
|
243
|
+
case "heading_3":
|
|
244
|
+
{
|
|
245
|
+
parsedData = md.heading3(parsedData);
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
case "quote":
|
|
249
|
+
{
|
|
250
|
+
parsedData = md.quote(parsedData);
|
|
251
|
+
}
|
|
252
|
+
break;
|
|
253
|
+
case "callout":
|
|
254
|
+
{
|
|
255
|
+
parsedData = md.callout(parsedData, block[type].icon);
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
case "bulleted_list_item":
|
|
259
|
+
case "numbered_list_item":
|
|
260
|
+
{
|
|
261
|
+
parsedData = md.bullet(parsedData);
|
|
262
|
+
}
|
|
263
|
+
break;
|
|
264
|
+
case "to_do":
|
|
265
|
+
{
|
|
266
|
+
parsedData = md.todo(parsedData, block.to_do.checked);
|
|
267
|
+
}
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
return parsedData;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Annoate text using provided annotations
|
|
274
|
+
* @param {string} text - String to be annotated
|
|
275
|
+
* @param {Annotations} annotations - Annotation object of a notion block
|
|
276
|
+
* @returns {string} - Annotated text
|
|
277
|
+
*/
|
|
278
|
+
annotatePlainText(text, annotations) {
|
|
279
|
+
// if text is all spaces, don't annotate
|
|
280
|
+
if (text.match(/^\s*$/))
|
|
281
|
+
return text;
|
|
282
|
+
const leadingSpaceMatch = text.match(/^(\s*)/);
|
|
283
|
+
const trailingSpaceMatch = text.match(/(\s*)$/);
|
|
284
|
+
const leading_space = leadingSpaceMatch ? leadingSpaceMatch[0] : "";
|
|
285
|
+
const trailing_space = trailingSpaceMatch ? trailingSpaceMatch[0] : "";
|
|
286
|
+
text = text.trim();
|
|
287
|
+
if (text !== "") {
|
|
288
|
+
if (annotations.code)
|
|
289
|
+
text = md.inlineCode(text);
|
|
290
|
+
if (annotations.bold)
|
|
291
|
+
text = md.bold(text);
|
|
292
|
+
if (annotations.italic)
|
|
293
|
+
text = md.italic(text);
|
|
294
|
+
if (annotations.strikethrough)
|
|
295
|
+
text = md.strikethrough(text);
|
|
296
|
+
if (annotations.underline)
|
|
297
|
+
text = md.underline(text);
|
|
298
|
+
}
|
|
299
|
+
return leading_space + text + trailing_space;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
exports.NotionToMarkdown = NotionToMarkdown;
|
|
303
|
+
//# sourceMappingURL=notion-to-md.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notion-to-md.js","sourceRoot":"","sources":["../src/notion-to-md.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AASA,+CAAiC;AACjC,2CAAkD;AAElD;;GAEG;AACH,MAAa,gBAAgB;IAG3B,YAAY,OAAgC;QAC1C,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,gBAAgB,CAAC,WAAsB,EAAE,EAAE,eAAuB,CAAC;QACjE,IAAI,QAAQ,GAAG,EAAE,CAAC;QAClB,QAAQ,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YAC5B,IAAI,QAAQ,CAAC,MAAM,EAAE;gBACnB,QAAQ,IAAI;EAClB,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;CAC9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;QAEnB,IAAI,IAAI,KAAK,EAAE,EAAE;YACf,IAAI,WAAW,CAAC,IAAI;gBAAE,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;YACjD,IAAI,WAAW,CAAC,IAAI;gBAAE,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC3C,IAAI,WAAW,CAAC,MAAM;gBAAE,IAAI,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAC/C,IAAI,WAAW,CAAC,aAAa;gBAAE,IAAI,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YAC7D,IAAI,WAAW,CAAC,SAAS;gBAAE,IAAI,GAAG,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;SACtD;QAED,OAAO,aAAa,GAAG,IAAI,GAAG,cAAc,CAAC;IAC/C,CAAC;CACF;AAnUD,4CAmUC","sourcesContent":["import { Client } from \"@notionhq/client\";\nimport {\n Annotations,\n ListBlockChildrenResponseResult,\n ListBlockChildrenResponseResults,\n MdBlock,\n Text,\n NotionToMarkdownOptions,\n} from \"./types\";\nimport * as md from \"./utils/md\";\nimport { getBlockChildren } from \"./utils/notion\";\n\n/**\n * Converts a Notion page to Markdown.\n */\nexport class NotionToMarkdown {\n private notionClient: Client;\n\n constructor(options: NotionToMarkdownOptions) {\n this.notionClient = options.notionClient;\n }\n\n /**\n * Converts Markdown Blocks to string\n * @param {MdBlock[]} mdBlocks - Array of markdown blocks\n * @param {number} nestingLevel - Defines max depth of nesting\n * @returns {string} - Returns markdown string\n */\n toMarkdownString(mdBlocks: MdBlock[] = [], nestingLevel: number = 0): string {\n let mdString = \"\";\n mdBlocks.forEach((mdBlocks) => {\n if (mdBlocks.parent) {\n mdString += `\n${md.addTabSpace(mdBlocks.parent, nestingLevel)}\n`;\n }\n if (mdBlocks.children && mdBlocks.children.length > 0) {\n mdString += this.toMarkdownString(mdBlocks.children, nestingLevel + 1);\n }\n });\n return mdString;\n }\n\n /**\n * Retrieves Notion Blocks based on ID and converts them to Markdown Blocks\n * @param {string} id - notion page id (not database id)\n * @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100 (Default=null)\n * @returns {Promise<MdBlock[]>} - List of markdown blocks\n */\n async pageToMarkdown(\n id: string,\n totalPage: number | null = null\n ): Promise<MdBlock[]> {\n if (!this.notionClient) {\n throw new Error(\n \"notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md\"\n );\n }\n\n const blocks = await getBlockChildren(this.notionClient, id, totalPage);\n\n const parsedData = await this.blocksToMarkdown(blocks);\n return parsedData;\n }\n\n /**\n * Converts list of Notion Blocks to Markdown Blocks\n * @param {ListBlockChildrenResponseResults | undefined} blocks - List of notion blocks\n * @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100\n * @param {MdBlock[]} mdBlocks - Defines max depth of nesting\n * @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children\n */\n async blocksToMarkdown(\n blocks?: ListBlockChildrenResponseResults,\n totalPage: number | null = null,\n mdBlocks: MdBlock[] = []\n ): Promise<MdBlock[]> {\n if (!this.notionClient) {\n throw new Error(\n \"notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md\"\n );\n }\n\n if (!blocks) return mdBlocks;\n\n for (let i = 0; i < blocks.length; i++) {\n let block = blocks[i];\n\n if (\"has_children\" in block && block.has_children) {\n let child_blocks = await getBlockChildren(\n this.notionClient,\n block.id,\n totalPage\n );\n mdBlocks.push({\n parent: await this.blockToMarkdown(block),\n children: [],\n });\n\n let l = mdBlocks.length;\n await this.blocksToMarkdown(\n child_blocks,\n totalPage,\n mdBlocks[l - 1].children\n );\n continue;\n }\n let tmp = await this.blockToMarkdown(block);\n\n mdBlocks.push({ parent: tmp, children: [] });\n }\n return mdBlocks;\n }\n\n /**\n * Converts a Notion Block to a Markdown Block\n * @param {ListBlockChildrenResponseResult} block - single notion block\n * @returns {string} corresponding markdown string of the passed block\n */\n async blockToMarkdown(block: ListBlockChildrenResponseResult) {\n if (!(\"type\" in block)) return \"\";\n\n let parsedData = \"\";\n const { type } = block;\n // console.log({ block });\n\n switch (type) {\n case \"image\":\n {\n let blockContent = block.image;\n const image_caption_plain = blockContent.caption\n .map((item) => item.plain_text)\n .join(\"\");\n const image_type = blockContent.type;\n if (image_type === \"external\")\n return md.image(image_caption_plain, blockContent.external.url);\n if (image_type === \"file\")\n return md.image(image_caption_plain, blockContent.file.url);\n }\n break;\n\n case \"divider\": {\n return md.divider();\n }\n\n case \"equation\": {\n return md.codeBlock(block.equation.expression);\n }\n\n case \"video\":\n case \"file\":\n case \"pdf\":\n {\n let blockContent;\n if (type === \"video\") blockContent = block.video;\n if (type === \"file\") blockContent = block.file;\n if (type === \"pdf\") blockContent = block.pdf;\n if (blockContent) {\n const file_type = blockContent.type;\n if (file_type === \"external\")\n return md.link(\"image\", blockContent.external.url);\n if (file_type === \"file\")\n return md.link(\"image\", blockContent.file.url);\n }\n }\n break;\n\n case \"bookmark\":\n case \"embed\":\n case \"link_preview\":\n {\n let blockContent;\n if (type === \"bookmark\") blockContent = block.bookmark;\n if (type === \"embed\") blockContent = block.embed;\n if (type === \"link_preview\") blockContent = block.link_preview;\n if (blockContent) return md.link(type, blockContent.url);\n }\n break;\n\n case \"table\": {\n const { id, has_children } = block;\n let tableArr: string[][] = [];\n if (has_children) {\n const tableRows = await getBlockChildren(this.notionClient, id, 100);\n // console.log(\">>\", tableRows);\n let rowsPromise = tableRows?.map(async (row) => {\n const { type } = row as any;\n const cells = (row as any)[type][\"cells\"];\n\n /**\n * this is more like a hack since matching the type text was\n * difficult. So converting each cell to paragraph type to\n * reuse the blockToMarkdown function\n */\n let cellStringPromise = cells.map(\n async (cell: any) =>\n await this.blockToMarkdown({\n type: \"paragraph\",\n paragraph: { text: cell },\n } as ListBlockChildrenResponseResult)\n );\n\n const cellStringArr = await Promise.all(cellStringPromise);\n // console.log(\"~~\", cellStringArr);\n tableArr.push(cellStringArr);\n // console.log(tableArr);\n });\n await Promise.all(rowsPromise || []);\n }\n parsedData = md.table(tableArr);\n return parsedData;\n }\n\n // Rest of the types\n // \"paragraph\"\n // \"heading_1\"\n // \"heading_2\"\n // \"heading_3\"\n // \"bulleted_list_item\"\n // \"numbered_list_item\"\n // \"quote\"\n // \"to_do\"\n // \"toggle\"\n // \"template\"\n // \"synced_block\"\n // \"child_page\"\n // \"child_database\"\n // \"code\"\n // \"callout\"\n // \"breadcrumb\"\n // \"table_of_contents\"\n // \"column_list\"\n // \"column\"\n // \"link_to_page\"\n // \"audio\"\n // \"unsupported\"\n\n default: {\n // In this case typescript is not able to index the types properly, hence ignoring the error\n\n // @ts-ignore\n let blockContent = block[type].text || [];\n blockContent.map((content: Text) => {\n const annotations = content.annotations;\n let plain_text = content.plain_text;\n\n plain_text = this.annotatePlainText(plain_text, annotations);\n\n if (content[\"href\"])\n plain_text = md.link(plain_text, content[\"href\"]);\n\n parsedData += plain_text;\n });\n }\n }\n\n switch (type) {\n case \"code\":\n {\n parsedData = md.codeBlock(parsedData, block[type].language);\n }\n break;\n\n case \"heading_1\":\n {\n parsedData = md.heading1(parsedData);\n }\n break;\n\n case \"heading_2\":\n {\n parsedData = md.heading2(parsedData);\n }\n break;\n\n case \"heading_3\":\n {\n parsedData = md.heading3(parsedData);\n }\n break;\n\n case \"quote\":\n {\n parsedData = md.quote(parsedData);\n }\n break;\n\n case \"callout\":\n {\n parsedData = md.callout(parsedData, block[type].icon);\n }\n break;\n\n case \"bulleted_list_item\":\n case \"numbered_list_item\":\n {\n parsedData = md.bullet(parsedData);\n }\n break;\n\n case \"to_do\":\n {\n parsedData = md.todo(parsedData, block.to_do.checked);\n }\n break;\n }\n\n return parsedData;\n }\n\n /**\n * Annoate text using provided annotations\n * @param {string} text - String to be annotated\n * @param {Annotations} annotations - Annotation object of a notion block\n * @returns {string} - Annotated text\n */\n annotatePlainText(text: string, annotations: Annotations): string {\n // if text is all spaces, don't annotate\n if (text.match(/^\\s*$/)) return text;\n\n const leadingSpaceMatch = text.match(/^(\\s*)/);\n const trailingSpaceMatch = text.match(/(\\s*)$/);\n\n const leading_space = leadingSpaceMatch ? leadingSpaceMatch[0] : \"\";\n const trailing_space = trailingSpaceMatch ? trailingSpaceMatch[0] : \"\";\n\n text = text.trim();\n\n if (text !== \"\") {\n if (annotations.code) text = md.inlineCode(text);\n if (annotations.bold) text = md.bold(text);\n if (annotations.italic) text = md.italic(text);\n if (annotations.strikethrough) text = md.strikethrough(text);\n if (annotations.underline) text = md.underline(text);\n }\n\n return leading_space + text + trailing_space;\n }\n}\n"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ListBlockChildrenResponse } from "@notionhq/client/build/src/api-endpoints";
|
|
2
|
+
import { Client } from "@notionhq/client";
|
|
3
|
+
export declare type ListBlockChildrenResponseResults = ListBlockChildrenResponse["results"];
|
|
4
|
+
export declare type ListBlockChildrenResponseResult = ListBlockChildrenResponseResults[0];
|
|
5
|
+
export declare type TextRequest = string;
|
|
6
|
+
export interface NotionToMarkdownOptions {
|
|
7
|
+
notionClient: Client;
|
|
8
|
+
}
|
|
9
|
+
export declare type MdBlock = {
|
|
10
|
+
parent: string;
|
|
11
|
+
children: MdBlock[];
|
|
12
|
+
};
|
|
13
|
+
export declare type Annotations = {
|
|
14
|
+
bold: boolean;
|
|
15
|
+
italic: boolean;
|
|
16
|
+
strikethrough: boolean;
|
|
17
|
+
underline: boolean;
|
|
18
|
+
code: boolean;
|
|
19
|
+
color: "default" | "gray" | "brown" | "orange" | "yellow" | "green" | "blue" | "purple" | "pink" | "red" | "gray_background" | "brown_background" | "orange_background" | "yellow_background" | "green_background" | "blue_background" | "purple_background" | "pink_background" | "red_background";
|
|
20
|
+
};
|
|
21
|
+
export declare type Text = {
|
|
22
|
+
type: "text";
|
|
23
|
+
text: {
|
|
24
|
+
content: string;
|
|
25
|
+
link: {
|
|
26
|
+
url: TextRequest;
|
|
27
|
+
} | null;
|
|
28
|
+
};
|
|
29
|
+
annotations: Annotations;
|
|
30
|
+
plain_text: string;
|
|
31
|
+
href: string | null;
|
|
32
|
+
};
|
|
33
|
+
export declare type CalloutIcon = {
|
|
34
|
+
type: "emoji";
|
|
35
|
+
emoji?: string;
|
|
36
|
+
} | {
|
|
37
|
+
type: "external";
|
|
38
|
+
external?: {
|
|
39
|
+
url: string;
|
|
40
|
+
};
|
|
41
|
+
} | {
|
|
42
|
+
type: "file";
|
|
43
|
+
file: {
|
|
44
|
+
url: string;
|
|
45
|
+
expiry_time: string;
|
|
46
|
+
};
|
|
47
|
+
} | null;
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,0CAA0C,CAAC;AACrF,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C,oBAAY,gCAAgC,GAC1C,yBAAyB,CAAC,SAAS,CAAC,CAAC;AAEvC,oBAAY,+BAA+B,GACzC,gCAAgC,CAAC,CAAC,CAAC,CAAC;AAEtC,oBAAY,WAAW,GAAG,MAAM,CAAC;AAEjC,MAAM,WAAW,uBAAuB;IACtC,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,oBAAY,OAAO,GAAG;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,EAAE,CAAC;CACrB,CAAC;AAEF,oBAAY,WAAW,GAAG;IACxB,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;IACvB,SAAS,EAAE,OAAO,CAAC;IACnB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EACD,SAAS,GACT,MAAM,GACN,OAAO,GACP,QAAQ,GACR,QAAQ,GACR,OAAO,GACP,MAAM,GACN,QAAQ,GACR,MAAM,GACN,KAAK,GACL,iBAAiB,GACjB,kBAAkB,GAClB,mBAAmB,GACnB,mBAAmB,GACnB,kBAAkB,GAClB,iBAAiB,GACjB,mBAAmB,GACnB,iBAAiB,GACjB,gBAAgB,CAAC;CACtB,CAAC;AAEF,oBAAY,IAAI,GAAG;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE;YACJ,GAAG,EAAE,WAAW,CAAC;SAClB,GAAG,IAAI,CAAC;KACV,CAAC;IACF,WAAW,EAAE,WAAW,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CACrB,CAAC;AAEF,oBAAY,WAAW,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CAAE,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,EAAE;QAAG,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAC;KAAE,CAAC;CAAE,GAAG,IAAI,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"","sourcesContent":["import { ListBlockChildrenResponse } from \"@notionhq/client/build/src/api-endpoints\";\nimport { Client } from \"@notionhq/client\";\n\nexport type ListBlockChildrenResponseResults =\n ListBlockChildrenResponse[\"results\"];\n\nexport type ListBlockChildrenResponseResult =\n ListBlockChildrenResponseResults[0];\n\nexport type TextRequest = string;\n\nexport interface NotionToMarkdownOptions {\n notionClient: Client;\n}\n\nexport type MdBlock = {\n parent: string;\n children: MdBlock[];\n};\n\nexport type Annotations = {\n bold: boolean;\n italic: boolean;\n strikethrough: boolean;\n underline: boolean;\n code: boolean;\n color:\n | \"default\"\n | \"gray\"\n | \"brown\"\n | \"orange\"\n | \"yellow\"\n | \"green\"\n | \"blue\"\n | \"purple\"\n | \"pink\"\n | \"red\"\n | \"gray_background\"\n | \"brown_background\"\n | \"orange_background\"\n | \"yellow_background\"\n | \"green_background\"\n | \"blue_background\"\n | \"purple_background\"\n | \"pink_background\"\n | \"red_background\";\n};\n\nexport type Text = {\n type: \"text\";\n text: {\n content: string;\n link: {\n url: TextRequest;\n } | null;\n };\n annotations: Annotations;\n plain_text: string;\n href: string | null;\n};\n\nexport type CalloutIcon = { type: \"emoji\"; emoji?: string; } | { type: \"external\"; external?: { url: string }; } | { type: \"file\"; file: { url: string; expiry_time: string; }; } | null;"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CalloutIcon } from '../types';
|
|
2
|
+
export declare const inlineCode: (text: string) => string;
|
|
3
|
+
export declare const bold: (text: string) => string;
|
|
4
|
+
export declare const italic: (text: string) => string;
|
|
5
|
+
export declare const strikethrough: (text: string) => string;
|
|
6
|
+
export declare const underline: (text: string) => string;
|
|
7
|
+
export declare const link: (text: string, href: string) => string;
|
|
8
|
+
export declare const codeBlock: (text: string, language?: string | undefined) => string;
|
|
9
|
+
export declare const heading1: (text: string) => string;
|
|
10
|
+
export declare const heading2: (text: string) => string;
|
|
11
|
+
export declare const heading3: (text: string) => string;
|
|
12
|
+
export declare const quote: (text: string) => string;
|
|
13
|
+
export declare const callout: (text: string, icon?: CalloutIcon | undefined) => string;
|
|
14
|
+
export declare const bullet: (text: string) => string;
|
|
15
|
+
export declare const todo: (text: string, checked: boolean) => string;
|
|
16
|
+
export declare const image: (alt: string, href: string) => string;
|
|
17
|
+
export declare const addTabSpace: (text: string, n?: number) => string;
|
|
18
|
+
export declare const divider: () => string;
|
|
19
|
+
export declare const tableRowHeader: (row: string[]) => string;
|
|
20
|
+
export declare const tableRowBody: (row: string[]) => string;
|
|
21
|
+
export declare const table: (cells: string[][]) => string;
|
|
22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0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.table = exports.tableRowBody = exports.tableRowHeader = exports.divider = exports.addTabSpace = exports.image = exports.todo = exports.bullet = exports.callout = exports.quote = exports.heading3 = exports.heading2 = exports.heading1 = exports.codeBlock = exports.link = exports.underline = exports.strikethrough = exports.italic = exports.bold = exports.inlineCode = void 0;
|
|
4
|
+
const inlineCode = (text) => {
|
|
5
|
+
return `\`${text}\``;
|
|
6
|
+
};
|
|
7
|
+
exports.inlineCode = inlineCode;
|
|
8
|
+
const bold = (text) => {
|
|
9
|
+
return `**${text}**`;
|
|
10
|
+
};
|
|
11
|
+
exports.bold = bold;
|
|
12
|
+
const italic = (text) => {
|
|
13
|
+
return `_${text}_`;
|
|
14
|
+
};
|
|
15
|
+
exports.italic = italic;
|
|
16
|
+
const strikethrough = (text) => {
|
|
17
|
+
return `~~${text}~~`;
|
|
18
|
+
};
|
|
19
|
+
exports.strikethrough = strikethrough;
|
|
20
|
+
const underline = (text) => {
|
|
21
|
+
return `<u>${text}</u>`;
|
|
22
|
+
};
|
|
23
|
+
exports.underline = underline;
|
|
24
|
+
const link = (text, href) => {
|
|
25
|
+
return `[${text}](${href})`;
|
|
26
|
+
};
|
|
27
|
+
exports.link = link;
|
|
28
|
+
const codeBlock = (text, language) => {
|
|
29
|
+
return `\`\`\`${language}
|
|
30
|
+
${text}
|
|
31
|
+
\`\`\``;
|
|
32
|
+
};
|
|
33
|
+
exports.codeBlock = codeBlock;
|
|
34
|
+
const heading1 = (text) => {
|
|
35
|
+
return `# ${text}`;
|
|
36
|
+
};
|
|
37
|
+
exports.heading1 = heading1;
|
|
38
|
+
const heading2 = (text) => {
|
|
39
|
+
return `## ${text}`;
|
|
40
|
+
};
|
|
41
|
+
exports.heading2 = heading2;
|
|
42
|
+
const heading3 = (text) => {
|
|
43
|
+
return `### ${text}`;
|
|
44
|
+
};
|
|
45
|
+
exports.heading3 = heading3;
|
|
46
|
+
const quote = (text) => {
|
|
47
|
+
// the replace is done to handle multiple lines
|
|
48
|
+
return `> ${text.replace(/\n/g, " \n>")}`;
|
|
49
|
+
};
|
|
50
|
+
exports.quote = quote;
|
|
51
|
+
const callout = (text, icon) => {
|
|
52
|
+
let emoji;
|
|
53
|
+
if ((icon === null || icon === void 0 ? void 0 : icon.type) === 'emoji') {
|
|
54
|
+
emoji = icon.emoji;
|
|
55
|
+
}
|
|
56
|
+
// the replace is done to handle multiple lines
|
|
57
|
+
return `> ${emoji ? emoji + ' ' : ''}${text.replace(/\n/g, " \n>")}`;
|
|
58
|
+
};
|
|
59
|
+
exports.callout = callout;
|
|
60
|
+
const bullet = (text) => {
|
|
61
|
+
return `- ${text}`;
|
|
62
|
+
};
|
|
63
|
+
exports.bullet = bullet;
|
|
64
|
+
const todo = (text, checked) => {
|
|
65
|
+
return checked ? `- [x] ${text}` : `- [ ] ${text}`;
|
|
66
|
+
};
|
|
67
|
+
exports.todo = todo;
|
|
68
|
+
const image = (alt, href) => {
|
|
69
|
+
return ``;
|
|
70
|
+
};
|
|
71
|
+
exports.image = image;
|
|
72
|
+
const addTabSpace = (text, n = 0) => {
|
|
73
|
+
const tab = " ";
|
|
74
|
+
for (let i = 0; i < n; i++)
|
|
75
|
+
text = tab + text;
|
|
76
|
+
return text;
|
|
77
|
+
};
|
|
78
|
+
exports.addTabSpace = addTabSpace;
|
|
79
|
+
const divider = () => {
|
|
80
|
+
return "---";
|
|
81
|
+
};
|
|
82
|
+
exports.divider = divider;
|
|
83
|
+
const tableRowHeader = (row) => {
|
|
84
|
+
let header = row.join("|");
|
|
85
|
+
let divider = row.map((_) => "---").join("|");
|
|
86
|
+
return `${header}\n${divider}`;
|
|
87
|
+
};
|
|
88
|
+
exports.tableRowHeader = tableRowHeader;
|
|
89
|
+
const tableRowBody = (row) => {
|
|
90
|
+
return row.join("|");
|
|
91
|
+
};
|
|
92
|
+
exports.tableRowBody = tableRowBody;
|
|
93
|
+
const table = (cells) => {
|
|
94
|
+
const tableRows = cells.map((row, i) => !i ? (0, exports.tableRowHeader)(row) : (0, exports.tableRowBody)(row));
|
|
95
|
+
return tableRows.join("\n");
|
|
96
|
+
};
|
|
97
|
+
exports.table = table;
|
|
98
|
+
//# sourceMappingURL=md.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"md.js","sourceRoot":"","sources":["../../src/utils/md.ts"],"names":[],"mappings":";;;AAEO,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,EAAE;IACzC,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,UAAU,cAErB;AAEK,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE;IACnC,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEK,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,EAAE;IACrC,OAAO,IAAI,IAAI,GAAG,CAAC;AACrB,CAAC,CAAC;AAFW,QAAA,MAAM,UAEjB;AAEK,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,EAAE;IAC5C,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,aAAa,iBAExB;AAEK,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,EAAE;IACxC,OAAO,MAAM,IAAI,MAAM,CAAC;AAC1B,CAAC,CAAC;AAFW,QAAA,SAAS,aAEpB;AAEK,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,IAAY,EAAE,EAAE;IACjD,OAAO,IAAI,IAAI,KAAK,IAAI,GAAG,CAAC;AAC9B,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEK,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,QAAiB,EAAE,EAAE;IAC3D,OAAO,SAAS,QAAQ;EACxB,IAAI;OACC,CAAC;AACR,CAAC,CAAC;AAJW,QAAA,SAAS,aAIpB;A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AC,GAAG,EAAE,CAAC,EAAE,EAAE,CACrC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,sBAAc,EAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAA,oBAAY,EAAC,GAAG,CAAC,CAC7C,CAAC;IACF,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC9B,CAAC,CAAC;AALW,QAAA,KAAK,SAKhB","sourcesContent":["import { CalloutIcon } from '../types';\n\nexport const inlineCode = (text: string) => {\n return `\\`${text}\\``;\n};\n\nexport const bold = (text: string) => {\n return `**${text}**`;\n};\n\nexport const italic = (text: string) => {\n return `_${text}_`;\n};\n\nexport const strikethrough = (text: string) => {\n return `~~${text}~~`;\n};\n\nexport const underline = (text: string) => {\n return `<u>${text}</u>`;\n};\n\nexport const link = (text: string, href: string) => {\n return `[${text}](${href})`;\n};\n\nexport const codeBlock = (text: string, language?: string) => {\n return `\\`\\`\\`${language}\n${text}\n\\`\\`\\``;\n};\n\nexport const heading1 = (text: string) => {\n return `# ${text}`;\n};\n\nexport const heading2 = (text: string) => {\n return `## ${text}`;\n};\n\nexport const heading3 = (text: string) => {\n return `### ${text}`;\n};\n\nexport const quote = (text: string) => {\n // the replace is done to handle multiple lines\n return `> ${text.replace(/\\n/g, \" \\n>\")}`;\n};\n\nexport const callout = (text: string, icon?: CalloutIcon) => {\n let emoji: string | undefined;\n if (icon?.type === 'emoji') {\n emoji = icon.emoji;\n }\n\n // the replace is done to handle multiple lines\n return `> ${emoji ? emoji + ' ' : ''}${text.replace(/\\n/g, \" \\n>\")}`;\n};\n\nexport const bullet = (text: string) => {\n return `- ${text}`;\n};\n\nexport const todo = (text: string, checked: boolean) => {\n return checked ? `- [x] ${text}` : `- [ ] ${text}`;\n};\n\nexport const image = (alt: string, href: string) => {\n return ``;\n};\n\nexport const addTabSpace = (text: string, n = 0) => {\n const tab = \"\t\";\n for (let i = 0; i < n; i++) text = tab + text;\n return text;\n};\n\nexport const divider = () => {\n return \"---\";\n};\n\nexport const tableRowHeader = (row: string[]) => {\n let header = row.join(\"|\");\n let divider = row.map((_) => \"---\").join(\"|\");\n return `${header}\\n${divider}`;\n};\n\nexport const tableRowBody = (row: string[]) => {\n return row.join(\"|\");\n};\n\nexport const table = (cells: string[][]) => {\n const tableRows = cells.map((row, i) =>\n !i ? tableRowHeader(row) : tableRowBody(row)\n );\n return tableRows.join(\"\\n\");\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notion.d.ts","sourceRoot":"","sources":["../../src/utils/notion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,eAAO,MAAM,gBAAgB,iBACb,MAAM,YACV,MAAM,aACL,MAAM,GAAG,IAAI;;;gBAwBzB,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBlockChildren = void 0;
|
|
4
|
+
const getBlockChildren = async (notionClient, block_id, totalPage) => {
|
|
5
|
+
try {
|
|
6
|
+
let result = [];
|
|
7
|
+
let pageCount = 0;
|
|
8
|
+
let start_cursor = undefined;
|
|
9
|
+
do {
|
|
10
|
+
const response = (await notionClient.blocks.children.list({
|
|
11
|
+
start_cursor: start_cursor,
|
|
12
|
+
block_id: block_id,
|
|
13
|
+
}));
|
|
14
|
+
result.push(...response.results);
|
|
15
|
+
start_cursor = response === null || response === void 0 ? void 0 : response.next_cursor;
|
|
16
|
+
pageCount += 1;
|
|
17
|
+
} while (start_cursor != null &&
|
|
18
|
+
(totalPage == null || pageCount < totalPage));
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
console.log(e);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.getBlockChildren = getBlockChildren;
|
|
26
|
+
//# sourceMappingURL=notion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notion.js","sourceRoot":"","sources":["../../src/utils/notion.ts"],"names":[],"mappings":";;;AAGO,MAAM,gBAAgB,GAAG,KAAK,EACnC,YAAoB,EACpB,QAAgB,EAChB,SAAwB,EACxB,EAAE;IACF,IAAI;QACF,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,IAAI,YAAY,GAAG,SAAS,CAAC;QAE7B,GAAG;YACD,MAAM,QAAQ,GAAG,CAAC,MAAM,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACxD,YAAY,EAAE,YAAY;gBAC1B,QAAQ,EAAE,QAAQ;aACnB,CAAC,CAA8B,CAAC;YACjC,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;YAEjC,YAAY,GAAG,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,WAAW,CAAC;YACrC,SAAS,IAAI,CAAC,CAAC;SAChB,QACC,YAAY,IAAI,IAAI;YACpB,CAAC,SAAS,IAAI,IAAI,IAAI,SAAS,GAAG,SAAS,CAAC,EAC5C;QACF,OAAO,MAAM,CAAC;KACf;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;KAChB;AACH,CAAC,CAAC;AA3BW,QAAA,gBAAgB,oBA2B3B","sourcesContent":["import { Client } from \"@notionhq/client\";\nimport { ListBlockChildrenResponse } from \"@notionhq/client/build/src/api-endpoints\";\n\nexport const getBlockChildren = async (\n notionClient: Client,\n block_id: string,\n totalPage: number | null\n) => {\n try {\n let result = [];\n let pageCount = 0;\n let start_cursor = undefined;\n\n do {\n const response = (await notionClient.blocks.children.list({\n start_cursor: start_cursor,\n block_id: block_id,\n })) as ListBlockChildrenResponse;\n result.push(...response.results);\n\n start_cursor = response?.next_cursor;\n pageCount += 1;\n } while (\n start_cursor != null &&\n (totalPage == null || pageCount < totalPage)\n );\n return result;\n } catch (e) {\n console.log(e);\n }\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "notion-to-md",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "2.3.0",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=12"
|
|
6
|
+
},
|
|
7
|
+
"main": "./build",
|
|
8
|
+
"types": "./build/index.d.ts",
|
|
6
9
|
"scripts": {
|
|
10
|
+
"prepare": "npm run build",
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"prebuild": "npm run clean",
|
|
13
|
+
"clean": "rm -rf ./build",
|
|
7
14
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
15
|
},
|
|
9
16
|
"keywords": [
|
|
@@ -17,6 +24,9 @@
|
|
|
17
24
|
],
|
|
18
25
|
"author": "",
|
|
19
26
|
"license": "ISC",
|
|
27
|
+
"files": [
|
|
28
|
+
"build/**"
|
|
29
|
+
],
|
|
20
30
|
"repository": {
|
|
21
31
|
"type": "git",
|
|
22
32
|
"url": "git+https://github.com/souvikinator/notion-to-md.git"
|
|
@@ -26,8 +36,8 @@
|
|
|
26
36
|
},
|
|
27
37
|
"homepage": "https://github.com/souvikinator/notion-to-md#readme",
|
|
28
38
|
"description": "convert notion pages, block and list of blocks to markdown (supports nesting)",
|
|
29
|
-
"
|
|
30
|
-
"@notionhq/client": "^0.4.
|
|
31
|
-
"
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@notionhq/client": "^0.4.12",
|
|
41
|
+
"typescript": "^4.5.4"
|
|
32
42
|
}
|
|
33
43
|
}
|
package/notion-to-md.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
const md = require("./utils/md");
|
|
2
|
-
const { getBlockChildren } = require("./utils/notion");
|
|
3
|
-
class notion2md {
|
|
4
|
-
constructor(options) {
|
|
5
|
-
if (!options) {
|
|
6
|
-
this.notionClient = undefined;
|
|
7
|
-
} else {
|
|
8
|
-
this.notionClient = options.notionClient || undefined;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
toMarkdownString(mdBlocks = [], nestingLevel = 0) {
|
|
13
|
-
let mdString = "";
|
|
14
|
-
mdBlocks.forEach((mdBlocks) => {
|
|
15
|
-
if (mdBlocks.parent) {
|
|
16
|
-
mdString += `
|
|
17
|
-
${md.addTabSpace(mdBlocks.parent, nestingLevel)}
|
|
18
|
-
`;
|
|
19
|
-
}
|
|
20
|
-
if (mdBlocks.children && mdBlocks.children.length > 0) {
|
|
21
|
-
mdString += this.toMarkdownString(mdBlocks.children, nestingLevel + 1);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return mdString;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
|
|
29
|
-
*/
|
|
30
|
-
async pageToMarkdown(id, totalPage = 1) {
|
|
31
|
-
if (!id) throw new Error("pageToMarkdown takes page_id as argument");
|
|
32
|
-
const blocks = await getBlockChildren(this.notionClient, id, totalPage);
|
|
33
|
-
const parsedData = await this.blocksToMarkdown(blocks);
|
|
34
|
-
return parsedData;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @param {object} blocks - list of notion blocks
|
|
39
|
-
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
|
|
40
|
-
* @returns array of md blocks with their children
|
|
41
|
-
*/
|
|
42
|
-
async blocksToMarkdown(blocks, mdBlocks = [], totalPage = 1) {
|
|
43
|
-
if (!this.notionClient) {
|
|
44
|
-
throw new Error(
|
|
45
|
-
"notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md"
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
for (let i = 0; i < blocks.length; i++) {
|
|
50
|
-
let block = blocks[i];
|
|
51
|
-
if (block.has_children) {
|
|
52
|
-
let child_blocks = await getBlockChildren(
|
|
53
|
-
this.notionClient,
|
|
54
|
-
block.id,
|
|
55
|
-
totalPage
|
|
56
|
-
);
|
|
57
|
-
mdBlocks.push({ parent: this.blockToMarkdown(block), children: [] });
|
|
58
|
-
let l = mdBlocks.length;
|
|
59
|
-
await this.blocksToMarkdown(child_blocks, mdBlocks[l - 1].children);
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
let tmp = this.blockToMarkdown(block);
|
|
63
|
-
|
|
64
|
-
mdBlocks.push({ parent: tmp, children: [] });
|
|
65
|
-
}
|
|
66
|
-
return mdBlocks;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @param {object} block - single notion block
|
|
71
|
-
* @returns markdown form of the passed block
|
|
72
|
-
*/
|
|
73
|
-
blockToMarkdown(block) {
|
|
74
|
-
if (!block) throw new Error("notion block required");
|
|
75
|
-
|
|
76
|
-
let parsedData = "",
|
|
77
|
-
blockContent;
|
|
78
|
-
const { type } = block;
|
|
79
|
-
if (type === "image") {
|
|
80
|
-
blockContent = block[type];
|
|
81
|
-
const image_type = blockContent["type"];
|
|
82
|
-
|
|
83
|
-
return md.image("image", blockContent[image_type].url);
|
|
84
|
-
} else if (type === "divider") {
|
|
85
|
-
return md.divider();
|
|
86
|
-
} else if (type === "equation") {
|
|
87
|
-
return md.codeBlock(block[type].expression);
|
|
88
|
-
} else if (type === "video" || type === "file" || type === "pdf") {
|
|
89
|
-
blockContent = block[type];
|
|
90
|
-
const urlType = blockContent["type"];
|
|
91
|
-
|
|
92
|
-
return md.link(type, blockContent[urlType].url);
|
|
93
|
-
} else if (
|
|
94
|
-
type === "bookmark" ||
|
|
95
|
-
type === "embed" ||
|
|
96
|
-
type === "link_preview"
|
|
97
|
-
) {
|
|
98
|
-
blockContent = block[type];
|
|
99
|
-
|
|
100
|
-
return md.link(type, blockContent.url);
|
|
101
|
-
} else {
|
|
102
|
-
blockContent = block[type]["text"] || [];
|
|
103
|
-
blockContent.map((content) => {
|
|
104
|
-
const annotations = content.annotations;
|
|
105
|
-
let plain_text = content.plain_text;
|
|
106
|
-
|
|
107
|
-
plain_text = this.annotatePlainText(plain_text, annotations);
|
|
108
|
-
|
|
109
|
-
if (content["href"]) plain_text = md.link(plain_text, content["href"]);
|
|
110
|
-
|
|
111
|
-
parsedData += plain_text;
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (type === "code")
|
|
116
|
-
parsedData = md.codeBlock(parsedData, block[type].language);
|
|
117
|
-
if (type === "heading_1") parsedData = md.heading1(parsedData);
|
|
118
|
-
if (type === "heading_2") parsedData = md.heading2(parsedData);
|
|
119
|
-
if (type === "heading_3") parsedData = md.heading3(parsedData);
|
|
120
|
-
if (type === "quote") parsedData = md.quote(parsedData);
|
|
121
|
-
|
|
122
|
-
if (type === "numbered_list_item" || type === "bulleted_list_item")
|
|
123
|
-
parsedData = md.bullet(parsedData);
|
|
124
|
-
|
|
125
|
-
if (type === "to_do") parsedData = md.todo(parsedData, block[type].checked);
|
|
126
|
-
|
|
127
|
-
return parsedData;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @param {string} text string to be annotated
|
|
132
|
-
* @param {object} annotations annotation object of a notion block
|
|
133
|
-
*/
|
|
134
|
-
annotatePlainText(text, annotations) {
|
|
135
|
-
const leading_space = text.match(/^\s*/)[0];
|
|
136
|
-
const trailing_space = text.match(/\s*$/)[0];
|
|
137
|
-
text = text.trim();
|
|
138
|
-
|
|
139
|
-
if (text !== "") {
|
|
140
|
-
if (annotations.code) text = md.inlineCode(text);
|
|
141
|
-
if (annotations.bold) text = md.bold(text);
|
|
142
|
-
if (annotations.italic) text = md.italic(text);
|
|
143
|
-
if (annotations.strikethrough) text = md.strikethrough(text);
|
|
144
|
-
if (annotations.underline) text = md.underline(text);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return leading_space + text + trailing_space;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
module.exports = notion2md;
|
package/utils/md.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
exports.inlineCode = (text) => {
|
|
2
|
-
return `\`${text}\``;
|
|
3
|
-
};
|
|
4
|
-
|
|
5
|
-
exports.bold = (text) => {
|
|
6
|
-
return `**${text}**`;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
exports.italic = (text) => {
|
|
10
|
-
return `_${text}_`;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
exports.strikethrough = (text) => {
|
|
14
|
-
return `~~${text}~~`;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
exports.underline = (text) => {
|
|
18
|
-
return `<u>${text}</u>`;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
exports.link = (text, href) => {
|
|
22
|
-
return `[${text}](${href})`;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
exports.codeBlock = (text, language) => {
|
|
26
|
-
return `\`\`\`${language}
|
|
27
|
-
${text}
|
|
28
|
-
\`\`\``;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
exports.heading1 = (text) => {
|
|
32
|
-
return `# ${text}`;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
exports.heading2 = (text) => {
|
|
36
|
-
return `## ${text}`;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
exports.heading3 = (text) => {
|
|
40
|
-
return `### ${text}`;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
exports.quote = (text) => {
|
|
44
|
-
// the replace is done to handle multiple lines
|
|
45
|
-
return `> ${text.replace(/\n/g, " \n>")}`;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
exports.bullet = (text) => {
|
|
49
|
-
return `- ${text}`;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
exports.todo = (text, checked) => {
|
|
53
|
-
return checked ? `- [x] ${text}` : `- [ ] ${text}`;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
exports.image = (alt, href) => {
|
|
57
|
-
return ``;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
exports.addTabSpace = (text, n = 0) => {
|
|
61
|
-
const tab = " ";
|
|
62
|
-
for (i = 0; i < n; i++) text = tab + text;
|
|
63
|
-
return text;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
exports.divider = () => {
|
|
67
|
-
return "---";
|
|
68
|
-
};
|
package/utils/notion.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
exports.getBlockChildren = async (notionClient, block_id, totalPage) => {
|
|
2
|
-
try {
|
|
3
|
-
if (!notionClient) {
|
|
4
|
-
throw new Error(
|
|
5
|
-
"notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md"
|
|
6
|
-
);
|
|
7
|
-
}
|
|
8
|
-
let result = [];
|
|
9
|
-
let start_cursor;
|
|
10
|
-
let pageSize = 100;
|
|
11
|
-
for (i = 0; i < totalPage; i++) {
|
|
12
|
-
// contain start_cursor
|
|
13
|
-
const response = await notionClient.blocks.children.list({
|
|
14
|
-
start_cursor: start_cursor,
|
|
15
|
-
page_size: pageSize,
|
|
16
|
-
block_id: block_id,
|
|
17
|
-
});
|
|
18
|
-
let current = response.results;
|
|
19
|
-
// delete start_cursor
|
|
20
|
-
if (start_cursor) {
|
|
21
|
-
current.shift();
|
|
22
|
-
}
|
|
23
|
-
result.push(...current);
|
|
24
|
-
if (current.length < 100) {
|
|
25
|
-
break;
|
|
26
|
-
}
|
|
27
|
-
pageSize = 101;
|
|
28
|
-
start_cursor = current[current.length - 1].id;
|
|
29
|
-
}
|
|
30
|
-
return result;
|
|
31
|
-
} catch (e) {
|
|
32
|
-
console.log(e);
|
|
33
|
-
}
|
|
34
|
-
};
|