Cobertos / md2notion

A better Notion.so Markdown importer
MIT License
654 stars 65 forks source link

Upload PDFs and other files #47

Open patrickplaggenborg opened 2 years ago

patrickplaggenborg commented 2 years ago

Right now, MD files including images are nicely uploaded as Image blocks.

However, any references to local files such as PDF's or other files are not uploaded as file. They are uploaded as a link to a block.

Links to files on my local filesystem are broken once uploaded to Notion.

Would it be possible to have it upload local files as attachment blocks, perhaps with a parameter such as --local-files?

whitebearded commented 2 years ago

Hey there! I had the same problem and was debugging it. In my case the issue could be described as the following:

I was using Markdown exports from Joplin, which seems to represent PDF like this (so actually as a Link) in Markdown: [This is a file.pdf](../_resources/This is a file.pdf)

There are two issues with this:

So changing the original markdown to this helped me: ![This is a file](../_resources/This%20is%20a%20file.pdf)

I created a small pre-processing script to go through all mkdown files in the directory and update it accordingly:


for filename in glob.glob('*.md'):
   with open(os.path.join(os.getcwd(), filename), 'r') as f: 
       mkdwn = f.read()

       while(True):
           # Find link block with local resources path and pdf file
           match = re.search(r'(?<!!)\[(.*)\]\(../_resources/(.*)\.pdf\)', mkdwn)
           if(match == None):
                break

           new_path = match.group(2).replace(" ", "%20")
           new_image_block = f"![{match.group(1)}](../_resources/{new_path}.pdf)"

           # Combine new block with surrounding markdown
           mkdwn = mkdwn[:match.span(0)[0]] + new_image_block + mkdwn[match.span(0)[1]:] 

       # Write changes
       with open(filename, "w") as fw:
        fw.write(mkdwn)