Cobertos / md2notion

A better Notion.so Markdown importer
MIT License
657 stars 66 forks source link

Relative images not found #15

Closed mattstratford closed 4 years ago

mattstratford commented 4 years ago

Thanks for writing this script. I can successfully import text Markdown files with it. However, in testing this it's failing to locate relative images.

I have a directory structure on Mac ~/Desktop/test/ where the contents of test are a file Favorite tweet – PizzoniGiada – June 17, 2020 at 0901AM.md and a folder Favorite tweet – PizzoniGiada – June 17, 2020 at 0901AM which contains a single image image_1.jpeg.

Running using Python 3.8.3 and the latest version of md2notion installed via pip3 install md2notion, I am getting the following:

ELSLABM-155961:Desktop stratfordm$ python3 -m md2notion {token_2} https://www.notion.so/Test-0192acd694d443ec924a2a5de270c90d /Users/stratfordm/Desktop/test/Favorite\ tweet\ – PizzoniGiada\ –\ June\ 17\,\ 2020\ at\ 0901AM.md 
Initializing Notion.so client...
Getting target PageBlock...
Uploading /Users/stratfordm/Desktop/test/Favorite tweet – PizzoniGiada – June 17, 2020 at 0901AM.md to Notion.so at page Favorite tweet – PizzoniGiada – June 17, 2020 at 0901AM.md...
Uploading ImageBlock, 4/7 (57.1%)ERROR: Local image '/Users/stratfordm/Desktop/test/Favorite%20tweet%20%E2%80%93%C2%A0PizzoniGiada%20%E2%80%93%20June%2017,%202020%20at%200901AM/image_1.jpeg' not found to upload. Skipping...
Uploading TextBlock, 7/7 (100.0%)EL

The end result is a note created but without the image attachment.

Hope this helps.

Cobertos commented 4 years ago

Couple things:

Will give a look next time I'm on computer

Cobertos commented 4 years ago

Is the file path to the image inside the .md file you're trying to upload URL encoded? I can't see how it's getting that file path any other way. Relative file paths shouldn't be URL encoded though so I'm hesitant to add a URL decode. Whatever is generating those .md files probably shouldn't be URL encoding the folder name.

If fixing the thing that's generating the input .md files isn't fixable, you can use imagePathFunc= to decode your paths before upload.

from pathlib import Path
import urllib
from notion.client import NotionClient
from md2notion.upload import upload

client = NotionClient(token_v2='{token_v2}')
page = client.get_block('https://www.notion.so/Test-0192acd694d443ec924a2a5de270c90d')
def convertImagePath(imagePath, mdFilePath):
    decodedImagePath = urllib.parse.unquote(imagePath)
    return Path(mdFilePath).parent / Path(decodedImagePath)
upload(mdFile, page , imagePathFunc=convertImagePath)

What do you think?

mattstratford commented 4 years ago

Thanks for responding. Apologies, I am not anywhere close to being competent with computers… I've done just enough introductory programming courses to be dangerous.

So I tried with a new file which references an image some image.png in the same directory using a variety of syntaxes, none of which seemed to work

No dice.

So finally I thought maybe the filename itself was causing problems with spaces. Bingo. Renaming the image from some image.png to image.png solved it.

I guess I can solve my own problem by going through and renaming all the attachments? Or perhaps there's something easy on your end that can handle spaces in file names?

Anyway thanks for getting back to me.

Cobertos commented 4 years ago

Hmm, so after looking at this further, it looks like links and images with spaces are actually supposed to be interpreted as text by CommonMark

image

So my suggestion is it's probably better to rename your attachments, but for the sake of utility, it might be nice if md2notion checked the URLs for file:// syntax as well as URL decode and potentially try all three strings as paths to find a file.

Cobertos commented 4 years ago

Alright, this should now work for you in v2.1.1. We can reopen if it doesn't

mattstratford commented 4 years ago

Thanks! I'll take a look presently…