Deep linking to location within md file stored in DTP

I was thinking that DTP permitted me to create a link to a specific location within a md file stored in a DTP database. This link would be accessible to other apps such as Bookends or Obsidian.

Now that I look more closely, it seems that the links I had created are links to “items” stored in a DTP database, not a deep link to a specific location within an item. In other words, I can activate that link from another app to tell DTP to open the item, but apparently not to go to a specific location within the item (the item being a markdown file).

Have I forgotten something? I read through pertinent parts of the docs and didn’t see what I was looking for. Thank you!

Afaik, that’s only possible with PDF and HTML. Perhaps, because “position” is not a concept in MD – you can inadvertently change something small like adding or removing whitespace and every position after that point changes.

PDF is a more static format and HTML knows about anchors and fragments.

1 Like

OK, thank you, chrillek. That clarifies the issue. I believe I had tricked myself into thinking I had been doing this sort of deep-linking into a md file when actually I had been linking into PDFs.

It’s also supported in case of EPUB and Markdown.

If you can find unique text you can sort of cheat to achieve that in Chrome

ePub is obvious, as that’s just HTML. For MD, are you referring to the search parameter of an item link? If not, what do you mean?

image

Documents > Markdown Documents > Item Linking

And each of the native document types has an Item Linking subsection describing any available options.

1 Like

I see. So, you can basically link to a header, and the header’s text must be unique to make that work. Better than nothing (and not very different from the search parameter, imo).

This JXA function (not a full script) lets you choose a heading in a markdown document, and returns information (heading level, id and text) about that heading.

function ChooseHeading (record) {

	/* This function lets you choose a heading in a markdown document, and returns information about the heading. Returns null if fallback. */

	const app = Application('DEVONthink 3');
	app.includeStandardAdditions = true;
	
	if (record.type() !== 'markdown') {
		return null;
	}
	
	// Gets an array of arrays: [[heading level, heading text, heading id]...]
	const headings = [...record.source().matchAll(/<[hH]([1-6]) +id="(.*?)">(.*?)<\/[hH][1-6]>/g)].map(item => {
		return {'level': item[1], 'id': item[2], 'text': item[3]}
	});
	
	if (headings.length > 0) {
	
		// Let the user choose one heading
		const defaultOption = 'Not specified';
		const list = headings.map(item => `${item.level}# ${item.text}`);
		list.push(defaultOption);
		const choice = app.chooseFromList(list, {withTitle: 'Notes', withPrompt: 'Choose a heading to append to item link', defaultItems: [defaultOption], emptySelectionAllowed: false, multipleSelectionsAllowed: false});
	
		// Retrieve heading name and id
		if (choice) {
			if (choice[0] !== defaultOption) {
				const heading = headings.find(item => `${item.level}# ${item.text}` === choice[0]);
				if (heading) return heading;
				
				/* Example of returned object:
				   {'level': 3, 'id': 'thisisaheading', 'text': 'This is a heading'} */
			}
		}
	}
	
	// Returns null if no heading is present in the document, or if user desires not to get a heading;
	return null;
}

I wrote this so I can create a link to the heading without actually opening the document. This may or may not be useful, depending on your workflow.

1 Like