Copying an "item link" and pasting into a Markdown doc should create "a proper link"

Right now, if I perform a Copy Item Link and then paste it into an RTF note, I see “a proper link”, i.e. the title of the item, and a link to the underlying item.

If I do the same (i.e. pasting the copied link) into a Markdown note, I see just the raw link (x-devonthink-item://...).

I’m very happy with the enhanced “quality of life” for Markdown notes, but I hope small differences like these will be patched up soon!

We might change this in a future release but lately several users were actually unhappy with e.g. DEVONthink converting automatically rich text to Markdown when pasting. In your case the item links could e.g. also be used for transclusion or images etc.

One workaround is to Cmd-Alt-drag & drop the item into the Markdown document, another one to use the Insert Link To submenu of the contextual menu and yet another one is to use Edit > Insert > Link To… (or its shortcut).

Rich text is a veneer on top of underlying code so pasting into rich text requires different behavior.
Markdown is a raw, plain text format so pasting into it would behave differently. You didn’t copy a Markdown-compliant link when you used *Copy Item Link, so logically it would paste the raw value in plain text.

As Criss mentioned, there are already mechanisms in place to insert Markdown links, as well as the ability to roll your own via AppleScript.

1 Like

Thank you, all of these workarounds are great, and exactly what I was looking for!

You’re very welcome…

As with many things in DEVONthink, there are many ways to…


:wink: :stuck_out_tongue:

(And no offense to cat lovers :slight_smile: )

When I copy a few images “item link” and paste them into a Markdown doc, it didn’t add the ‘!’ exclamation mark. These images are thus not rendered/displayed in the markdown document. These become links.

I know if I drag these images into the Markdown doc, it will create the proper item link with ‘!’ exclamation mark. Images are displayed.

I prefer to use keyboard than mouse as it is faster. Is there any other keyboard shortcuts to achieve this?

And that’s actually intended. Images can be both inserted via dropping or pasting, no need to copy the item link.

Maybe I didn’t explain my workflow clearly. I don’t drop and paste images from outside DT. All my images are auto imported to Global Inbox.

When they are in DT, I will copy their “item link” and paste them into Markdown doc. All the images will then be moved to a central location/DB.

Will a service or quick action like below is possible to add the ‘!’ exclamation mark?

This just inserts a link (the copied item link), not an image. Therefore no exclamation mark. To insert an image, either copy & paste or drag & drop the image.

However, in this case the image might either use relative or item links. The next release will add an option to prefer item links.

1 Like

After the fact, i.e. after inserting the item link? Not so easily, I’d say. One could, of course, search for “” and then prepend an ! to each match. But who knows if the item link does really refer to an image?

You could use a script for this, along the lines of this JavaScript:

(() => {
  /* Function used in the `replaceAll` method call. 
     match = the whole string matched by the regular expression 
     start = Everything including // after the DT item link 
     uuid = the UUID part of the item link
  */
  function replacerFunction(match, start, uuid) {
     const r = app.getRecordWithUuid(uuid);
    /* If the record referred to by the UUID is an image, prepend a ! to the link*/
     if (r.type() === 'image') {
       return `!${match}`;
    } else {
    /* do not modify the link */
      return match;
    }
  }
  
  const app = Application("DEVONthink 3");
  /* Get the first of the selected records */
  const rec = app.selectedRecords()[0];
  /* Get the record's text */
  txt = rec.plainText();
  /* Search for all item links _not_ beginning with an exclamation mark
    ( [^!] ), save the start of the link and the UUID in two capturing groups.
   Use the replacerFunction defined above to prepend a ! if necessary
*/
  txt = txt.replaceAll(/(?:^|[^!])(\[.*?\]\(x-devonthink-item:\/\/)(.*?)\)/gm,replacerFunction);
})()

Beware: I didn’t test this code, and it will modify your file. So try it out on a copy first!

The “cool” thing to note here is the replacer function that figures out if an item link is referring to an image or not. The regular expression in replaceAll is looking for those item links not preceded by an exclamation link: either at the beginning of a line or somewhere in the text: (?:^|[^!])means:

  • use a non-capturing group (?: … )
  • look for beginning of the line: ^
  • or: |
  • any character that is not an exclamation mark [^!];

the rest is kind of self-explanaing :wink:

Looking forward to next release.

For now, if the images are in different database as the Markdown doc, it will be using item links. If there are in the same database as the Markdown doc, it will use relative/path links.

I tried Copy the image first, then Move the image to another DB (I use a central location for all images), then Paste. The Paste didn’t work. Somehow, it is gone from the ‘clipboard’. The Paste need to happen immediately after the Copy, before the Move. Is this the expected behaviour?

Update: In order for the image(s) to stay in the ‘clipboard’, we just need to Paste it. Paste it anywhere, including external app. I tried Apple Notes and it works. Then Move the image. Then Paste in the Markdown doc. The second Paste is item link.
For now, I just Copy, Paste (relative link), Move, and Paste (item link).
I hope this is a bug (image lost from ‘clipboard’ after Move) that will be fixed.

For my current workflow, only those new images and new Markdown doc in Global Inbox are affected since there are in the same database. If I simply updated the new images (in Global Inbox) in existing Markdown doc (in other DB), the copy & paste will work just fine.

Looking for next release. It will make my workflow consistent.

Thanks. Will explore this later. Haven’t tried JavaScript in DT. Been using AppleScript in DT.

No, just a bug. Fixed for the next release.

When I click open (I know suppose to double click to open, but sometimes when you change window single click will open) the image is opened in a separate window in DT, Copy & Paste it in a Markdown doc in DT will behave as if it is importing the image. Another copy of image is created in images group (set in Preferences) and a relative path is inserted in the Markdown doc. Is this the expected behaviour or another bug?

What exactly and how did you open and afterwards copy & paste?

If I select the image from the item list, then paste it into a Markdown doc. if the images are in different database as the Markdown doc, it will be using item links. If there are in the same database as the Markdown doc, it will use relative/path links.

Say, I double click the image in the item list to open the image in a separate window in DT. Then I do a Command-C to copy of the image in the separate window. Then I go to a Markdown doc to Command-V to paste. All these done within DT.

Somehow, it treats it as if I copy the image outside DT. It creates another copy of image in images group (set in Preferences) and a relative path is inserted in the Markdown doc.

Actually the image or current selection of the image is copied, not the item and therefore the image data available on the clipboard gets imported. Copying the item in the main view should work as expected.

Ok. Got it. Thanks.