souvikinator / notion-to-md

Convert notion pages, block and list of blocks to markdown (supports nesting and custom parsing)
https://www.npmjs.com/package/notion-to-md
MIT License
1.11k stars 91 forks source link

feature/generate formatted ascii tables #30

Closed victornpb closed 2 years ago

victornpb commented 2 years ago

I have a simple table in a page like this. The output does not produce a valid GitHub flavor markdown.

image

Current behavior

Markdown

Name|Description|Required|Default
\---|---|---|---
NOTION\_SECRET|Notion API Token secret|✅|
NOTION\_DATABASE|Notion Database URL or ID (example: <https://notion.so/00112233445566778899aabbccddeeff>)|✅|
FILTER\_PROP|||Status
FILTER\_VALUES|||Published
CONVERT\_PROP\_CASE|||snake
ARTICLE\_PATH|Path template to where to save markdown files||posts/{title}/README.md
ASSETS\_PATH|Path to save images, if started with . will be relative to ARTICLE\_PATH, otherwise absolute||./
PARALLEL\_PAGES|||25
PARALLEL\_DOWNLOADS\_PER\_PAGE|||3
DOWNLOAD\_IMAGE\_TIMEOUT|||30
SKIP\_DOWNLOADED\_IMAGES|Skip downloading images if they already exist in the path||true
DOWNLOAD\_FRONTMATTER\_IMAGES|Downloads image urls in the frontmatter||true

Rendered

Name|Description|Required|Default ---|---|---|--- NOTION_SECRET|Notion API Token secret|✅| NOTION_DATABASE|Notion Database URL or ID (example: https://notion.so/00112233445566778899aabbccddeeff)|✅| FILTER_PROP|||Status FILTER_VALUES|||Published CONVERT_PROP_CASE|||snake ARTICLE_PATH|Path template to where to save markdown files||posts/{title}/README.md ASSETS_PATH|Path to save images, if started with . will be relative to ARTICLE_PATH, otherwise absolute||./ PARALLEL_PAGES|||25 PARALLEL_DOWNLOADS_PER_PAGE|||3 DOWNLOAD_IMAGE_TIMEOUT|||30 SKIP_DOWNLOADED_IMAGES|Skip downloading images if they already exist in the path||true DOWNLOAD_FRONTMATTER_IMAGES|Downloads image urls in the frontmatter||true

New behavior

I added proper ascii table compatible with GitHub markdown generation, which is also readable as plaintext, as per markdown principle.

Markdown

| Name                        | Description                                                                                                                                   | Required | Default                 |
|-----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|----------|-------------------------|
| NOTION_SECRET               | Notion API Token secret                                                                                                                       | ✅        |                         |
| NOTION_DATABASE             | Notion Database URL or ID (example: [https://notion.so/00112233445566778899aabbccddeeff](https://notion.so/00112233445566778899aabbccddeeff)) | ✅        |                         |
| FILTER_PROP                 |                                                                                                                                               |          | Status                  |
| FILTER_VALUES               |                                                                                                                                               |          | Published               |
| CONVERT_PROP_CASE           |                                                                                                                                               |          | snake                   |
| ARTICLE_PATH                | Path template to where to save markdown files                                                                                                 |          | posts/{title}/README.md |
| ASSETS_PATH                 | Path to save images, if started with . will be relative to ARTICLE_PATH, otherwise absolute                                                   |          | ./                      |
| PARALLEL_PAGES              |                                                                                                                                               |          | 25                      |
| PARALLEL_DOWNLOADS_PER_PAGE |                                                                                                                                               |          | 3                       |
| DOWNLOAD_IMAGE_TIMEOUT      |                                                                                                                                               |          | 30                      |
| SKIP_DOWNLOADED_IMAGES      | Skip downloading images if they already exist in the path                                                                                     |          | true                    |
| DOWNLOAD_FRONTMATTER_IMAGES | Downloads image urls in the frontmatter                                                                                                       |          | true                    |

Rendered output

Name Description Required Default
NOTION_SECRET Notion API Token secret
NOTION_DATABASE Notion Database URL or ID (example: https://notion.so/00112233445566778899aabbccddeeff)
FILTER_PROP Status
FILTER_VALUES Published
CONVERT_PROP_CASE snake
ARTICLE_PATH Path template to where to save markdown files posts/{title}/README.md
ASSETS_PATH Path to save images, if started with . will be relative to ARTICLE_PATH, otherwise absolute ./
PARALLEL_PAGES 25
PARALLEL_DOWNLOADS_PER_PAGE 3
DOWNLOAD_IMAGE_TIMEOUT 30
SKIP_DOWNLOADED_IMAGES Skip downloading images if they already exist in the path true
DOWNLOAD_FRONTMATTER_IMAGES Downloads image urls in the frontmatter true

I need some guidance, I couldn't figure out how you generated the .js files on the side of the .ts files inside the src/ I can write some tests

souvikinator commented 2 years ago

I tried replicating the issue and the table is getting appropriately rendered, even in GitHub. Correct me if I am wrong. So this PR uses asciitables.js to generate a correctly formatted table and makes it readable in plain text format as well.

Ignore the js files inside src/, I must have made some mistake in the last build and forgot to remove those. It would be great if you can remove the js files inside the src/ folder.

victornpb commented 2 years ago

I renamed is as a feature instead, this is not really a "bugfix" anymore. Sorry for the mistake. I was parsing the output of notion-to-md with remark, and it isn't able to parse tables without an additional plugin.

I added a test case, and removed the src/*.js files.

Is up to you if you still want to merge this, I'm ok if want to keep it as is, but if you're interested in this PR let me know if you want any changes, I'll happily update my branch. Disclaimer: I’m the author of this npm package.

souvikinator commented 2 years ago

Thanks for the contribution. I'll review the code and merge it.

souvikinator commented 2 years ago

hey @victornpb, can you switch to the markdown-table package from asciitable.js and update the test file accordingly?

souvikinator commented 2 years ago

@victornpb Closing this issue, I have added markdown-table as dependency and updated test files accordingly. Will create a release shortly. Thank you.