I just had an “aha” moment that links together several ideas across several forum threads.
MultiMarkdown supports links to anchor tags. This can take the form of an internal link within the same markdown file (headings automatically create an anchor tag), or an external link to a standard URL with an anchor tag. However, anchor tags don’t seem to work in DEVONthink for x-callback URLs.
For example:
# Link to Headers via Anchor Tags
## Examples
With MultiMarkdown, I can do [internal links][] to other headings in the same markdown document.
If I link to an [external page](https://github.com/fletcher/MultiMarkdown-6-Syntax-Guide/#building) with ```https://url#anchor``` it will also work.
However, if I link to a markdown file's [heading](x-devonthink-item://XXXCALLBACKXXX#anchor) with an internal DEVONthink x-callback URL using an anchor tag, it doesn't work.
## Internal Links
The first item in the [Examples][] will link here.
If DEVONthink supported the MMD anchor-tag format for internal x-callback URLs, it would enable the specific “knowledge block” addressing needed for true networked knowledge management. This would open up a whole new world of workflows within DT without requiring any other change as far as I can see. And it’s already supported within MultiMarkdown.
What do you think?
Here are some of the other forum threads for reference:
Not sure I understand the need to specifically link to headers within a MD file. You can link to any line or word you want by using the x-callback url scheme. E.g.: x-devonthink-item://...?line=NUMBER or x-devonthink-item://...?search=WORD. Lines can change if you edit the document, so it is simply a matter of choosing a unique ID for any given header and link there.
Ah that’s very useful, thank you! I didn’t know about that functionality. Is it documented anywhere?
Named anchor is still a good addition I think. It uses the MultiMarkdown spec, it’s less fragile (named anchors can remain consistent in MMD even as text changes and moves). It also translates well to other systems, EG if a bunch of notes are converted to HTML. And it’s already built-in… just not for x-callbacks
Ah, ok - so in the context of this thread about Markdown, this syntax doesn’t work. So the situation remains of desiring a way to link to a specific section of a markdown document.
Using the MMD spec for linking to anchor tags via x-callback would be ideal. If not, enabling the ?search=name for markdown documents would also be a way to go. I’d prefer anchor tags but if development @cgrunenberg or anyone has a chance to make it work I’ll be happy!
I have no need to create links to headers in other MD files. EDIT: Of course, this would work for the header only if you are using edit-mode. Not sure if this is why you’re asking. (One could always simply place the id in the following line, if one prefers to use preview-mode).
It’s working for you? In my testing with markdown files clicking a link with this syntax loads the referenced file but doesn’t go to or highlight the search term.
Are you testing on macOS or iOS? I haven’t tried it on macOS, but on iOS I get the same results (loads the document, doesn’t highlight the search term).
Testing on macOS so far. It appears to be a bug in the logic. To click the link, the document containing the link has to be in preview mode. With DT prefs set to “Markdown Display: Preview Documents” the behaviour is:
with [id: xxxx] and search term: xxxx, no result
with Header (or any text) set to xxxx and search term: xxxx, highlights the search term in the text
With DT set to “Markdown Display: Edit Documents” the behaviour is:
no result from search term, either for ID or text
By manually opening the “side-by-side” markdown view, I can get the expected behaviour where the search term is highlighted (both ID and text) but only in the “edit” side of the window.
I’d label this as a bug? It’s certainly not expected or consistent behaviour.
Did you make sure that the search term in the x-callback-url is exactly the same as the term that should be highlighted?
Notice that if you have id001 you need ?search=id001, not?search=001.
If you have funnylittlebear, you need ?search=funnylittlebear.
Spaces must be html encoded, so funny little bears should be funny%20little%20bears.
Here is a script you can use:
tell application id "DNtp"
try
set currentRecord to (content record of think window 1)
set theTerm to the selected text of think window 1 as string
set theTerm to my replaceText(theTerm, " ", "%20")
set theURL to the reference URL of currentRecord & "?search=" & theTerm
set the clipboard to theURL
end try
end tell
on replaceText(theString, old, new)
set {TID, text item delimiters} to {text item delimiters, old}
set theStringItems to text items of theString
set text item delimiters to new
set theString to theStringItems as text
set text item delimiters to TID
return theString
end replaceText
Wait a second… if you’re expecting DEVONthink to find and highlight Destination Text, it won’t as that’s not the search term. Also, it won’t highlight a hidden object in the rendered HTML.
Here it works when opening in edit mode (set by the Media preferences)…
OK interesting. I’m not getting that behaviour here - it doesn’t highlight the search term in edit mode, it loads the page in preview mode (even though the preference is set to open in edit mode)
I’m not expecting it to find the other text, just the search term