notion-to-md 2.2.1 → 2.3.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/README.md CHANGED
@@ -1,11 +1,15 @@
1
1
  <img src="https://imgur.com/WgXdz9r.png" />
2
2
 
3
+ > 💡 For better readability and detailed instructions headover to the [wiki](https://github.com/souvikinator/notion-to-md/wiki)
4
+
3
5
  # Notion to Markdown
4
6
 
5
7
  Convert notion pages, block and list of blocks to markdown (supports nesting) using **[notion-sdk-js](https://github.com/makenotion/notion-sdk-js)**
6
8
 
7
9
  > **Note:** Before getting started, create [an integration and find the token](https://www.notion.so/my-integrations).
8
10
 
11
+ [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/O5O1AFCJR)
12
+
9
13
  ## Todo
10
14
 
11
15
  - [x] heading
@@ -191,44 +195,6 @@ console.log(result);
191
195
  ![image](https://media.giphy.com/media/Ju7l5y9osyymQ/giphy.gif)
192
196
  ```
193
197
 
194
- ## API
195
-
196
- > ### `toMarkdownString(mdBlock[])`
197
- >
198
- > - takes output of `pageToMarkdown` or `blocksToMarkdown` as argument
199
- > - converts to markdown string.
200
-
201
- > ### `pageToMarkdown(id,totalPage)`
202
- >
203
- > - Uses `blocksToMarkdown` internally.
204
- > - `id`(pageid) as input and converts all the blocks in the page to corresponding markdown object
205
- > - `totalPage` is the retrieve block children request number i.e `page_size Maximum = totalPage * 100`.
206
-
207
- > ### totalPage
208
- >
209
- > 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)).
210
- >
211
- > ### How to use `totalPage` arg ?
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 `150` blocks.
215
-
216
- > ### `blocksToMarkdown(blocks,totalPage)`
217
- >
218
- > **Note**: requires <u>**notion-sdk-js**</u>
219
- >
220
- > - `blocks`: array of notion blocks
221
- > - `totalPage`: the retrieve block children request number i.e `page_size Maximum = totalPage * 100`.
222
- > - deals with <u>**nested blocks**</u>
223
- > - uses `blockToMarkdown` internally.
224
-
225
- > ### `blockToMarkdown(block)`
226
- >
227
- > - Takes single notion block and converts to markdown string
228
- > - does not deal with nested notion blocks
229
- > - depends on `notion-sdk-js`.
230
- > - Refer docs to know more about [notion blocks](https://developers.notion.com/reference/block)
231
-
232
198
  ## Contribution
233
199
 
234
200
  Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
package/build/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAA+B","sourcesContent":["export * from \"./notion-to-md\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B","sourcesContent":["export * from \"./notion-to-md\";\n"]}
@@ -15,10 +15,10 @@ export declare class NotionToMarkdown {
15
15
  /**
16
16
  * Retrieves Notion Blocks based on ID and converts them to Markdown Blocks
17
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)
18
+ * @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100 (Default=null)
19
19
  * @returns {Promise<MdBlock[]>} - List of markdown blocks
20
20
  */
21
- pageToMarkdown(id: string, totalPage?: number): Promise<MdBlock[]>;
21
+ pageToMarkdown(id: string, totalPage?: number | null): Promise<MdBlock[]>;
22
22
  /**
23
23
  * Converts list of Notion Blocks to Markdown Blocks
24
24
  * @param {ListBlockChildrenResponseResults | undefined} blocks - List of notion blocks
@@ -26,7 +26,7 @@ export declare class NotionToMarkdown {
26
26
  * @param {MdBlock[]} mdBlocks - Defines max depth of nesting
27
27
  * @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children
28
28
  */
29
- blocksToMarkdown(blocks?: ListBlockChildrenResponseResults, totalPage?: number, mdBlocks?: MdBlock[]): Promise<MdBlock[]>;
29
+ blocksToMarkdown(blocks?: ListBlockChildrenResponseResults, totalPage?: number | null, mdBlocks?: MdBlock[]): Promise<MdBlock[]>;
30
30
  /**
31
31
  * Converts a Notion Block to a Markdown Block
32
32
  * @param {ListBlockChildrenResponseResult} block - single notion block
@@ -1 +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,CAAS;gBAEjB,OAAO,EAAE,uBAAuB;IAI5C;;;;;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;IAa3E;;;;;;OAMG;IACG,gBAAgB,CACpB,MAAM,CAAC,EAAE,gCAAgC,EACzC,SAAS,GAAE,MAAU,EACrB,QAAQ,GAAE,OAAO,EAAO,GACvB,OAAO,CAAC,OAAO,EAAE,CAAC;IAsCrB;;;;OAIG;IACG,eAAe,CAAC,KAAK,EAAE,+BAA+B;IAyL5D;;;;;OAKG;IACH,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,MAAM;CAsBlE"}
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,CAAS;gBAEjB,OAAO,EAAE,uBAAuB;IAI5C;;;;;OAKG;IACH,gBAAgB,CAAC,QAAQ,GAAE,OAAO,EAAO,EAAE,YAAY,GAAE,MAAU,GAAG,MAAM;IAe5E;;;;;OAKG;IACG,cAAc,CAClB,EAAE,EAAE,MAAM,EACV,SAAS,GAAE,MAAM,GAAG,IAAW,GAC9B,OAAO,CAAC,OAAO,EAAE,CAAC;IAarB;;;;;;OAMG;IACG,gBAAgB,CACpB,MAAM,CAAC,EAAE,gCAAgC,EACzC,SAAS,GAAE,MAAM,GAAG,IAAW,EAC/B,QAAQ,GAAE,OAAO,EAAO,GACvB,OAAO,CAAC,OAAO,EAAE,CAAC;IAsCrB;;;;OAIG;IACG,eAAe,CAAC,KAAK,EAAE,+BAA+B;IA+L5D;;;;;OAKG;IACH,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,MAAM;CAsBlE"}
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[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);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -52,15 +56,13 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
52
56
  /**
53
57
  * Retrieves Notion Blocks based on ID and converts them to Markdown Blocks
54
58
  * @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)
59
+ * @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100 (Default=null)
56
60
  * @returns {Promise<MdBlock[]>} - List of markdown blocks
57
61
  */
58
- async pageToMarkdown(id, totalPage = 1) {
62
+ async pageToMarkdown(id, totalPage = null) {
59
63
  if (!this.notionClient) {
60
64
  throw new Error("notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md");
61
65
  }
62
- if (!id)
63
- throw new Error("pageToMarkdown takes page_id as argument");
64
66
  const blocks = await (0, notion_1.getBlockChildren)(this.notionClient, id, totalPage);
65
67
  const parsedData = await this.blocksToMarkdown(blocks);
66
68
  return parsedData;
@@ -72,7 +74,7 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
72
74
  * @param {MdBlock[]} mdBlocks - Defines max depth of nesting
73
75
  * @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children
74
76
  */
75
- async blocksToMarkdown(blocks, totalPage = 1, mdBlocks = []) {
77
+ async blocksToMarkdown(blocks, totalPage = null, mdBlocks = []) {
76
78
  if (!this.notionClient) {
77
79
  throw new Error("notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md");
78
80
  }
@@ -177,7 +179,7 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
177
179
  */
178
180
  let cellStringPromise = cells.map(async (cell) => await this.blockToMarkdown({
179
181
  type: "paragraph",
180
- paragraph: { text: cell },
182
+ paragraph: { rich_text: cell },
181
183
  }));
182
184
  const cellStringArr = await Promise.all(cellStringPromise);
183
185
  // console.log("~~", cellStringArr);
@@ -215,7 +217,7 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
215
217
  default: {
216
218
  // In this case typescript is not able to index the types properly, hence ignoring the error
217
219
  // @ts-ignore
218
- let blockContent = block[type].text || [];
220
+ let blockContent = block[type].text || block[type].rich_text || [];
219
221
  blockContent.map((content) => {
220
222
  const annotations = content.annotations;
221
223
  let plain_text = content.plain_text;
@@ -252,6 +254,11 @@ ${md.addTabSpace(mdBlocks.parent, nestingLevel)}
252
254
  parsedData = md.quote(parsedData);
253
255
  }
254
256
  break;
257
+ case "callout":
258
+ {
259
+ parsedData = md.callout(parsedData, block[type].icon);
260
+ }
261
+ break;
255
262
  case "bulleted_list_item":
256
263
  case "numbered_list_item":
257
264
  {
@@ -1 +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,OAAgC;QAC1C,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAC3C,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,GAA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gBAAE,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC3C,IAAI,WAAW,CAAC,MAAM;gBAAE,IAAI,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAC/C,IAAI,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;AA1TD,4CA0TC","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.notionClient;\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\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\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({\n parent: await this.blockToMarkdown(block),\n children: [],\n });\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 = await 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 async blockToMarkdown(block: ListBlockChildrenResponseResult) {\n if (!(\"type\" in block)) return \"\";\n\n let parsedData = \"\";\n const { type } = block;\n // console.log({ block });\n\n switch (type) {\n case \"image\":\n {\n let blockContent = block.image;\n const image_caption_plain = blockContent.caption\n .map((item) => item.plain_text)\n .join(\"\");\n const image_type = blockContent.type;\n if (image_type === \"external\")\n return md.image(image_caption_plain, blockContent.external.url);\n if (image_type === \"file\")\n return md.image(image_caption_plain, blockContent.file.url);\n }\n break;\n\n case \"divider\": {\n return md.divider();\n }\n\n case \"equation\": {\n return md.codeBlock(block.equation.expression);\n }\n\n case \"video\":\n case \"file\":\n case \"pdf\":\n {\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 }\n break;\n\n case \"bookmark\":\n case \"embed\":\n case \"link_preview\":\n {\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 }\n break;\n\n case \"table\": {\n const { id, has_children } = block;\n let tableArr: string[][] = [];\n if (has_children) {\n const tableRows = await getBlockChildren(this.notionClient, id, 100);\n // console.log(\">>\", tableRows);\n let rowsPromise = tableRows?.map(async (row) => {\n const { type } = row as any;\n const cells = (row as any)[type][\"cells\"];\n\n /**\n * this is more like a hack since matching the type text was\n * difficult. So converting each cell to paragraph type to\n * reuse the blockToMarkdown function\n */\n let cellStringPromise = cells.map(\n async (cell: any) =>\n await this.blockToMarkdown({\n type: \"paragraph\",\n paragraph: { text: cell },\n } as ListBlockChildrenResponseResult)\n );\n\n const cellStringArr = await Promise.all(cellStringPromise);\n // console.log(\"~~\", cellStringArr);\n tableArr.push(cellStringArr);\n // console.log(tableArr);\n });\n await Promise.all(rowsPromise || []);\n }\n parsedData = md.table(tableArr);\n return parsedData;\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\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 {\n parsedData = md.codeBlock(parsedData, block[type].language);\n }\n break;\n\n case \"heading_1\":\n {\n parsedData = md.heading1(parsedData);\n }\n break;\n\n case \"heading_2\":\n {\n parsedData = md.heading2(parsedData);\n }\n break;\n\n case \"heading_3\":\n {\n parsedData = md.heading3(parsedData);\n }\n break;\n\n case \"quote\":\n {\n parsedData = md.quote(parsedData);\n }\n break;\n\n case \"bulleted_list_item\":\n case \"numbered_list_item\":\n {\n parsedData = md.bullet(parsedData);\n }\n break;\n\n case \"to_do\":\n {\n parsedData = md.todo(parsedData, block.to_do.checked);\n }\n break;\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"]}
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,OAAgC;QAC1C,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAC3C,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;QACH,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,cAAc,CAClB,EAAU,EACV,YAA2B,IAAI;QAE/B,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACtB,MAAM,IAAI,KAAK,CACb,wGAAwG,CACzG,CAAC;SACH;QAED,MAAM,MAAM,GAAG,MAAM,IAAA,yBAAgB,EAAC,IAAI,CAAC,YAAY,EAA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C,SAAS,CAAC,IAAI,CAAC,CAAC;SACtD;QAED,OAAO,aAAa,GAAG,IAAI,GAAG,cAAc,CAAC;IAC/C,CAAC;CACF;AAnUD,4CAmUC","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.notionClient;\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=null)\n * @returns {Promise<MdBlock[]>} - List of markdown blocks\n */\n async pageToMarkdown(\n id: string,\n totalPage: number | null = null\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 const blocks = await getBlockChildren(this.notionClient, id, totalPage);\n\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 | null = null,\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\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({\n parent: await this.blockToMarkdown(block),\n children: [],\n });\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 = await 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 async blockToMarkdown(block: ListBlockChildrenResponseResult) {\n if (!(\"type\" in block)) return \"\";\n\n let parsedData = \"\";\n const { type } = block;\n // console.log({ block });\n\n switch (type) {\n case \"image\":\n {\n let blockContent = block.image;\n const image_caption_plain = blockContent.caption\n .map((item) => item.plain_text)\n .join(\"\");\n const image_type = blockContent.type;\n if (image_type === \"external\")\n return md.image(image_caption_plain, blockContent.external.url);\n if (image_type === \"file\")\n return md.image(image_caption_plain, blockContent.file.url);\n }\n break;\n\n case \"divider\": {\n return md.divider();\n }\n\n case \"equation\": {\n return md.codeBlock(block.equation.expression);\n }\n\n case \"video\":\n case \"file\":\n case \"pdf\":\n {\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 }\n break;\n\n case \"bookmark\":\n case \"embed\":\n case \"link_preview\":\n {\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 }\n break;\n\n case \"table\": {\n const { id, has_children } = block;\n let tableArr: string[][] = [];\n if (has_children) {\n const tableRows = await getBlockChildren(this.notionClient, id, 100);\n // console.log(\">>\", tableRows);\n let rowsPromise = tableRows?.map(async (row) => {\n const { type } = row as any;\n const cells = (row as any)[type][\"cells\"];\n\n /**\n * this is more like a hack since matching the type text was\n * difficult. So converting each cell to paragraph type to\n * reuse the blockToMarkdown function\n */\n let cellStringPromise = cells.map(\n async (cell: any) =>\n await this.blockToMarkdown({\n type: \"paragraph\",\n paragraph: { rich_text: cell },\n } as ListBlockChildrenResponseResult)\n );\n\n const cellStringArr = await Promise.all(cellStringPromise);\n // console.log(\"~~\", cellStringArr);\n tableArr.push(cellStringArr);\n // console.log(tableArr);\n });\n await Promise.all(rowsPromise || []);\n }\n parsedData = md.table(tableArr);\n return parsedData;\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\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 || block[type].rich_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 {\n parsedData = md.codeBlock(parsedData, block[type].language);\n }\n break;\n\n case \"heading_1\":\n {\n parsedData = md.heading1(parsedData);\n }\n break;\n\n case \"heading_2\":\n {\n parsedData = md.heading2(parsedData);\n }\n break;\n\n case \"heading_3\":\n {\n parsedData = md.heading3(parsedData);\n }\n break;\n\n case \"quote\":\n {\n parsedData = md.quote(parsedData);\n }\n break;\n\n case \"callout\":\n {\n parsedData = md.callout(parsedData, block[type].icon);\n }\n break;\n\n case \"bulleted_list_item\":\n case \"numbered_list_item\":\n {\n parsedData = md.bullet(parsedData);\n }\n break;\n\n case \"to_do\":\n {\n parsedData = md.todo(parsedData, block.to_do.checked);\n }\n break;\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"]}
@@ -30,4 +30,19 @@ export declare type Text = {
30
30
  plain_text: string;
31
31
  href: string | null;
32
32
  };
33
+ export declare type CalloutIcon = {
34
+ type: "emoji";
35
+ emoji?: string;
36
+ } | {
37
+ type: "external";
38
+ external?: {
39
+ url: string;
40
+ };
41
+ } | {
42
+ type: "file";
43
+ file: {
44
+ url: string;
45
+ expiry_time: string;
46
+ };
47
+ } | null;
33
48
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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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E;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;AAEF,oBAAY,WAAW,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CAAE,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,EAAE;QAAG,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAC;KAAE,CAAC;CAAE,GAAG,IAAI,CAAC"}
@@ -1 +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"]}
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\nexport type CalloutIcon = { type: \"emoji\"; emoji?: string; } | { type: \"external\"; external?: { url: string }; } | { type: \"file\"; file: { url: string; expiry_time: string; }; } | null;"]}
@@ -1,3 +1,4 @@
1
+ import { CalloutIcon } from '../types';
1
2
  export declare const inlineCode: (text: string) => string;
2
3
  export declare const bold: (text: string) => string;
3
4
  export declare const italic: (text: string) => string;
@@ -9,6 +10,7 @@ export declare const heading1: (text: string) => string;
9
10
  export declare const heading2: (text: string) => string;
10
11
  export declare const heading3: (text: string) => string;
11
12
  export declare const quote: (text: string) => string;
13
+ export declare const callout: (text: string, icon?: CalloutIcon | undefined) => string;
12
14
  export declare const bullet: (text: string) => string;
13
15
  export declare const todo: (text: string, checked: boolean) => string;
14
16
  export declare const image: (alt: string, href: string) => string;
@@ -1 +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;AAEF,eAAO,MAAM,cAAc,QAAS,MAAM,EAAE,WAI3C,CAAC;AAEF,eAAO,MAAM,YAAY,QAAS,MAAM,EAAE,WAEzC,CAAC;AAEF,eAAO,MAAM,KAAK,UAAW,MAAM,EAAE,EAAE,WAKtC,CAAC"}
1
+ {"version":3,"file":"md.d.ts","sourceRoot":"","sources":["../../src/utils/md.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,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,OAAO,SAAU,MAAM,2CAQnC,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,uBASvC,CAAC;AAEF,eAAO,MAAM,OAAO,cAEnB,CAAC;AAEF,eAAO,MAAM,cAAc,QAAS,MAAM,EAAE,WAI3C,CAAC;AAEF,eAAO,MAAM,YAAY,QAAS,MAAM,EAAE,WAEzC,CAAC;AAEF,eAAO,MAAM,KAAK,UAAW,MAAM,EAAE,EAAE,WAKtC,CAAC"}
package/build/utils/md.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.table = exports.tableRowBody = exports.tableRowHeader = exports.divider = exports.addTabSpace = exports.image = exports.todo = exports.bullet = exports.quote = exports.heading3 = exports.heading2 = exports.heading1 = exports.codeBlock = exports.link = exports.underline = exports.strikethrough = exports.italic = exports.bold = exports.inlineCode = void 0;
3
+ exports.table = exports.tableRowBody = exports.tableRowHeader = exports.divider = exports.addTabSpace = exports.image = exports.todo = exports.bullet = exports.callout = exports.quote = exports.heading3 = exports.heading2 = exports.heading1 = exports.codeBlock = exports.link = exports.underline = exports.strikethrough = exports.italic = exports.bold = exports.inlineCode = void 0;
4
4
  const inlineCode = (text) => {
5
5
  return `\`${text}\``;
6
6
  };
@@ -48,6 +48,15 @@ const quote = (text) => {
48
48
  return `> ${text.replace(/\n/g, " \n>")}`;
49
49
  };
50
50
  exports.quote = quote;
51
+ const callout = (text, icon) => {
52
+ let emoji;
53
+ if ((icon === null || icon === void 0 ? void 0 : icon.type) === 'emoji') {
54
+ emoji = icon.emoji;
55
+ }
56
+ // the replace is done to handle multiple lines
57
+ return `> ${emoji ? emoji + ' ' : ''}${text.replace(/\n/g, " \n>")}`;
58
+ };
59
+ exports.callout = callout;
51
60
  const bullet = (text) => {
52
61
  return `- ${text}`;
53
62
  };
@@ -62,8 +71,14 @@ const image = (alt, href) => {
62
71
  exports.image = image;
63
72
  const addTabSpace = (text, n = 0) => {
64
73
  const tab = " ";
65
- for (let i = 0; i < n; i++)
66
- text = tab + text;
74
+ for (let i = 0; i < n; i++) {
75
+ if (text.includes("\n")) {
76
+ const multiLineText = text.split(/(?<=\n)/).join(tab);
77
+ text = tab + multiLineText;
78
+ }
79
+ else
80
+ text = tab + text;
81
+ }
67
82
  return text;
68
83
  };
69
84
  exports.addTabSpace = addTabSpace;
@@ -1 +1 @@
1
- {"version":3,"file":"md.js","sourceRoot":"","sources":["../../src/utils/md.ts"],"names":[],"mappings":";;;AAAO,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,EAAE;IACzC,OAAO,KAAK,IAAI,IAAI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,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC9C,OAAO,GAAG,MAAM,KAAK,OAAO,EAAE,CAAC;AACjC,CAAC,CAAC;AAJW,QAAA,cAAc,kBAIzB;AAEK,MAAM,YAAY,GAAG,CAAC,GAAa,EAAE,EAAE;IAC5C,OAAO,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEK,MAAM,KAAK,GAAG,CAAC,KAAiB,EAAE,EAAE;IACzC,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CACrC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,sBAAc,EAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAA,oBAAY,EAAC,GAAG,CAAC,CAC7C,CAAC;IACF,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC9B,CAAC,CAAC;AALW,QAAA,KAAK,SAKhB","sourcesContent":["export const inlineCode = (text: string) => {\n return `\\`${text}\\``;\n};\n\nexport const bold = (text: string) => {\n return `**${text}**`;\n};\n\nexport const italic = (text: string) => {\n return `_${text}_`;\n};\n\nexport const strikethrough = (text: string) => {\n return `~~${text}~~`;\n};\n\nexport const underline = (text: string) => {\n return `<u>${text}</u>`;\n};\n\nexport const link = (text: string, href: string) => {\n return `[${text}](${href})`;\n};\n\nexport const codeBlock = (text: string, language?: string) => {\n return `\\`\\`\\`${language}\n${text}\n\\`\\`\\``;\n};\n\nexport const heading1 = (text: string) => {\n return `# ${text}`;\n};\n\nexport const heading2 = (text: string) => {\n return `## ${text}`;\n};\n\nexport const heading3 = (text: string) => {\n return `### ${text}`;\n};\n\nexport const quote = (text: string) => {\n // the replace is done to handle multiple lines\n return `> ${text.replace(/\\n/g, \" \\n>\")}`;\n};\n\nexport const bullet = (text: string) => {\n return `- ${text}`;\n};\n\nexport const todo = (text: string, checked: boolean) => {\n return checked ? `- [x] ${text}` : `- [ ] ${text}`;\n};\n\nexport const image = (alt: string, href: string) => {\n return `![${alt}](${href})`;\n};\n\nexport const addTabSpace = (text: string, n = 0) => {\n const tab = \"\t\";\n for (let i = 0; i < n; i++) text = tab + text;\n return text;\n};\n\nexport const divider = () => {\n return \"---\";\n};\n\nexport const tableRowHeader = (row: string[]) => {\n let header = row.join(\"|\");\n let divider = row.map((_) => \"---\").join(\"|\");\n return `${header}\\n${divider}`;\n};\n\nexport const tableRowBody = (row: string[]) => {\n return row.join(\"|\");\n};\n\nexport const table = (cells: string[][]) => {\n const tableRows = cells.map((row, i) =>\n !i ? tableRowHeader(row) : tableRowBody(row)\n );\n return tableRows.join(\"\\n\");\n};\n"]}
1
+ {"version":3,"file":"md.js","sourceRoot":"","sources":["../../src/utils/md.ts"],"names":[],"mappings":";;;AAEO,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,EAAE;IACzC,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,UAAU,cAErB;AAEK,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE;IACnC,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEK,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,EAAE;IACrC,OAAO,IAAI,IAAI,GAAG,CAAC;AACrB,CAAC,CAAC;AAFW,QAAA,MAAM,UAEjB;AAEK,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,EAAE;IAC5C,OAAO,KAAK,IAAI,IAAI,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,aAAa,iBAExB;AAEK,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,EAAE;IACxC,OAAO,MAAM,IAAI,MAAM,CAAC;AAC1B,CAAC,CAAC;AAFW,QAAA,SAAS,aAEpB;AAEK,MAA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AG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3B,IAAI,OAAO,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC9C,OAAO,GAAG,MAAM,KAAK,OAAO,EAAE,CAAC;AACjC,CAAC,CAAC;AAJW,QAAA,cAAc,kBAIzB;AAEK,MAAM,YAAY,GAAG,CAAC,GAAa,EAAE,EAAE;IAC5C,OAAO,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC;AAFW,QAAA,YAAY,gBAEvB;AAEK,MAAM,KAAK,GAAG,CAAC,KAAiB,EAAE,EAAE;IACzC,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CACrC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,sBAAc,EAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAA,oBAAY,EAAC,GAAG,CAAC,CAC7C,CAAC;IACF,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC9B,CAAC,CAAC;AALW,QAAA,KAAK,SAKhB","sourcesContent":["import { CalloutIcon } from '../types';\n\nexport const inlineCode = (text: string) => {\n return `\\`${text}\\``;\n};\n\nexport const bold = (text: string) => {\n return `**${text}**`;\n};\n\nexport const italic = (text: string) => {\n return `_${text}_`;\n};\n\nexport const strikethrough = (text: string) => {\n return `~~${text}~~`;\n};\n\nexport const underline = (text: string) => {\n return `<u>${text}</u>`;\n};\n\nexport const link = (text: string, href: string) => {\n return `[${text}](${href})`;\n};\n\nexport const codeBlock = (text: string, language?: string) => {\n return `\\`\\`\\`${language}\n${text}\n\\`\\`\\``;\n};\n\nexport const heading1 = (text: string) => {\n return `# ${text}`;\n};\n\nexport const heading2 = (text: string) => {\n return `## ${text}`;\n};\n\nexport const heading3 = (text: string) => {\n return `### ${text}`;\n};\n\nexport const quote = (text: string) => {\n // the replace is done to handle multiple lines\n return `> ${text.replace(/\\n/g, \" \\n>\")}`;\n};\n\nexport const callout = (text: string, icon?: CalloutIcon) => {\n let emoji: string | undefined;\n if (icon?.type === 'emoji') {\n emoji = icon.emoji;\n }\n\n // the replace is done to handle multiple lines\n return `> ${emoji ? emoji + ' ' : ''}${text.replace(/\\n/g, \" \\n>\")}`;\n};\n\nexport const bullet = (text: string) => {\n return `- ${text}`;\n};\n\nexport const todo = (text: string, checked: boolean) => {\n return checked ? `- [x] ${text}` : `- [ ] ${text}`;\n};\n\nexport const image = (alt: string, href: string) => {\n return `![${alt}](${href})`;\n};\n\nexport const addTabSpace = (text: string, n = 0) => {\n const tab = \"\t\";\n for (let i = 0; i < n; i++) {\n if (text.includes(\"\\n\")) {\n const multiLineText = text.split(/(?<=\\n)/).join(tab);\n text = tab + multiLineText;\n } else text = tab + text;\n }\n return text;\n};\n\nexport const divider = () => {\n return \"---\";\n};\n\nexport const tableRowHeader = (row: string[]) => {\n let header = row.join(\"|\");\n let divider = row.map((_) => \"---\").join(\"|\");\n return `${header}\\n${divider}`;\n};\n\nexport const tableRowBody = (row: string[]) => {\n return row.join(\"|\");\n};\n\nexport const table = (cells: string[][]) => {\n const tableRows = cells.map((row, i) =>\n !i ? tableRowHeader(row) : tableRowBody(row)\n );\n return tableRows.join(\"\\n\");\n};\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=md.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"md.spec.d.ts","sourceRoot":"","sources":["../../src/utils/md.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const md_1 = require("./md");
4
+ describe('Callout', () => {
5
+ test('parses callout without emoji', () => {
6
+ const text = 'Call out text content.';
7
+ expect((0, md_1.callout)(text)).toBe(`> ${text}`);
8
+ });
9
+ test('parses callout with emoji', () => {
10
+ const text = 'Call out text content.';
11
+ expect((0, md_1.callout)(text, {
12
+ type: 'emoji',
13
+ emoji: '😍'
14
+ })).toBe(`> 😍 ${text}`);
15
+ });
16
+ });
17
+ //# sourceMappingURL=md.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"md.spec.js","sourceRoot":"","sources":["../../src/utils/md.spec.ts"],"names":[],"mappings":";;AAAA,6BAA+B;AAE/B,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE;IACrB,IAAI,CAAC,8BAA8B,EAAE,GAAG,EAAE;QACtC,MAAM,IAAI,GAAG,wBAAwB,CAAC;QAEtC,MAAM,CAAC,IAAA,YAAO,EAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,2BAA2B,EAAE,GAAG,EAAE;QACnC,MAAM,IAAI,GAAG,wBAAwB,CAAC;QAEtC,MAAM,CAAC,IAAA,YAAO,EAAC,IAAI,EAAE;YACjB,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,IAAI;SACd,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC","sourcesContent":["import { callout } from './md';\n\ndescribe('Callout', () => {\n test('parses callout without emoji', () => {\n const text = 'Call out text content.';\n\n expect(callout(text)).toBe(`> ${text}`);\n });\n\n test('parses callout with emoji', () => {\n const text = 'Call out text content.';\n\n expect(callout(text, {\n type: 'emoji',\n emoji: '😍'\n })).toBe(`> 😍 ${text}`);\n });\n});\n"]}
@@ -1,5 +1,5 @@
1
1
  import { Client } from "@notionhq/client";
2
- export declare const getBlockChildren: (notionClient: Client, block_id: string, totalPage: number) => Promise<{
2
+ export declare const getBlockChildren: (notionClient: Client, block_id: string, totalPage: number | null) => Promise<{
3
3
  object: "block";
4
4
  id: string;
5
5
  }[] | undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"notion.d.ts","sourceRoot":"","sources":["../../src/utils/notion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,eAAO,MAAM,gBAAgB,iBACb,MAAM,YACV,MAAM,aACL,MAAM;;;gBA6BlB,CAAC"}
1
+ {"version":3,"file":"notion.d.ts","sourceRoot":"","sources":["../../src/utils/notion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,eAAO,MAAM,gBAAgB,iBACb,MAAM,YACV,MAAM,aACL,MAAM,GAAG,IAAI;;;gBAwBzB,CAAC"}
@@ -4,27 +4,18 @@ exports.getBlockChildren = void 0;
4
4
  const getBlockChildren = async (notionClient, block_id, totalPage) => {
5
5
  try {
6
6
  let result = [];
7
- let start_cursor;
8
- let pageSize = 100;
9
- for (let i = 0; i < totalPage; i++) {
10
- // contain start_cursor
7
+ let pageCount = 0;
8
+ let start_cursor = undefined;
9
+ do {
11
10
  const response = (await notionClient.blocks.children.list({
12
11
  start_cursor: start_cursor,
13
- page_size: pageSize,
14
12
  block_id: block_id,
15
13
  }));
16
- let current = response.results;
17
- // delete start_cursor
18
- if (start_cursor) {
19
- current.shift();
20
- }
21
- result.push(...current);
22
- if (current.length < 100) {
23
- break;
24
- }
25
- pageSize = 101;
26
- start_cursor = current[current.length - 1].id;
27
- }
14
+ result.push(...response.results);
15
+ start_cursor = response === null || response === void 0 ? void 0 : response.next_cursor;
16
+ pageCount += 1;
17
+ } while (start_cursor != null &&
18
+ (totalPage == null || pageCount < totalPage));
28
19
  return result;
29
20
  }
30
21
  catch (e) {
@@ -1 +1 @@
1
- {"version":3,"file":"notion.js","sourceRoot":"","sources":["../../src/utils/notion.ts"],"names":[],"mappings":";;;AAGO,MAAM,gBAAgB,GAAG,KAAK,EACnC,YAAoB,EACpB,QAAgB,EAChB,SAAiB,EACjB,EAAE;IACF,IAAI;QACF,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,IAAI,YAAY,CAAC;QACjB,IAAI,QAAQ,GAAG,GAAG,CAAC;QACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE;YAClC,uBAAuB;YACvB,MAAM,QAAQ,GAAG,CAAC,MAAM,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACxD,YAAY,EAAE,YAAY;gBAC1B,SAAS,EAAE,QAAQ;gBACnB,QAAQ,EAAE,QAAQ;aACnB,CAAC,CAA8B,CAAC;YACjC,IAAI,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC;YAC/B,sBAAsB;YACtB,IAAI,YAAY,EAAE;gBAChB,OAAO,CAAC,KAAK,EAAE,CAAC;aACjB;YACD,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC;YACxB,IAAI,OAAO,CAAC,MAAM,GAAG,GAAG,EAAE;gBACxB,MAAM;aACP;YACD,QAAQ,GAAG,GAAG,CAAC;YACf,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;SAC/C;QACD,OAAO,MAAM,CAAC;KACf;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;KAChB;AACH,CAAC,CAAC;AAhCW,QAAA,gBAAgB,oBAgC3B","sourcesContent":["import { Client } from \"@notionhq/client\";\nimport { ListBlockChildrenResponse } from \"@notionhq/client/build/src/api-endpoints\";\n\nexport const getBlockChildren = async (\n notionClient: Client,\n block_id: string,\n totalPage: number\n) => {\n try {\n let result = [];\n let start_cursor;\n let pageSize = 100;\n for (let i = 0; i < totalPage; i++) {\n // contain start_cursor\n const response = (await notionClient.blocks.children.list({\n start_cursor: start_cursor,\n page_size: pageSize,\n block_id: block_id,\n })) as ListBlockChildrenResponse;\n let current = response.results;\n // delete start_cursor\n if (start_cursor) {\n current.shift();\n }\n result.push(...current);\n if (current.length < 100) {\n break;\n }\n pageSize = 101;\n start_cursor = current[current.length - 1].id;\n }\n return result;\n } catch (e) {\n console.log(e);\n }\n};\n"]}
1
+ {"version":3,"file":"notion.js","sourceRoot":"","sources":["../../src/utils/notion.ts"],"names":[],"mappings":";;;AAGO,MAAM,gBAAgB,GAAG,KAAK,EACnC,YAAoB,EACpB,QAAgB,EAChB,SAAwB,EACxB,EAAE;IACF,IAAI;QACF,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,IAAI,YAAY,GAAG,SAAS,CAAC;QAE7B,GAAG;YACD,MAAM,QAAQ,GAAG,CAAC,MAAM,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACxD,YAAY,EAAE,YAAY;gBAC1B,QAAQ,EAAE,QAAQ;aACnB,CAAC,CAA8B,CAAC;YACjC,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;YAEjC,YAAY,GAAG,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,WAAW,CAAC;YACrC,SAAS,IAAI,CAAC,CAAC;SAChB,QACC,YAAY,IAAI,IAAI;YACpB,CAAC,SAAS,IAAI,IAAI,IAAI,SAAS,GAAG,SAAS,CAAC,EAC5C;QACF,OAAO,MAAM,CAAC;KACf;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;KAChB;AACH,CAAC,CAAC;AA3BW,QAAA,gBAAgB,oBA2B3B","sourcesContent":["import { Client } from \"@notionhq/client\";\nimport { ListBlockChildrenResponse } from \"@notionhq/client/build/src/api-endpoints\";\n\nexport const getBlockChildren = async (\n notionClient: Client,\n block_id: string,\n totalPage: number | null\n) => {\n try {\n let result = [];\n let pageCount = 0;\n let start_cursor = undefined;\n\n do {\n const response = (await notionClient.blocks.children.list({\n start_cursor: start_cursor,\n block_id: block_id,\n })) as ListBlockChildrenResponse;\n result.push(...response.results);\n\n start_cursor = response?.next_cursor;\n pageCount += 1;\n } while (\n start_cursor != null &&\n (totalPage == null || pageCount < totalPage)\n );\n return result;\n } catch (e) {\n console.log(e);\n }\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notion-to-md",
3
- "version": "2.2.1",
3
+ "version": "2.3.2",
4
4
  "engines": {
5
5
  "node": ">=12"
6
6
  },
@@ -11,7 +11,8 @@
11
11
  "build": "tsc",
12
12
  "prebuild": "npm run clean",
13
13
  "clean": "rm -rf ./build",
14
- "test": "echo \"Error: no test specified\" && exit 1"
14
+ "test": "jest",
15
+ "test:watch": "jest -w"
15
16
  },
16
17
  "keywords": [
17
18
  "notion",
@@ -22,7 +23,7 @@
22
23
  "notion-to-md",
23
24
  "notion2md"
24
25
  ],
25
- "author": "",
26
+ "author": "Souvik Kar Mahapatra <souvikat001+chitchat@gmail.com>",
26
27
  "license": "ISC",
27
28
  "files": [
28
29
  "build/**"
@@ -37,7 +38,10 @@
37
38
  "homepage": "https://github.com/souvikinator/notion-to-md#readme",
38
39
  "description": "convert notion pages, block and list of blocks to markdown (supports nesting)",
39
40
  "devDependencies": {
40
- "@notionhq/client": "^0.4.12",
41
+ "@notionhq/client": "^1.0.1",
42
+ "@types/jest": "^27.4.1",
43
+ "jest": "^27.4.7",
44
+ "ts-jest": "^27.1.3",
41
45
  "typescript": "^4.5.4"
42
46
  }
43
- }
47
+ }