Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Upload PDFs and other files #47

Open
patrickplaggenborg opened this issue Oct 31, 2021 · 1 comment
Open

Upload PDFs and other files #47

patrickplaggenborg opened this issue Oct 31, 2021 · 1 comment

Comments

@patrickplaggenborg
Copy link

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
Copy link

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:

  • "Linked" files do not get uploaded -> Solution: Use "images" for PDF embeds
  • Spaces in file names seem to confuse mistletoe -> Solution: Rename files or escape spaces with %20

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)

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

No branches or pull requests

2 participants