notion-to-md 2.5.5 → 2.6.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Souvik Kar Mahapatra
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Souvik Kar Mahapatra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,234 +1,309 @@
1
- <img src="https://imgur.com/WgXdz9r.png" />
2
-
3
- > 💡 For better readability and detailed instructions headover to the [wiki](https://github.com/souvikinator/notion-to-md/wiki)
4
-
5
- # Notion to Markdown
6
-
7
- Convert notion pages, block and list of blocks to markdown (supports nesting) using **[notion-sdk-js](https://github.com/makenotion/notion-sdk-js)**
8
-
9
- > **Note:** Before getting started, create [an integration and find the token](https://www.notion.so/my-integrations).
10
-
11
- <a href="https://www.producthunt.com/posts/notion-to-md?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-notion&#0045;to&#0045;md" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=351669&theme=light" alt="notion&#0045;to&#0045;md - Programmatically&#0032;convert&#0032;notion&#0032;pages&#0032;to&#0032;markdown | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a>
12
-
13
- [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/O5O1AFCJR)
14
-
15
- ## Todo
16
-
17
- - [x] heading
18
- - [x] images
19
- - [x] quotes
20
- - [x] links
21
- - [x] bullets
22
- - [x] todo
23
- - [x] inline code
24
- - [x] code block
25
- - [x] strikethrough, underline, bold, italic
26
- - [x] nested blocks
27
- - [x] embeds, bookmarks, videos, files (converted to links)
28
- - [x] Simple tables
29
- - [x] toggle
30
- - [x] divider
31
- - [x] equation block (converted to code blocks)
32
- - [x] convert returned markdown object to string (`toMarkdownString()`)
33
- - [x] typescript support
34
- - [x] add tests
35
-
36
- ## Install
37
-
38
- ```Bash
39
- $ npm install notion-to-md
40
- ```
41
-
42
- ## Usage
43
-
44
- > **Note:** Details on methods can be found in [API section](https://github.com/souvikinator/notion-to-md#api)
45
-
46
- ### converting markdown objects to markdown string
47
-
48
- This is how the notion page looks for this example:
49
-
50
- ![Imgur](https://imgur.com/O6bKCmH.png)
51
-
52
- ```js
53
- const { Client } = require("@notionhq/client");
54
- const { NotionToMarkdown } = require("notion-to-md");
55
- // or
56
- // import {NotionToMarkdown} from "notion-to-md";
57
-
58
- const notion = new Client({
59
- auth: "your integration token",
60
- });
61
-
62
- // passing notion client to the option
63
- const n2m = new NotionToMarkdown({ notionClient: notion });
64
-
65
- (async () => {
66
- const mdblocks = await n2m.pageToMarkdown("target_page_id");
67
- const mdString = n2m.toMarkdownString(mdblocks);
68
-
69
- //writing to file
70
- fs.writeFile("test.md", mdString, (err) => {
71
- console.log(err);
72
- });
73
- })();
74
- ```
75
-
76
- **Output:**
77
-
78
- ![output](https://imgur.com/XrUYrZ0.png)
79
-
80
- ### converting page to markdown object
81
-
82
- Example notion page:
83
-
84
- ![Imgur](https://imgur.com/9iqRpBl.png)
85
-
86
- ```js
87
- const { Client } = require("@notionhq/client");
88
- const { NotionToMarkdown } = require("notion-to-md");
89
-
90
- const notion = new Client({
91
- auth: "your integration token",
92
- });
93
-
94
- // passing notion client to the option
95
- const n2m = new NotionToMarkdown({ notionClient: notion });
96
-
97
- (async () => {
98
- // notice second argument, totalPage.
99
- const x = await n2m.pageToMarkdown("target_page_id", 2);
100
- console.log(x);
101
- })();
102
- ```
103
-
104
- **Output:**
105
-
106
- ```json
107
- [
108
- {
109
- "parent": "# heading 1",
110
- "children": []
111
- },
112
- {
113
- "parent": "- bullet 1",
114
- "children": [
115
- {
116
- "parent": "- bullet 1.1",
117
- "children": []
118
- },
119
- {
120
- "parent": "- bullet 1.2",
121
- "children": []
122
- }
123
- ]
124
- },
125
- {
126
- "parent": "- bullet 2",
127
- "children": []
128
- },
129
- {
130
- "parent": "- [ ] check box 1",
131
- "children": [
132
- {
133
- "parent": "- [x] check box 1.2",
134
- "children": []
135
- },
136
- {
137
- "parent": "- [ ] check box 1.3",
138
- "children": []
139
- }
140
- ]
141
- },
142
- {
143
- "parent": "- [ ] checkbox 2",
144
- "children": []
145
- }
146
- ]
147
- ```
148
-
149
- ### converting list of blocks to markdown object
150
-
151
- same notion page as before
152
-
153
- ```js
154
- const { Client } = require("@notionhq/client");
155
- const { NotionToMarkdown } = require("notion-to-md");
156
-
157
- const notion = new Client({
158
- auth: "your integration token",
159
- });
160
-
161
- // passing notion client to the option
162
- const n2m = new NotionToMarkdown({ notionClient: notion });
163
-
164
- (async () => {
165
- // get all blocks in the page
166
- const { results } = await notion.blocks.children.list({
167
- block_id,
168
- });
169
-
170
- //convert to markdown
171
- const x = await n2m.blocksToMarkdown(results);
172
- console.log(x);
173
- })();
174
- ```
175
-
176
- **Output**: same as before
177
-
178
- ### Converting a single block to markdown string
179
-
180
- - only takes a single notion block and returns corresponding markdown string
181
- - nesting is ignored
182
- - depends on @notionhq/client
183
-
184
- ```js
185
- const { NotionToMarkdown } = require("notion-to-md");
186
-
187
- // passing notion client to the option
188
- const n2m = new NotionToMarkdown({ notionClient: notion });
189
-
190
- const result = n2m.blockToMarkdown(block);
191
- console.log(result);
192
- ```
193
-
194
- **result**:
195
-
196
- ```
197
- ![image](https://media.giphy.com/media/Ju7l5y9osyymQ/giphy.gif)
198
- ```
199
-
200
- ## Custom Transformers
201
- You can define your own custom transformer for a notion type, to parse and return your own string.
202
- `setCustomTransformer(type, func)` will overload the parsing for the giving type.
203
-
204
- ```js
205
- const { NotionToMarkdown } = require("notion-to-md");
206
- const n2m = new NotionToMarkdown({ notionClient: notion });
207
- n2m.setCustomTransformer('embed', async (block) => {
208
- const {embed} = block as any;
209
- if (!embed?.url) return '';
210
- return `<figure>
211
- <iframe src="${embed?.url}"></iframe>
212
- <figcaption>${await n2m.blockToMarkdown(embed?.caption)}</figcaption>
213
- </figure>`;
214
- });
215
- const result = n2m.blockToMarkdown(block);
216
- // Result will now parse the `embed` type with your custom function.
217
- ```
218
- **Note** Be aware that `setCustomTransformer` will take only the last function for the given type. You can't set two different transforms for the same type.
219
-
220
- ## Contribution
221
-
222
- Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
223
- Please make sure to update tests as appropriate.
224
-
225
- ## Contributers
226
-
227
- <a href="https://github.com/souvikinator/notion-to-md/graphs/contributors">
228
- <img src="https://contrib.rocks/image?repo=souvikinator/notion-to-md" />
229
- </a>
230
-
231
-
232
- ## License
233
-
234
- [MIT](https://choosealicense.com/licenses/mit/)
1
+
2
+ <!--
3
+ 💡 For better readability and detailed instructions head over to the [wiki](https://github.com/souvikinator/notion-to-md/wiki).
4
+ -->
5
+ <h1 align="center">
6
+ <br>
7
+ <img src="https://imgur.com/WgXdz9r.png" alt="notion-to-md banner" width="750" />
8
+ <br>
9
+ <b>Notion-to-MD</b>
10
+ <br>
11
+ <sub><sup><b>(Notion to Markdown)</b></sup></sub>
12
+ <br>
13
+ </h1>
14
+
15
+ <p align="center">
16
+ Notion-to-MD is a Node.js package that allows you to convert Notion pages to Markdown format.
17
+ </p>
18
+ <p align="center">
19
+ Convert notion pages, block and list of blocks to markdown (supports nesting) using <a href="https://github.com/makenotion/notion-sdk-js">notion-sdk-js</a>
20
+ </p>
21
+ <p align="center">
22
+ <img src="https://img.shields.io/github/stars/souvikinator/notion-to-md?style=for-the-badge"
23
+ alt="">
24
+ <a href="https://www.producthunt.com/products/notion-to-md?utm_source=badge-follow&utm_medium=badge&utm_souce=badge-notion&#0045;to&#0045;md" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/follow.svg?product_id=486574&theme=light&size=small" alt="notion&#0045;to&#0045;md - Programmatically&#0032;convert&#0032;notion&#0032;pages&#0032;to&#0032;markdown | Product Hunt" style="width: 86px; height: 32px;" width="86" height="32" /></a>
25
+
26
+ </p>
27
+
28
+
29
+
30
+
31
+ ## Install
32
+
33
+ ```Bash
34
+ npm install notion-to-md
35
+ ```
36
+
37
+ ## Usage
38
+ > **Note:** Before getting started, create [an integration and find the token](https://www.notion.so/my-integrations).
39
+ > Details on methods can be found in [API section](https://github.com/souvikinator/notion-to-md#api)
40
+
41
+ ## converting markdown objects to markdown string
42
+
43
+ This is how the notion page looks for this example:
44
+
45
+ <img src="https://imgur.com/O6bKCmH.png" width="500" />
46
+
47
+ ```javascript
48
+ const { Client } = require("@notionhq/client");
49
+ const { NotionToMarkdown } = require("notion-to-md");
50
+ const fs = require('fs');
51
+ // or
52
+ // import {NotionToMarkdown} from "notion-to-md";
53
+
54
+ const notion = new Client({
55
+ auth: "your integration token",
56
+ });
57
+
58
+ // passing notion client to the option
59
+ const n2m = new NotionToMarkdown({ notionClient: notion });
60
+
61
+ (async () => {
62
+ const mdblocks = await n2m.pageToMarkdown("target_page_id");
63
+ const mdString = n2m.toMarkdownString(mdblocks);
64
+ console.log(mdString);
65
+ })();
66
+ ```
67
+
68
+ **Output:**
69
+
70
+ <img src="https://imgur.com/XrUYrZ0.png" width="500" />
71
+
72
+ ## Save content to file
73
+
74
+ **parent page content:**
75
+
76
+ <img src="https://user-images.githubusercontent.com/64456160/236519171-b32d8f62-1e2c-4897-a302-ae0dd85adef9.png" width="500" />
77
+
78
+ **child page content:**
79
+
80
+ <img src="https://user-images.githubusercontent.com/64456160/236519726-37513735-db0c-4643-9c89-f8deb355a6d3.png" width="500" />
81
+
82
+ ### Saving page to a file
83
+
84
+ Simply saving `mdString` from above code to a file does the work like so
85
+
86
+ ```javascript
87
+ //writing to file
88
+ fs.writeFile("test.md", mdString, (err) => {
89
+ console.log(err);
90
+ });
91
+ ```
92
+ > ⚠️ **Note**: with this the `child_page` content is merged in the same file.
93
+
94
+ **Output:**
95
+
96
+ <img src="https://user-images.githubusercontent.com/64456160/236520621-fc1d328f-51e3-4e39-a529-7ef1b9f75ab3.png" width="500" />
97
+
98
+ ### Saving `child_page` to separate file
99
+
100
+ `NotionToMarkdown` takes second argument, `config`
101
+
102
+
103
+ ```javascript
104
+ const { Client } = require("@notionhq/client");
105
+ const { NotionToMarkdown } = require("notion-to-md");
106
+ const fs = require('fs');
107
+ // or
108
+ // import {NotionToMarkdown} from "notion-to-md";
109
+
110
+ const notion = new Client({
111
+ auth: "your integration token",
112
+ config:{
113
+ saveChildPage: true, //default: false
114
+ saveToDir: "path/to/output/dir", //default: current directory
115
+ }
116
+ });
117
+
118
+ // passing notion client to the option
119
+ const n2m = new NotionToMarkdown({ notionClient: notion });
120
+
121
+ (async () => {
122
+ const mdblocks = await n2m.pageToMarkdown("target_page_id");
123
+ const mdString = n2m.toMarkdownString(mdblocks);
124
+
125
+ // writing parent page content to file
126
+ // where the child pages will be saved
127
+ // the child page will be linked to the parent page
128
+ fs.writeFile("test.md", mdString, (err) => {
129
+ console.log(err);
130
+ });
131
+ })();
132
+ ```
133
+
134
+ **Output:**
135
+
136
+ > ⚠️ **Note:** make sure to save parent page in the same directory as that of child page
137
+ > for the link from parent to child page to work.
138
+
139
+ <img src="https://user-images.githubusercontent.com/64456160/236521464-fd137be9-ade1-444b-86fc-562d5ad06568.png" width="500" />
140
+ <img src="https://user-images.githubusercontent.com/64456160/236521541-c08d6062-9229-4429-96d5-78ab699600a1.png" width="500" />
141
+
142
+ ## converting page to markdown object
143
+
144
+ **Example notion page:**
145
+ <img src="https://imgur.com/9iqRpBl.png" width="500" />
146
+
147
+ ```js
148
+ const { Client } = require("@notionhq/client");
149
+ const { NotionToMarkdown } = require("notion-to-md");
150
+
151
+ const notion = new Client({
152
+ auth: "your integration token",
153
+ });
154
+
155
+ // passing notion client to the option
156
+ const n2m = new NotionToMarkdown({ notionClient: notion });
157
+
158
+ (async () => {
159
+ // notice second argument, totalPage.
160
+ const x = await n2m.pageToMarkdown("target_page_id", 2);
161
+ console.log(x);
162
+ })();
163
+ ```
164
+
165
+ **Output:**
166
+
167
+ ```json
168
+ [
169
+ {
170
+ "parent": "# heading 1",
171
+ "children": []
172
+ },
173
+ {
174
+ "parent": "- bullet 1",
175
+ "children": [
176
+ {
177
+ "parent": "- bullet 1.1",
178
+ "children": []
179
+ },
180
+ {
181
+ "parent": "- bullet 1.2",
182
+ "children": []
183
+ }
184
+ ]
185
+ },
186
+ {
187
+ "parent": "- bullet 2",
188
+ "children": []
189
+ },
190
+ {
191
+ "parent": "- [ ] check box 1",
192
+ "children": [
193
+ {
194
+ "parent": "- [x] check box 1.2",
195
+ "children": []
196
+ },
197
+ {
198
+ "parent": "- [ ] check box 1.3",
199
+ "children": []
200
+ }
201
+ ]
202
+ },
203
+ {
204
+ "parent": "- [ ] checkbox 2",
205
+ "children": []
206
+ }
207
+ ]
208
+ ```
209
+
210
+ ## converting list of blocks to markdown object
211
+
212
+ ```js
213
+ const { Client } = require("@notionhq/client");
214
+ const { NotionToMarkdown } = require("notion-to-md");
215
+
216
+ const notion = new Client({
217
+ auth: "your integration token",
218
+ });
219
+
220
+ // passing notion client to the option
221
+ const n2m = new NotionToMarkdown({ notionClient: notion });
222
+
223
+ (async () => {
224
+ // get all blocks in the page
225
+ const { results } = await notion.blocks.children.list({
226
+ block_id,
227
+ });
228
+
229
+ //convert to markdown
230
+ const x = await n2m.blocksToMarkdown(results);
231
+ console.log(x);
232
+ })();
233
+ ```
234
+
235
+ **Output**: same as before
236
+
237
+ ## Converting a single block to markdown string
238
+
239
+ - only takes a single notion block and returns corresponding markdown string
240
+ - nesting is ignored
241
+ - depends on @notionhq/client
242
+
243
+ ```js
244
+ const { NotionToMarkdown } = require("notion-to-md");
245
+
246
+ // passing notion client to the option
247
+ const n2m = new NotionToMarkdown({ notionClient: notion });
248
+
249
+ const result = n2m.blockToMarkdown(block);
250
+ console.log(result);
251
+ ```
252
+
253
+ **result**:
254
+
255
+ ```
256
+ ![image](https://media.giphy.com/media/Ju7l5y9osyymQ/giphy.gif)
257
+ ```
258
+
259
+ ## Custom Transformers
260
+
261
+ You can define your own custom transformer for a notion type, to parse and return your own string.
262
+ `setCustomTransformer(type, func)` will overload the parsing for the giving type.
263
+
264
+ ```ts
265
+ const { NotionToMarkdown } = require("notion-to-md");
266
+ const n2m = new NotionToMarkdown({ notionClient: notion });
267
+ n2m.setCustomTransformer("embed", async (block) => {
268
+ const { embed } = block as any;
269
+ if (!embed?.url) return "";
270
+ return `<figure>
271
+ <iframe src="${embed?.url}"></iframe>
272
+ <figcaption>${await n2m.blockToMarkdown(embed?.caption)}</figcaption>
273
+ </figure>`;
274
+ });
275
+ const result = n2m.blockToMarkdown(block);
276
+ // Result will now parse the `embed` type with your custom function.
277
+ ```
278
+
279
+ **Note** Be aware that `setCustomTransformer` will take only the last function for the given type. You can't set two different transforms for the same type.
280
+
281
+ You can also use the default parsing by returning `false` in your custom transformer.
282
+
283
+ ```ts
284
+ // ...
285
+ n2m.setCustomTransformer("embed", async (block) => {
286
+ const { embed } = block as any;
287
+ if (embed?.url?.includes("myspecialurl.com")) {
288
+ return `...`; // some special rendering
289
+ }
290
+ return false; // use default behavior
291
+ });
292
+ const result = n2m.blockToMarkdown(block);
293
+ // Result will now only use custom parser if the embed url matches a specific url
294
+ ```
295
+
296
+ ## Contribution
297
+
298
+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
299
+ Please make sure to update tests as appropriate.
300
+
301
+ ## Contributers
302
+
303
+ <a href="https://github.com/souvikinator/notion-to-md/graphs/contributors">
304
+ <img src="https://contrib.rocks/image?repo=souvikinator/notion-to-md" />
305
+ </a>
306
+
307
+ ## License
308
+
309
+ [MIT](https://choosealicense.com/licenses/mit/)
package/build/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./notion-to-md";
1
+ export * from "./notion-to-md";
2
2
  //# sourceMappingURL=index.d.ts.map
package/build/index.js CHANGED
@@ -1,18 +1,18 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./notion-to-md"), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./notion-to-md"), exports);
18
18
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B","sourcesContent":["export * from \"./notion-to-md\";\r\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B","sourcesContent":["export * from \"./notion-to-md\";\n"]}