notion-to-md 1.0.6 → 2.0.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 CHANGED
@@ -66,6 +66,8 @@ const n2m = new notion2md({ notionClient: notion });
66
66
 
67
67
  ![output](https://imgur.com/XrUYrZ0.png)
68
68
 
69
+ > **Note:** Details on provided methods can be found in [API section](https://github.com/souvikinator/notion-to-md#api)
70
+
69
71
  ### converting page to markdown object
70
72
 
71
73
  This is how the notion page looks for this example:
@@ -84,11 +86,17 @@ const notion = new Client({
84
86
  const n2m = new notion2md({ notionClient: notion });
85
87
 
86
88
  (async () => {
87
- const x = await n2m.pageToMarkdown("target_page_id");
89
+ // notice second argument, totalPage.
90
+ const x = await n2m.pageToMarkdown("target_page_id", 2);
88
91
  console.log(x);
89
92
  })();
90
93
  ```
91
94
 
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
+
92
100
  **Output:**
93
101
 
94
102
  ```json
@@ -194,26 +202,33 @@ console.log(result);
194
202
  - takes output of `pageToMarkdown` or `blocksToMarkdown` as argument
195
203
  - convert to markdown string.
196
204
 
197
- ### `pageToMarkdown(page_id)`
205
+ ### `pageToMarkdown(page_id,totalPage)`
198
206
 
199
- - Takes page_id as input and converts all the blocks in the page to corresponding markdown
200
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`.
201
210
 
202
- ### `blockToMarkdown(block)`
203
-
204
- - Takes one notion block and converts to markdown
205
- - does not deal with nested notion blocks
206
- - This method doesn't require the `notion-sdk-js`.
207
- - Refer docs to know more about [notion blocks](https://developers.notion.com/reference/block)
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`.
208
215
 
209
- ### `blocksToMarkdown(blocks)`
216
+ ### `blocksToMarkdown(blocks,totalPage)`
210
217
 
211
218
  > **Note**: requires <u>**notion-sdk-js**</u> unlike `blockToMarkdown`
212
219
 
213
220
  - `blocks`: array of notion blocks
221
+ - `totalPage`: the retrieve block children request number i.e `page_size Maximum = totalPage * 100`.
214
222
  - deals with <u>**nested blocks**</u>
215
223
  - uses `blockToMarkdown` internally.
216
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
+
217
232
  ## Contribution
218
233
 
219
234
  Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
package/notion-to-md.js CHANGED
@@ -1,6 +1,5 @@
1
1
  const md = require("./utils/md");
2
2
  const { getBlockChildren } = require("./utils/notion");
3
-
4
3
  class notion2md {
5
4
  constructor(options) {
6
5
  if (!options) {
@@ -25,18 +24,22 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
25
24
  return mdString;
26
25
  }
27
26
 
28
- async pageToMarkdown(id) {
27
+ /**
28
+ * @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
29
+ */
30
+ async pageToMarkdown(id, totalPage = 1) {
29
31
  if (!id) throw new Error("pageToMarkdown takes page_id as argument");
30
- const blocks = await getBlockChildren(this.notionClient, id);
32
+ const blocks = await getBlockChildren(this.notionClient, id, totalPage);
31
33
  const parsedData = await this.blocksToMarkdown(blocks);
32
34
  return parsedData;
33
35
  }
34
36
 
35
37
  /**
36
38
  * @param {object} blocks - list of notion blocks
39
+ * @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
37
40
  * @returns array of md blocks with their children
38
41
  */
39
- async blocksToMarkdown(blocks, mdBlocks = []) {
42
+ async blocksToMarkdown(blocks, mdBlocks = [], totalPage = 1) {
40
43
  if (!this.notionClient) {
41
44
  throw new Error(
42
45
  "notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md"
@@ -46,7 +49,11 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
46
49
  for (let i = 0; i < blocks.length; i++) {
47
50
  let block = blocks[i];
48
51
  if (block.has_children) {
49
- let child_blocks = await getBlockChildren(this.notionClient, block.id);
52
+ let child_blocks = await getBlockChildren(
53
+ this.notionClient,
54
+ block.id,
55
+ totalPage
56
+ );
50
57
  mdBlocks.push({ parent: this.blockToMarkdown(block), children: [] });
51
58
  let l = mdBlocks.length;
52
59
  await this.blocksToMarkdown(child_blocks, mdBlocks[l - 1].children);
@@ -125,13 +132,19 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
125
132
  * @param {object} annotations annotation object of a notion block
126
133
  */
127
134
  annotatePlainText(text, annotations) {
128
- if (annotations.code) text = md.inlineCode(text);
129
- if (annotations.bold) text = md.bold(text);
130
- if (annotations.italic) text = md.italic(text);
131
- if (annotations.strikethrough) text = md.strikethrough(text);
132
- if (annotations.underline) text = md.underline(text);
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
+ }
133
146
 
134
- return text;
147
+ return leading_space + text + trailing_space;
135
148
  }
136
149
  }
137
150
 
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "notion-to-md",
3
- "version": "1.0.6",
3
+ "version": "2.0.0",
4
4
  "main": "notion-to-md.js",
5
+ "typings": "main.d.ts",
5
6
  "scripts": {
6
7
  "test": "echo \"Error: no test specified\" && exit 1"
7
8
  },
@@ -24,5 +25,9 @@
24
25
  "url": "https://github.com/souvikinator/notion-to-md/issues"
25
26
  },
26
27
  "homepage": "https://github.com/souvikinator/notion-to-md#readme",
27
- "description": "convert notion pages, block and list of blocks to markdown (supports nesting)"
28
+ "description": "convert notion pages, block and list of blocks to markdown (supports nesting)",
29
+ "dependencies": {
30
+ "@notionhq/client": "^0.4.11",
31
+ "notion-to-md": "^1.0.9"
32
+ }
28
33
  }
package/utils/notion.js CHANGED
@@ -1,14 +1,33 @@
1
- exports.getBlockChildren = async (notionClient, block_id) => {
1
+ exports.getBlockChildren = async (notionClient, block_id, totalPage) => {
2
2
  try {
3
3
  if (!notionClient) {
4
4
  throw new Error(
5
5
  "notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md"
6
6
  );
7
7
  }
8
- const response = await notionClient.blocks.children.list({
9
- block_id,
10
- });
11
- return response.results;
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;
12
31
  } catch (e) {
13
32
  console.log(e);
14
33
  }