notion-to-md 1.0.1 → 1.0.2

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 ADDED
@@ -0,0 +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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Notion to Markdown
4
4
 
5
- Notion Markdown Exporter using **[notion-sdk-js](https://github.com/makenotion/notion-sdk-js)**
5
+ Convert notion pages, block and list of blocks to markdown (supports nesting) using **[notion-sdk-js](https://github.com/makenotion/notion-sdk-js)**
6
6
 
7
7
  > **Note:** Before getting started, create [an integration and find the token](https://www.notion.so/my-integrations).
8
8
 
@@ -21,7 +21,7 @@ Notion Markdown Exporter using **[notion-sdk-js](https://github.com/makenotion/n
21
21
  - [ ] embeds
22
22
  - [ ] tables
23
23
  - [ ] videos
24
- - [ ] convert returned markdown object to string
24
+ - [x] convert returned markdown object to string
25
25
  - [ ] add tests
26
26
 
27
27
  ## Install
@@ -32,7 +32,39 @@ $ npm install notion-to-md
32
32
 
33
33
  ## Usage
34
34
 
35
- ### converting page to markdown
35
+ ### converting markdown objects to markdown string
36
+
37
+ This is how the notion page looks for this example:
38
+
39
+ ![Imgur](https://imgur.com/O6bKCmH.png)
40
+
41
+ ```js
42
+ const { Client } = require("@notionhq/client");
43
+ const notion2md = require("notion-to-md");
44
+
45
+ const notion = new Client({
46
+ auth: "your integration token",
47
+ });
48
+
49
+ // passing notion client to the option
50
+ const n2m = new notion2md({ notionClient: notion });
51
+
52
+ (async () => {
53
+ const mdblocks = await n2m.pageToMarkdown("target_page_id");
54
+ const mdString = n2m.toString(mdblocks);
55
+
56
+ //writing to file
57
+ fs.writeFile("test.md", mdString, (err) => {
58
+ console.log(err);
59
+ });
60
+ })();
61
+ ```
62
+
63
+ **Output:**
64
+
65
+ ![output](https://imgur.com/XrUYrZ0.png)
66
+
67
+ ### converting page to markdown object
36
68
 
37
69
  This is how the notion page looks for this example:
38
70
 
@@ -100,7 +132,7 @@ const n2m = new notion2md({ notionClient: notion });
100
132
  ]
101
133
  ```
102
134
 
103
- ### converting list of blocks to markdown
135
+ ### converting list of blocks to markdown object
104
136
 
105
137
  > This example blocks from a single page is used. One construct array of blocks from different notion pages and pass in to the `blocksToMarkdown()`
106
138
 
@@ -131,7 +163,7 @@ const n2m = new notion2md({ notionClient: notion });
131
163
 
132
164
  **Output**: same as before
133
165
 
134
- ### Converting a single block to markdown
166
+ ### Converting a single block to markdown string
135
167
 
136
168
  - just takes a single notion block and returns corresponding markdown string
137
169
  - nesting is ignored
@@ -149,9 +181,15 @@ console.log(result);
149
181
 
150
182
  ## API
151
183
 
184
+ ### `toString(markdownObjects)`
185
+
186
+ - takes output of `pageToMarkdown` or `blocksToMarkdown` as argument
187
+ - convert to markdown string.
188
+
152
189
  ### `pageToMarkdown(page_id)`
153
190
 
154
191
  - Takes page_id as input and converts all the blocks in the page to corresponding markdown
192
+ - Uses `blocksToMarkdown` internally.
155
193
 
156
194
  ### `blockToMarkdown(block)`
157
195
 
package/notion-to-md.js CHANGED
@@ -10,6 +10,21 @@ class notion2md {
10
10
  this.notionClient = options.notionClient;
11
11
  }
12
12
 
13
+ toString(mdBlocks = [], nestingLevel = 0) {
14
+ let mdString = "";
15
+ mdBlocks.forEach((mdBlocks) => {
16
+ if (mdBlocks.parent) {
17
+ mdString += `
18
+ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
19
+ `;
20
+ }
21
+ if (mdBlocks.children && mdBlocks.children.length > 0) {
22
+ mdString += this.toString(mdBlocks.children, nestingLevel + 1);
23
+ }
24
+ });
25
+ return mdString;
26
+ }
27
+
13
28
  async pageToMarkdown(id) {
14
29
  if (!id) throw new Error("pageToMarkdown takes page_id as argument");
15
30
  const blocks = await getBlockChildren(this.notionClient, id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notion-to-md",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "notion-to-md.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
package/utils/md.js CHANGED
@@ -23,7 +23,9 @@ exports.link = (text, href) => {
23
23
  };
24
24
 
25
25
  exports.codeBlock = (text) => {
26
- return `\`\`\`${text}\`\`\``;
26
+ return `\`\`\`
27
+ ${text}
28
+ \`\`\``;
27
29
  };
28
30
 
29
31
  exports.heading1 = (text) => {
@@ -54,4 +56,8 @@ exports.image = (alt, href) => {
54
56
  return `![${alt}](${href})`;
55
57
  };
56
58
 
57
- //TODO: embed and fluid tags
59
+ exports.addTabSpace = (text, n = 0) => {
60
+ const tab = " ";
61
+ for (i = 0; i < n; i++) text = tab + text;
62
+ return text;
63
+ };