Skip to content

Conversation

LandonSchropp
Copy link
Contributor

@LandonSchropp LandonSchropp commented Jan 8, 2022

This pull request wholely refactors the notionBlockToMarkdown function.

Description

This code makes a few changes to the existing functionality:

  • The reduce function could be removed by relying on the recursive nature of the blocks.
  • Ensures that most block elements are always separated by two newlines. This is important to prevent them from being interpreted as a single entry, such as with back-to-back blockquotes.
  • Allows blockquotes to contain child blocks, such as lists and paragraphs.
  • Allows list items to contain child blocks, such as lists and paragraphs.

Motivation and Context

Originally, my motivation for this was that I needed a blockquote that could contain multiple paragraphs. However, I realized that my approach fixed a few other problems, so I went ahead and did the whole thing.

Screenshots

I created a test page in Notion to check the code.

Screen Shot 2022-01-07 at 8 06 15 PM

So far, this seems to be parsing well! Here's what the generated markdown looks like:

This is an example paragraph.

[This](http://example.com) `is` **another** _example_ ~~paragraph~~.

---

## Header 1

### Header 2

#### Header 3

---
- [ ]  To do item 1
- [x]  To do item 2
  
  Child of to-do item 2
- [ ]  To do item 3
  
  - [x]  Child 1 of to-do item 3
  - [ ]  Child 2 of to-do item 3

---
* Bulleted item 1
* Bulleted item 2
  
  Child of bulleted item 2
* Bulleted item 3
  
  * Child 1 of bulleted item 3
  * Child 2 of bulleted item 3

---
1. Numbered item 1
1. Numbered item 2
  
  1. Child of numbered item 2
1. Numbered item 3
  
  1. Child 1 of numbered item 3
  1. Child 2 of numbered item 3

---

<details><summary>This is a toggle.</summary>This is the toggle content</details>

---

`` javascript [Backticks edited to display correctly on GitHub]
function() {
  return "code block";
}
``


---

![Bill Murray](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/…1cd0dfe80051a2bc49a5&X-Amz-SignedHeaders=host&x-id=GetObject)

---

> I’m not superstitious, but I am a little stitious.
> 
> —Michael Scott (Steve Carrell),
> 
> _The Office_
---

<audio controls><source src="https://s3.us-west-2.amazonaws.com/secure.notion-static.com/…fe9bf0d1097063f19091&X-Amz-SignedHeaders=host&x-id=GetObject" /></audio>

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Security fix (non-breaking change wich fixes a security issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as
    expected)

This commit wholelly refactors the notionBlockToMarkdown function.
Originally, my motivation for this was that I needed blockquotes
that could contain multiple paragraphs. However, I realized that my
approach fixed a few other problems, so I went ahead and did the whole
thing.

There are a few things this code does:

* The `reduce` function could be removed by relying on the recursive
  nature of the blocks.
* Ensures that most block elements are always separated by two newlines.
  This is important to prevent them from being interpreted as a single
  entry, such as with back-to-back blockquotes.
* Allows blockquotes to contain child blocks, such as lists and
  paragraphs.
* Allows list items to contain child blocks, such as lists and
  paragraphs.
@LandonSchropp
Copy link
Contributor Author

Please let me know if there's anything else I can do. Thanks!

@orlowdev
Copy link
Owner

This is looking great! Thank you. I couldn't find the time to get back to refactoring this for half a year now.

@orlowdev orlowdev merged commit 3811cc3 into orlowdev:main Jan 11, 2022
@LandonSchropp
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants