How to convert markdown link to item link

DT3 provides function to convert wikilinks to item link.
I was wondering how to convert markdown link ![xxx](image/xxx.png) to item link.

Sometimes I import image to markdown document through pasting and it generate a markdown syntax link, which is much much less powerful than item link. For example, if I change the name of the image, the link just become invalid.

Both links have their advantages. Item links are more reliable, relative links are more compatible to other apps. Currently DEVONthink chooses the link automatically depending on the location of the document and the image. We might add a preference to choose the preferred style.

:+1: Cann’t wait to see the new preference!

Is there a possibility to convert image links (assets/xyz.jpg) to item-links for whole markdown documents? I have searched in the forum here and in the manual, but haven’t found such a possibility.

No, that’s not possible.

maybe this could be a potential feature for the future… :wink:? technically DTP can identify the item link of a linked file, or am I wrong? thanks a lot for considering this feature suggestion.

Possible yes but no promises.

Scripting. Find all links in the MD, filter out those that are external or already item links. For the remaining links, find the records they refer to, get their item links and replace the links with them.

I posted code here that can be used as a starting point recently. It copies images referred to in a MD to the assets group and fixes the references accordingly.

Thank you for pointing this out. unfortunately I have no scripting skills :disappointed:

One can always learn :wink:
But I’m wondering why you’d want to do that. Item links do not work outside of DT, so previewing your MD file in any other app will not work. Is that really worth the easier removal of un-referenced images?

1 Like

I started with relative links and am now thinking to change to item links, as they provide higher flexibility within devonthink (moving images from assets folder to other groups, moving a MD file to another group, without having to think which images have to be moved together with it). Therefore, knowing that there is a possibility to convert item links to relative links and viceversa would be rather useful.

That’s already been determined. But I still don’t follow: Why don’t you simply use a group for each MD file with all its images, treating them like a package that stays together? Then you don’t have to think about

1 Like

as I have multiple MD files (annotation files) in one group and all their images in one single assets folder that is generated automatically from DTP when images are imported in MD files. Sometimes I have to move such a markdown file to another group.

I am currently in the process of trying to convert relative links to item links in an existing markdown document. This conversion would allow me to relocate markdown documents to different groups in my database without worrying about the location of associated images.

As I have no experience with AppleScript, I asked ChatGPT to assist me in drafting a script. While I acknowledge that the initial outcome might not be fully functional, it could potentially serve as a starting point for someone more proficient in scripting.

My goal is to develop a smart rule that can be manually executed to trigger an AppleScript. This script would search for all relative links, such as ![Name](Assets/xyz.jpeg) , in the markdown document and convert them into item links like [Name](x-devonthink-item://ED293EBA-4AA3-444E-B444-166DBBEE7A19) . Could you provide guidance on this? Thank you very much.

tell application id "DNtp"
    try
        set theSelection to the selection
        if theSelection is {} then error "Please select some Markdown documents."
        
        repeat with thisItem in theSelection
            if type of thisItem is markdown then
                set theText to plain text of thisItem
                set newText to theText
                -- This pattern captures the links as you described
                set pattern to "(!\\[.*\\]\\()(Assets/.+\\.jpeg\\))"
                
                -- Finding all matches and converting them
                repeat with thisMatch in find text pattern returning matches
                    set assetPath to contents of thisMatch
                    set theRecords to lookup records with path assetPath
                    if theRecords is not {} then
                        set theRecord to item 1 of theRecords
                        set recordLink to reference URL of theRecord
                        set markdownLink to "![](" & recordLink & ")"
                        set newText to replaceText("(" & assetPath & ")", "(" & recordLink & ")", newText)
                    end if
                end repeat
                
                if newText is not theText then
                    set plain text of thisItem to newText
                end if
            end if
        end repeat
        
    on error error_message number error_number
        if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
    end try
end tell

-- Function to replace text
on replaceText(find, replace, text)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to find
    set textItems to text items of text
    set AppleScript's text item delimiters to replace
    set newText to textItems as string
    set AppleScript's text item delimiters to prevTIDs
    return newText
end replaceText

AppleScript does not have built-in regex support. For regex, you need to invoke AppleScript/Objective-C, or another scripting language that supports regex. You might want to use something like this toolkit by the developer of Script Debugger.

An alternative, non-regex approach is also possible, using ](Assets/ as the anchor for locating relative links.

1 Like

Good idea, using ](Assets/ would be feasible. Unfortunately, I have no apple script knowledge to write the code myself. Maybe anyone else here has already written such a code. @chrillek you mentioned something similar in this thread, but I have not found the posting you are referring to.

Or, simpler, use JavaScript, which includes regex support. Also, instead of using an “AI”, learning about scripting might be a more sustainable approach.
I for one am not going to correct the hallucinations of ChatGPT and the like.

2 Likes

This might be a starting point:

But it’s not written in AppleScript.

thank you. Indeed, for me it doesn’t matter if applescript or javascript (or any other language) would be used to solve this. I was aware that chatgpt cannot fully solve such a task, but as the learning curve for scripting to me appears quite high, I hoped to get at least an idea how such a task could be solved by its suggestion. But according to your answers I realized that this is not the case and chatgpt’s solution isn’t helpful at all.

Tools > Item Links…