notion-to-md 2.0.0 → 2.1.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 +37 -39
- 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 +255 -0
- package/build/notion-to-md.js.map +1 -0
- package/build/types/index.d.ts +33 -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 +17 -0
- package/build/utils/md.d.ts.map +1 -0
- package/build/utils/md.js +74 -0
- package/build/utils/md.js.map +1 -0
- package/build/utils/notion.d.ts +3029 -0
- package/build/utils/notion.d.ts.map +1 -0
- package/build/utils/notion.js +35 -0
- package/build/utils/notion.js.map +1 -0
- package/package.json +15 -5
- package/notion-to-md.js +0 -151
- package/utils/md.js +0 -68
- package/utils/notion.js +0 -34
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ Convert notion pages, block and list of blocks to markdown (supports nesting) us
|
|
|
24
24
|
- [x] divider
|
|
25
25
|
- [x] equation block (converted to code blocks)
|
|
26
26
|
- [x] convert returned markdown object to string (`toMarkdownString()`)
|
|
27
|
+
- [x] typescript support
|
|
27
28
|
- [ ] add tests
|
|
28
29
|
|
|
29
30
|
## Install
|
|
@@ -34,6 +35,8 @@ $ npm install notion-to-md
|
|
|
34
35
|
|
|
35
36
|
## Usage
|
|
36
37
|
|
|
38
|
+
> **Note:** Details on methods can be found in [API section](https://github.com/souvikinator/notion-to-md#api)
|
|
39
|
+
|
|
37
40
|
### converting markdown objects to markdown string
|
|
38
41
|
|
|
39
42
|
This is how the notion page looks for this example:
|
|
@@ -42,14 +45,14 @@ This is how the notion page looks for this example:
|
|
|
42
45
|
|
|
43
46
|
```js
|
|
44
47
|
const { Client } = require("@notionhq/client");
|
|
45
|
-
const
|
|
48
|
+
const { NotionToMarkdown } = require("notion-to-md");
|
|
46
49
|
|
|
47
50
|
const notion = new Client({
|
|
48
51
|
auth: "your integration token",
|
|
49
52
|
});
|
|
50
53
|
|
|
51
54
|
// passing notion client to the option
|
|
52
|
-
const n2m = new
|
|
55
|
+
const n2m = new NotionToMarkdown({ notionClient: notion });
|
|
53
56
|
|
|
54
57
|
(async () => {
|
|
55
58
|
const mdblocks = await n2m.pageToMarkdown("target_page_id");
|
|
@@ -66,24 +69,22 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
66
69
|
|
|
67
70
|

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

|
|
76
77
|
|
|
77
78
|
```js
|
|
78
79
|
const { Client } = require("@notionhq/client");
|
|
79
|
-
const
|
|
80
|
+
const { NotionToMarkdown } = require("notion-to-md");
|
|
80
81
|
|
|
81
82
|
const notion = new Client({
|
|
82
83
|
auth: "your integration token",
|
|
83
84
|
});
|
|
84
85
|
|
|
85
86
|
// passing notion client to the option
|
|
86
|
-
const n2m = new
|
|
87
|
+
const n2m = new NotionToMarkdown({ notionClient: notion });
|
|
87
88
|
|
|
88
89
|
(async () => {
|
|
89
90
|
// notice second argument, totalPage.
|
|
@@ -92,11 +93,6 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
92
93
|
})();
|
|
93
94
|
```
|
|
94
95
|
|
|
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
96
|
**Output:**
|
|
101
97
|
|
|
102
98
|
```json
|
|
@@ -144,8 +140,6 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
144
140
|
|
|
145
141
|
### converting list of blocks to markdown object
|
|
146
142
|
|
|
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
143
|
same notion page as before
|
|
150
144
|
|
|
151
145
|
```js
|
|
@@ -177,7 +171,7 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
177
171
|
|
|
178
172
|
- only takes a single notion block and returns corresponding markdown string
|
|
179
173
|
- nesting is ignored
|
|
180
|
-
-
|
|
174
|
+
- independent of @notionhq/client
|
|
181
175
|
|
|
182
176
|
```js
|
|
183
177
|
const notion2md = require("notion-to-md");
|
|
@@ -197,37 +191,41 @@ console.log(result);
|
|
|
197
191
|
|
|
198
192
|
## API
|
|
199
193
|
|
|
200
|
-
### `toMarkdownString(
|
|
201
|
-
|
|
202
|
-
- takes output of `pageToMarkdown` or `blocksToMarkdown` as argument
|
|
203
|
-
-
|
|
204
|
-
|
|
205
|
-
### `pageToMarkdown(page_id,totalPage)`
|
|
194
|
+
> ### `toMarkdownString(mdBlock[])`
|
|
195
|
+
>
|
|
196
|
+
> - takes output of `pageToMarkdown` or `blocksToMarkdown` as argument
|
|
197
|
+
> - converts to markdown string.
|
|
206
198
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
- `
|
|
199
|
+
> ### `pageToMarkdown(id,totalPage)`
|
|
200
|
+
>
|
|
201
|
+
> - Uses `blocksToMarkdown` internally.
|
|
202
|
+
> - `id`(pageid) as input and converts all the blocks in the page to corresponding markdown object
|
|
203
|
+
> - `totalPage` is the retrieve block children request number i.e `page_size Maximum = totalPage * 100`.
|
|
210
204
|
|
|
211
|
-
>
|
|
205
|
+
> ### totalPage
|
|
206
|
+
>
|
|
207
|
+
> 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)).
|
|
208
|
+
>
|
|
209
|
+
> ### How to use `totalPage` arg ?
|
|
212
210
|
>
|
|
213
211
|
> - 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
|
|
215
|
-
|
|
216
|
-
### `blocksToMarkdown(blocks,totalPage)`
|
|
212
|
+
> - 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.
|
|
217
213
|
|
|
214
|
+
> ### `blocksToMarkdown(blocks,totalPage)`
|
|
215
|
+
>
|
|
218
216
|
> **Note**: requires <u>**notion-sdk-js**</u> unlike `blockToMarkdown`
|
|
217
|
+
>
|
|
218
|
+
> - `blocks`: array of notion blocks
|
|
219
|
+
> - `totalPage`: the retrieve block children request number i.e `page_size Maximum = totalPage * 100`.
|
|
220
|
+
> - deals with <u>**nested blocks**</u>
|
|
221
|
+
> - uses `blockToMarkdown` internally.
|
|
219
222
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
-
|
|
223
|
-
-
|
|
224
|
-
|
|
225
|
-
|
|
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)
|
|
223
|
+
> ### `blockToMarkdown(block)`
|
|
224
|
+
>
|
|
225
|
+
> - Takes single notion block and converts to markdown string
|
|
226
|
+
> - does not deal with nested notion blocks
|
|
227
|
+
> - This method doesn't require the `notion-sdk-js`.
|
|
228
|
+
> - Refer docs to know more about [notion blocks](https://developers.notion.com/reference/block)
|
|
231
229
|
|
|
232
230
|
## Contribution
|
|
233
231
|
|
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=1)
|
|
19
|
+
* @returns {Promise<MdBlock[]>} - List of markdown blocks
|
|
20
|
+
*/
|
|
21
|
+
pageToMarkdown(id: string, totalPage?: number): 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, 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): 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,CAAC,CAAS;gBAElB,OAAO,CAAC,EAAE,uBAAuB;IAI7C;;;;;OAKG;IACH,gBAAgB,CAAC,QAAQ,GAAE,OAAO,EAAO,EAAE,YAAY,GAAE,MAAU,GAAG,MAAM;IAe5E;;;;;OAKG;IACG,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,GAAE,MAAU,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAY3E;;;;;;OAMG;IACG,gBAAgB,CACpB,MAAM,CAAC,EAAE,gCAAgC,EACzC,SAAS,GAAE,MAAU,EACrB,QAAQ,GAAE,OAAO,EAAO,GACvB,OAAO,CAAC,OAAO,EAAE,CAAC;IAkCrB;;;;OAIG;IACH,eAAe,CAAC,KAAK,EAAE,+BAA+B,GAAG,MAAM;IA8H/D;;;;;OAKG;IACH,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,MAAM;CAsBlE"}
|
|
@@ -0,0 +1,255 @@
|
|
|
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 ? options.notionClient : undefined;
|
|
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=1)
|
|
56
|
+
* @returns {Promise<MdBlock[]>} - List of markdown blocks
|
|
57
|
+
*/
|
|
58
|
+
async pageToMarkdown(id, totalPage = 1) {
|
|
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
|
+
if (!id)
|
|
63
|
+
throw new Error("pageToMarkdown takes page_id as argument");
|
|
64
|
+
const blocks = await (0, notion_1.getBlockChildren)(this.notionClient, id, totalPage);
|
|
65
|
+
const parsedData = await this.blocksToMarkdown(blocks);
|
|
66
|
+
return parsedData;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Converts list of Notion Blocks to Markdown Blocks
|
|
70
|
+
* @param {ListBlockChildrenResponseResults | undefined} blocks - List of notion blocks
|
|
71
|
+
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
|
|
72
|
+
* @param {MdBlock[]} mdBlocks - Defines max depth of nesting
|
|
73
|
+
* @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children
|
|
74
|
+
*/
|
|
75
|
+
async blocksToMarkdown(blocks, totalPage = 1, mdBlocks = []) {
|
|
76
|
+
if (!this.notionClient) {
|
|
77
|
+
throw new Error("notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md");
|
|
78
|
+
}
|
|
79
|
+
if (!blocks)
|
|
80
|
+
return mdBlocks;
|
|
81
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
82
|
+
let block = blocks[i];
|
|
83
|
+
if ("has_children" in block && block.has_children) {
|
|
84
|
+
let child_blocks = await (0, notion_1.getBlockChildren)(this.notionClient, block.id, totalPage);
|
|
85
|
+
mdBlocks.push({ parent: this.blockToMarkdown(block), children: [] });
|
|
86
|
+
let l = mdBlocks.length;
|
|
87
|
+
await this.blocksToMarkdown(child_blocks, totalPage, mdBlocks[l - 1].children);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
let tmp = this.blockToMarkdown(block);
|
|
91
|
+
mdBlocks.push({ parent: tmp, children: [] });
|
|
92
|
+
}
|
|
93
|
+
return mdBlocks;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Converts a Notion Block to a Markdown Block
|
|
97
|
+
* @param {ListBlockChildrenResponseResult} block - single notion block
|
|
98
|
+
* @returns {string} corresponding markdown string of the passed block
|
|
99
|
+
*/
|
|
100
|
+
blockToMarkdown(block) {
|
|
101
|
+
if (!("type" in block))
|
|
102
|
+
return "";
|
|
103
|
+
let parsedData = "";
|
|
104
|
+
const { type } = block;
|
|
105
|
+
switch (type) {
|
|
106
|
+
case "image": {
|
|
107
|
+
let blockContent = block.image;
|
|
108
|
+
const image_type = blockContent.type;
|
|
109
|
+
if (image_type === "external")
|
|
110
|
+
return md.image("image", blockContent.external.url);
|
|
111
|
+
if (image_type === "file")
|
|
112
|
+
return md.image("image", blockContent.file.url);
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
case "divider": {
|
|
116
|
+
return md.divider();
|
|
117
|
+
}
|
|
118
|
+
case "equation": {
|
|
119
|
+
return md.codeBlock(block.equation.expression);
|
|
120
|
+
}
|
|
121
|
+
case "video":
|
|
122
|
+
case "file":
|
|
123
|
+
case "pdf": {
|
|
124
|
+
let blockContent;
|
|
125
|
+
if (type === "video")
|
|
126
|
+
blockContent = block.video;
|
|
127
|
+
if (type === "file")
|
|
128
|
+
blockContent = block.file;
|
|
129
|
+
if (type === "pdf")
|
|
130
|
+
blockContent = block.pdf;
|
|
131
|
+
if (blockContent) {
|
|
132
|
+
const file_type = blockContent.type;
|
|
133
|
+
if (file_type === "external")
|
|
134
|
+
return md.link("image", blockContent.external.url);
|
|
135
|
+
if (file_type === "file")
|
|
136
|
+
return md.link("image", blockContent.file.url);
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case "bookmark":
|
|
141
|
+
case "embed":
|
|
142
|
+
case "link_preview": {
|
|
143
|
+
let blockContent;
|
|
144
|
+
if (type === "bookmark")
|
|
145
|
+
blockContent = block.bookmark;
|
|
146
|
+
if (type === "embed")
|
|
147
|
+
blockContent = block.embed;
|
|
148
|
+
if (type === "link_preview")
|
|
149
|
+
blockContent = block.link_preview;
|
|
150
|
+
if (blockContent)
|
|
151
|
+
return md.link(type, blockContent.url);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
// Rest of the types
|
|
155
|
+
// "paragraph"
|
|
156
|
+
// "heading_1"
|
|
157
|
+
// "heading_2"
|
|
158
|
+
// "heading_3"
|
|
159
|
+
// "bulleted_list_item"
|
|
160
|
+
// "numbered_list_item"
|
|
161
|
+
// "quote"
|
|
162
|
+
// "to_do"
|
|
163
|
+
// "toggle"
|
|
164
|
+
// "template"
|
|
165
|
+
// "synced_block"
|
|
166
|
+
// "child_page"
|
|
167
|
+
// "child_database"
|
|
168
|
+
// "code"
|
|
169
|
+
// "callout"
|
|
170
|
+
// "breadcrumb"
|
|
171
|
+
// "table_of_contents"
|
|
172
|
+
// "column_list"
|
|
173
|
+
// "column"
|
|
174
|
+
// "link_to_page"
|
|
175
|
+
// "audio"
|
|
176
|
+
// "unsupported"
|
|
177
|
+
default: {
|
|
178
|
+
// In this case typescript is not able to index the types properly, hence ignoring the error
|
|
179
|
+
// @ts-ignore
|
|
180
|
+
let blockContent = block[type].text || [];
|
|
181
|
+
blockContent.map((content) => {
|
|
182
|
+
const annotations = content.annotations;
|
|
183
|
+
let plain_text = content.plain_text;
|
|
184
|
+
plain_text = this.annotatePlainText(plain_text, annotations);
|
|
185
|
+
if (content["href"])
|
|
186
|
+
plain_text = md.link(plain_text, content["href"]);
|
|
187
|
+
parsedData += plain_text;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
switch (type) {
|
|
192
|
+
case "code": {
|
|
193
|
+
parsedData = md.codeBlock(parsedData, block[type].language);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case "heading_1": {
|
|
197
|
+
parsedData = md.heading1(parsedData);
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case "heading_2": {
|
|
201
|
+
parsedData = md.heading2(parsedData);
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "heading_3": {
|
|
205
|
+
parsedData = md.heading3(parsedData);
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
case "quote": {
|
|
209
|
+
parsedData = md.quote(parsedData);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
case "bulleted_list_item":
|
|
213
|
+
case "numbered_list_item": {
|
|
214
|
+
parsedData = md.bullet(parsedData);
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case "to_do": {
|
|
218
|
+
parsedData = md.todo(parsedData, block.to_do.checked);
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return parsedData;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Annoate text using provided annotations
|
|
226
|
+
* @param {string} text - String to be annotated
|
|
227
|
+
* @param {Annotations} annotations - Annotation object of a notion block
|
|
228
|
+
* @returns {string} - Annotated text
|
|
229
|
+
*/
|
|
230
|
+
annotatePlainText(text, annotations) {
|
|
231
|
+
// if text is all spaces, don't annotate
|
|
232
|
+
if (text.match(/^\s*$/))
|
|
233
|
+
return text;
|
|
234
|
+
const leadingSpaceMatch = text.match(/^(\s*)/);
|
|
235
|
+
const trailingSpaceMatch = text.match(/(\s*)$/);
|
|
236
|
+
const leading_space = leadingSpaceMatch ? leadingSpaceMatch[0] : "";
|
|
237
|
+
const trailing_space = trailingSpaceMatch ? trailingSpaceMatch[0] : "";
|
|
238
|
+
text = text.trim();
|
|
239
|
+
if (text !== "") {
|
|
240
|
+
if (annotations.code)
|
|
241
|
+
text = md.inlineCode(text);
|
|
242
|
+
if (annotations.bold)
|
|
243
|
+
text = md.bold(text);
|
|
244
|
+
if (annotations.italic)
|
|
245
|
+
text = md.italic(text);
|
|
246
|
+
if (annotations.strikethrough)
|
|
247
|
+
text = md.strikethrough(text);
|
|
248
|
+
if (annotations.underline)
|
|
249
|
+
text = md.underline(text);
|
|
250
|
+
}
|
|
251
|
+
return leading_space + text + trailing_space;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
exports.NotionToMarkdown = NotionToMarkdown;
|
|
255
|
+
//# 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,OAAiC;QAC3C,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC;IACjE,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;CAC9C,CAAC;aACK;YACD,IAAI,QAAQ,CAAC,QAAQ,IAAI,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;gBACrD,QAAQ,IAAI,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,QAAQ,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC;aACxE;QACH,CAAC,CAAC,CAAC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AI,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;AA1PD,4CA0PC","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 ? options.notionClient : undefined;\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=1)\n * @returns {Promise<MdBlock[]>} - List of markdown blocks\n */\n async pageToMarkdown(id: string, totalPage: number = 1): 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 if (!id) throw new Error(\"pageToMarkdown takes page_id as argument\");\n const blocks = await getBlockChildren(this.notionClient, id, totalPage);\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 = 1,\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 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({ parent: this.blockToMarkdown(block), children: [] });\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 = 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 blockToMarkdown(block: ListBlockChildrenResponseResult): string {\n if (!(\"type\" in block)) return \"\";\n\n let parsedData = \"\";\n const { type } = block;\n\n switch (type) {\n case \"image\": {\n let blockContent = block.image;\n const image_type = blockContent.type;\n if (image_type === \"external\")\n return md.image(\"image\", blockContent.external.url);\n if (image_type === \"file\")\n return md.image(\"image\", blockContent.file.url);\n break;\n }\n case \"divider\": {\n return md.divider();\n }\n case \"equation\": {\n return md.codeBlock(block.equation.expression);\n }\n case \"video\":\n case \"file\":\n case \"pdf\": {\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 break;\n }\n case \"bookmark\":\n case \"embed\":\n case \"link_preview\": {\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 break;\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 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 parsedData = md.codeBlock(parsedData, block[type].language);\n break;\n }\n case \"heading_1\": {\n parsedData = md.heading1(parsedData);\n break;\n }\n case \"heading_2\": {\n parsedData = md.heading2(parsedData);\n break;\n }\n case \"heading_3\": {\n parsedData = md.heading3(parsedData);\n break;\n }\n case \"quote\": {\n parsedData = md.quote(parsedData);\n break;\n }\n case \"bulleted_list_item\":\n case \"numbered_list_item\": {\n parsedData = md.bullet(parsedData);\n break;\n }\n case \"to_do\": {\n parsedData = md.todo(parsedData, block.to_do.checked);\n break;\n }\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,33 @@
|
|
|
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
|
+
//# 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"}
|
|
@@ -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"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const inlineCode: (text: string) => string;
|
|
2
|
+
export declare const bold: (text: string) => string;
|
|
3
|
+
export declare const italic: (text: string) => string;
|
|
4
|
+
export declare const strikethrough: (text: string) => string;
|
|
5
|
+
export declare const underline: (text: string) => string;
|
|
6
|
+
export declare const link: (text: string, href: string) => string;
|
|
7
|
+
export declare const codeBlock: (text: string, language?: string | undefined) => string;
|
|
8
|
+
export declare const heading1: (text: string) => string;
|
|
9
|
+
export declare const heading2: (text: string) => string;
|
|
10
|
+
export declare const heading3: (text: string) => string;
|
|
11
|
+
export declare const quote: (text: string) => string;
|
|
12
|
+
export declare const bullet: (text: string) => string;
|
|
13
|
+
export declare const todo: (text: string, checked: boolean) => string;
|
|
14
|
+
export declare const image: (alt: string, href: string) => string;
|
|
15
|
+
export declare const addTabSpace: (text: string, n?: number) => string;
|
|
16
|
+
export declare const divider: () => string;
|
|
17
|
+
//# sourceMappingURL=md.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"md.d.ts","sourceRoot":"","sources":["../../src/utils/md.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,SAAU,MAAM,WAEtC,CAAC;AAEF,eAAO,MAAM,IAAI,SAAU,MAAM,WAEhC,CAAC;AAEF,eAAO,MAAM,MAAM,SAAU,MAAM,WAElC,CAAC;AAEF,eAAO,MAAM,aAAa,SAAU,MAAM,WAEzC,CAAC;AAEF,eAAO,MAAM,SAAS,SAAU,MAAM,WAErC,CAAC;AAEF,eAAO,MAAM,IAAI,SAAU,MAAM,QAAQ,MAAM,WAE9C,CAAC;AAEF,eAAO,MAAM,SAAS,SAAU,MAAM,0CAIrC,CAAC;AAEF,eAAO,MAAM,QAAQ,SAAU,MAAM,WAEpC,CAAC;AAEF,eAAO,MAAM,QAAQ,SAAU,MAAM,WAEpC,CAAC;AAEF,eAAO,MAAM,QAAQ,SAAU,MAAM,WAEpC,CAAC;AAEF,eAAO,MAAM,KAAK,SAAU,MAAM,WAGjC,CAAC;AAEF,eAAO,MAAM,MAAM,SAAU,MAAM,WAElC,CAAC;AAEF,eAAO,MAAM,IAAI,SAAU,MAAM,WAAW,OAAO,WAElD,CAAC;AAEF,eAAO,MAAM,KAAK,QAAS,MAAM,QAAQ,MAAM,WAE9C,CAAC;AAEF,eAAO,MAAM,WAAW,SAAU,MAAM,uBAIvC,CAAC;AAEF,eAAO,MAAM,OAAO,cAEnB,CAAC"}
|