notion-to-md 1.0.0 → 1.0.4
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 +21 -0
- package/README.md +49 -9
- package/notion-to-md.js +33 -2
- package/package.json +2 -2
- package/utils/md.js +12 -2
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
|
-
|
|
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
|
|
|
@@ -18,10 +18,12 @@ Notion Markdown Exporter using **[notion-sdk-js](https://github.com/makenotion/n
|
|
|
18
18
|
- [x] code block
|
|
19
19
|
- [x] strikethrough, underline, bold, italic
|
|
20
20
|
- [x] nested blocks
|
|
21
|
-
- [ ]
|
|
21
|
+
- [ ] pages inside pages/child page
|
|
22
|
+
- [x] embeds, bookmarks, videos, files (converted to links)
|
|
22
23
|
- [ ] tables
|
|
23
|
-
- [
|
|
24
|
-
- [
|
|
24
|
+
- [x] divider
|
|
25
|
+
- [x] equation block (converted to code blocks)
|
|
26
|
+
- [x] convert returned markdown object to string (`toMarkdownString()`)
|
|
25
27
|
- [ ] add tests
|
|
26
28
|
|
|
27
29
|
## Install
|
|
@@ -32,7 +34,39 @@ $ npm install notion-to-md
|
|
|
32
34
|
|
|
33
35
|
## Usage
|
|
34
36
|
|
|
35
|
-
### converting
|
|
37
|
+
### converting markdown objects to markdown string
|
|
38
|
+
|
|
39
|
+
This is how the notion page looks for this example:
|
|
40
|
+
|
|
41
|
+

|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
const { Client } = require("@notionhq/client");
|
|
45
|
+
const notion2md = require("notion-to-md");
|
|
46
|
+
|
|
47
|
+
const notion = new Client({
|
|
48
|
+
auth: "your integration token",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// passing notion client to the option
|
|
52
|
+
const n2m = new notion2md({ notionClient: notion });
|
|
53
|
+
|
|
54
|
+
(async () => {
|
|
55
|
+
const mdblocks = await n2m.pageToMarkdown("target_page_id");
|
|
56
|
+
const mdString = n2m.toMarkdownString(mdblocks);
|
|
57
|
+
|
|
58
|
+
//writing to file
|
|
59
|
+
fs.writeFile("test.md", mdString, (err) => {
|
|
60
|
+
console.log(err);
|
|
61
|
+
});
|
|
62
|
+
})();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**Output:**
|
|
66
|
+
|
|
67
|
+

|
|
68
|
+
|
|
69
|
+
### converting page to markdown object
|
|
36
70
|
|
|
37
71
|
This is how the notion page looks for this example:
|
|
38
72
|
|
|
@@ -40,7 +74,7 @@ This is how the notion page looks for this example:
|
|
|
40
74
|
|
|
41
75
|
```js
|
|
42
76
|
const { Client } = require("@notionhq/client");
|
|
43
|
-
const notion2md = require("
|
|
77
|
+
const notion2md = require("notion-to-md");
|
|
44
78
|
|
|
45
79
|
const notion = new Client({
|
|
46
80
|
auth: "your integration token",
|
|
@@ -100,7 +134,7 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
100
134
|
]
|
|
101
135
|
```
|
|
102
136
|
|
|
103
|
-
### converting list of blocks to markdown
|
|
137
|
+
### converting list of blocks to markdown object
|
|
104
138
|
|
|
105
139
|
> 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
140
|
|
|
@@ -108,7 +142,7 @@ same notion page as before
|
|
|
108
142
|
|
|
109
143
|
```js
|
|
110
144
|
const { Client } = require("@notionhq/client");
|
|
111
|
-
const notion2md = require("
|
|
145
|
+
const notion2md = require("notion-to-md");
|
|
112
146
|
|
|
113
147
|
const notion = new Client({
|
|
114
148
|
auth: "your integration token",
|
|
@@ -131,7 +165,7 @@ const n2m = new notion2md({ notionClient: notion });
|
|
|
131
165
|
|
|
132
166
|
**Output**: same as before
|
|
133
167
|
|
|
134
|
-
### Converting a single block to markdown
|
|
168
|
+
### Converting a single block to markdown string
|
|
135
169
|
|
|
136
170
|
- just takes a single notion block and returns corresponding markdown string
|
|
137
171
|
- nesting is ignored
|
|
@@ -149,9 +183,15 @@ console.log(result);
|
|
|
149
183
|
|
|
150
184
|
## API
|
|
151
185
|
|
|
186
|
+
### `toMarkdownString(markdownObjects)`
|
|
187
|
+
|
|
188
|
+
- takes output of `pageToMarkdown` or `blocksToMarkdown` as argument
|
|
189
|
+
- convert to markdown string.
|
|
190
|
+
|
|
152
191
|
### `pageToMarkdown(page_id)`
|
|
153
192
|
|
|
154
193
|
- Takes page_id as input and converts all the blocks in the page to corresponding markdown
|
|
194
|
+
- Uses `blocksToMarkdown` internally.
|
|
155
195
|
|
|
156
196
|
### `blockToMarkdown(block)`
|
|
157
197
|
|
package/notion-to-md.js
CHANGED
|
@@ -10,6 +10,21 @@ class notion2md {
|
|
|
10
10
|
this.notionClient = options.notionClient;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
toMarkdownString(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.toMarkdownString(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);
|
|
@@ -32,7 +47,7 @@ class notion2md {
|
|
|
32
47
|
continue;
|
|
33
48
|
}
|
|
34
49
|
let tmp = this.blockToMarkdown(block);
|
|
35
|
-
|
|
50
|
+
|
|
36
51
|
mdBlocks.push({ parent: tmp, children: [] });
|
|
37
52
|
}
|
|
38
53
|
return mdBlocks;
|
|
@@ -51,6 +66,23 @@ class notion2md {
|
|
|
51
66
|
const image_type = blockContent["type"];
|
|
52
67
|
|
|
53
68
|
return md.image("image", blockContent[image_type].url);
|
|
69
|
+
} else if (type === "divider") {
|
|
70
|
+
return md.divider();
|
|
71
|
+
} else if (type === "equation") {
|
|
72
|
+
return md.codeBlock(block[type].expression);
|
|
73
|
+
} else if (type === "video" || type === "file" || type === "pdf") {
|
|
74
|
+
blockContent = block[type];
|
|
75
|
+
const urlType = blockContent["type"];
|
|
76
|
+
|
|
77
|
+
return md.link(type, blockContent[urlType].url);
|
|
78
|
+
} else if (
|
|
79
|
+
type === "bookmark" ||
|
|
80
|
+
type === "embed" ||
|
|
81
|
+
type === "link_preview"
|
|
82
|
+
) {
|
|
83
|
+
blockContent = block[type];
|
|
84
|
+
|
|
85
|
+
return md.link(type, blockContent.url);
|
|
54
86
|
} else {
|
|
55
87
|
blockContent = block[type]["text"] || [];
|
|
56
88
|
blockContent.map((content) => {
|
|
@@ -65,7 +97,6 @@ class notion2md {
|
|
|
65
97
|
});
|
|
66
98
|
}
|
|
67
99
|
|
|
68
|
-
// TODO: need to take care of escape sequences
|
|
69
100
|
if (type === "code") parsedData = md.codeBlock(parsedData);
|
|
70
101
|
if (type === "heading_1") parsedData = md.heading1(parsedData);
|
|
71
102
|
if (type === "heading_2") parsedData = md.heading2(parsedData);
|
package/package.json
CHANGED
package/utils/md.js
CHANGED
|
@@ -23,7 +23,9 @@ exports.link = (text, href) => {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
exports.codeBlock = (text) => {
|
|
26
|
-
return
|
|
26
|
+
return `\`\`\`
|
|
27
|
+
${text}
|
|
28
|
+
\`\`\``;
|
|
27
29
|
};
|
|
28
30
|
|
|
29
31
|
exports.heading1 = (text) => {
|
|
@@ -54,4 +56,12 @@ exports.image = (alt, href) => {
|
|
|
54
56
|
return ``;
|
|
55
57
|
};
|
|
56
58
|
|
|
57
|
-
|
|
59
|
+
exports.addTabSpace = (text, n = 0) => {
|
|
60
|
+
const tab = " ";
|
|
61
|
+
for (i = 0; i < n; i++) text = tab + text;
|
|
62
|
+
return text;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
exports.divider = () => {
|
|
66
|
+
return "---";
|
|
67
|
+
};
|