Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions notion/markdown.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import commonmark
import re
import html
from xml.dom import minidom

from commonmark.dump import prepare

Expand Down Expand Up @@ -69,16 +71,20 @@
"\u3000",
}

_NOTION_TO_MARKDOWN_MAPPER = {"i": "☃", "b": "☃☃", "s": "~~", "c": "`"}
_NOTION_TO_MARKDOWN_MAPPER = {"i": "☃", "b": "☃☃", "s": "~~", "c": "`", "e": "$$"}

FORMAT_PRECEDENCE = ["s", "b", "i", "a", "c"]
FORMAT_PRECEDENCE = ["s", "b", "i", "a", "c", "e"]


def _extract_text_and_format_from_ast(item):

if item["type"] == "html_inline":
if item.get("literal", "") == "<s>":
return "", ("s",)
if item.get("literal", "").startswith('<latex'):
elem = minidom.parseString(item.get("literal", "") + '</latex>').documentElement
equation = elem.attributes['equation'].value
return "", ("e", equation)

if item["type"] == "emph":
return item.get("literal", ""), ("i",)
Expand Down Expand Up @@ -118,6 +124,11 @@ def markdown_to_notion(markdown):
markdown = markdown.replace("~~", "<s>", 1)
markdown = markdown.replace("~~", "</s>", 1)

# commonmark doesn't support latex blocks, so we need to handle it ourselves
def handle_latex(match):
return f'<latex equation="{html.escape(match.group(0)[2:-2])}">\u204d</latex>'
markdown = re.sub(r'(?<!\\\\|\$\$)(?:\\\\)*((\$\$)+)(?!(\$\$))(.+?)(?<!(\$\$))\1(?!(\$\$))', handle_latex, markdown)

# we don't want to touch dashes, so temporarily replace them here
markdown = markdown.replace("-", "⸻")

Expand Down Expand Up @@ -148,6 +159,12 @@ def markdown_to_notion(markdown):
format.remove(("s",))
literal = ""

if item["type"] == "html_inline" and literal == "</latex>":
for f in filter(lambda f: f[0] == 'e', format):
format.remove(f)
break
literal = ""

if item["type"] == "softbreak":
literal = "\n"

Expand Down Expand Up @@ -227,7 +244,15 @@ def notion_to_markdown(notion):
if f[0] == "a":
markdown += "["

markdown += stripped
# Check wheter a format modifies the content
content_changed = False
for f in sorted_format:
if f[0] == 'e':
markdown += f[1]
content_changed = True

if not content_changed:
markdown += stripped

for f in reversed(sorted_format):
if f[0] in _NOTION_TO_MARKDOWN_MAPPER:
Expand Down