edoardob90 / jrnl

Collect your thoughts and notes without leaving the command line.
GNU General Public License v3.0
0 stars 0 forks source link

Day One export: footnotes #5

Open edoardob90 opened 2 days ago

edoardob90 commented 2 days ago

Day One doesn't support footnotes. They are replaced with superscript numbers, while the content of the footnote remains at the bottom.

A valid Markdown file like:

This is a sentence with a footnote[^1].

[^1]: This is the first footnote text.

Here's some more text that simulates what a real text might look like. There might be some text without footnotes before another footnote appears. Here's another footnote[^2], indeed.

[^2]: This is the second footnote text.

(More text.)

After[^3] some[^4] text[^5], we[^6] have[^7] a[^8] footnote[^9] with two digits[^10]

[^3]: This is the second footnote text.
[^4]: This is the second footnote text.
[^5]: This is the second footnote text.
[^6]: This is the second footnote text.
[^7]: This is the second footnote text.
[^8]: This is the second footnote text.
[^9]: This is the second footnote text.
[^10]: This is the second footnote text.

Get exported as:

This is a sentence with a footnote¹.

Here's some more text that simulates what a real text might look like. There might be some text without footnotes before another footnote appears. Here's another footnote², indeed.

(More text.)

After³ some⁴ text⁵, we⁶ have⁷ a⁸ footnote⁹ with two digits¹0

¹ This is the first footnote text.
² This is the second footnote text.
³ This is the second footnote text.
⁴ This is the second footnote text.
⁵ This is the second footnote text.
⁶ This is the second footnote text.
⁷ This is the second footnote text.
⁸ This is the second footnote text.
⁹ This is the second footnote text.
¹0 This is the second footnote text.
edoardob90 commented 2 days ago

One way to fix it during import is a function like:

def convert_inline_superscripts_to_markdown(text):
    # Map superscripts
    superscript_map = {
        "¹": "1", "²": "2", "³": "3", "⁴": "4", "⁵": "5",
        "⁶": "6", "⁷": "7", "⁸": "8", "⁹": "9", "⁰": "0"
    }

    # Pattern to match footnote anchors and footnote bodies
    anchor_pattern = re.compile(r"([¹²³⁴⁵⁶⁷⁸⁹⁰])")
    body_pattern = re.compile(r"([¹²³⁴⁵⁶⁷⁸⁹⁰])\s+(.+?)(?=\s[¹²³⁴⁵⁶⁷⁸⁹⁰]|$)")

    markdown_footnotes = {}

    # Find and replace footnote bodies
    def replace_body(match):
        sup = match.group(1)
        normal_digit = superscript_map[sup]
        footnote_text = match.group(2).strip()
        markdown_footnotes[normal_digit] = footnote_text
        return ""  # Remove the inline footnote body

    # Process footnote bodies first
    text = re.sub(body_pattern, replace_body, text)

    # Replace superscript anchors in text
    def replace_anchor(match):
        sup = match.group(1)
        normal_digit = superscript_map[sup]
        return f"[^{normal_digit}]"

    text = re.sub(anchor_pattern, replace_anchor, text)

    print(markdown_footnotes)

    # Append footnote definitions in Markdown format
    for number, body in sorted(markdown_footnotes.items(), key=lambda x: int(x[0])):
        text += f"\n[^{number}]: {body}"

    return text