Including Linked Images with Markdown Import

I’m just switching to Joplin and importing my current notes. These are markdown with links to locally stored images. Importing the linked images as Joplin resources as far as I could make out was not supported, so the result was broken links in the notes. I didn’t find mention of any workarounds here or elsewhere, so today I modified the import to give basic support sufficient for my case.

I added the below in this location: https://github.com/laurent22/joplin/blob/master/ReactNativeClient/lib/services/InteropService_Importer_Md.js#L71

//TODO
// - only do this for links which could be valid local paths (not urls)
const noteResult = await Note.save(note, { autoTimestamp: false });
const regex = /!\[[^\]]*\]\((?<filename>.*?)(?=\"|\))?\)/g;
let match;
while ((match = regex.exec(noteResult.body)) !== null) {
    console.log("ATTEMPTING TO ATTACH FILE: ", match[1], "AT POSITION: ", match.index)
    const attachmentPath = path.resolve(path.dirname(filePath), match[1])
    const resource = await shim.createResourceFromPath(attachmentPath);
    const newNoteBody = noteResult.body.replace(match[0], Resource.markdownTag(resource))
    noteResult.body = newNoteBody
    await Note.save(noteResult, { autoTimestamp: false });
}

This has solved my problem for now.
I’d like to know if this would be a worthwhile addition to the app?
If so I can try and cover some more cases, add tests etc and make a PR.

Yes I think it would. But are there any function in markdownUtils that could be reused to parse the links? As I’d rather not add one more complicated regex to maintain to the code base, if it can be avoided.

Didn’t find anything in markdownUtils for parsing the links, but I’ve added a test, tidied up a bit, and commented the regex.
I’m getting 403 trying to push up my branch so I can make a PR - do I need to be added as a contributor?

Most github projects use fork and pull so you can create a fork of the repo, push your branch to the fork, and open a PR from there.

1 Like

Is this code in master now? I am interested.

@ecvgit Nope, got a bit distracted by Christmas :upside_down_face:
Will try to make a PR tonight.

Made a PR @laurent https://github.com/laurent22/joplin/pull/2262
FYI @ecvgit

Thank you @bart. Can’t wait for the next release with this change so that I can import all my existing markdown notes.

BTW, does the export to Markdown feature export all the embedded images as well?

@ecvgit Haven’t looked into the export myself, although this thread seems relevant Display images correctly with markdown export