From 0833fbe16100f49ff26bfff16e5db819a7b25ba0 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Fri, 12 Apr 2024 11:44:05 +0200 Subject: [PATCH] change internal link when exporting an html note (fastmail specific) fix exception after last note --- imap2md/export-imap-notes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/imap2md/export-imap-notes.py b/imap2md/export-imap-notes.py index 656a21d..58aa238 100644 --- a/imap2md/export-imap-notes.py +++ b/imap2md/export-imap-notes.py @@ -3,6 +3,7 @@ import imaplib import email from email.header import decode_header import os +import re username = "" password = "" @@ -19,7 +20,7 @@ status, messages = imap.select("Notes", True) messages = int(messages[0]) -for i in range(messages, messages-N, -1): +for i in range(messages, max(0, messages-N), -1): res, msg = imap.fetch(str(i), "(RFC822)") for response in msg: if isinstance(response, tuple): @@ -59,6 +60,8 @@ for i in range(messages, messages-N, -1): md = markdownify(body) if md[:4] == 'html': md = md[4:] + # fastmail specific: replace internal links + md = re.sub(r'\[(.*)\]\(https://app.fastmail.com.*\)', '[[\\1]]', md) open(filepath, "wb").write(md.replace('\r\n', '\n').encode('utf8')) imap.close()