[Some ramblings … skip them and see below for a script ]
I suppose that’s due to the fact that for a long time URIs were ASCII-only. However, since 2005, RTF 3987 defines “Internationalized Resource Identifiers”, some details are standardized in RFC 3986 as well.
So, 中文標題
would have to be encoded as %E4%B8%AD%E6%96%87%E6%A8%99%E9%A1%8C, according to the JavaScript function encodeURI.
That would be a trivial solution to apply to the URI. I have no idea why they mangle the text part.
However, in a heading, the MD processor inserts - between words (ie replacing the spaces) in latin script text (as a convention, afaik). And I think that’s what it is trying to do for Mandarin (Japanese, Korean, perhaps Thai, Lao, Cambodian?) too. Think of 小心 (xiao xin, literally “little heart”, meaning “cautious”) which is probably always considered a single word in Mandarin. But algorithmically, it’s easier to break that down into signs and treat each of them separately. So, you get xiao-xin.
Anyway, this is more broken than it looks on the surface. Because you can specify an id attribute for your heading, like so
# 中文標題 (#中文標題)
and in the HTML, you actually do get
<h1 id="中文標題">中文標題</h1>
Looks good. Add that as a quote to the annotation and you’ll end up with
[zhong-wen-biao-ti-zhong-wen-biao-ti](x-devonthink-item://5C78D5F2-FCE1-4CB0-B98C-0BB5EAC61407?section=zhong-wen-biao-ti-zhong-wen-biao-ti)
Which is completely bonkers, because there is no fraction or section with an ID of zhong-wen-biao-ti-zhong-wen-biao-ti (that’s actually twice the original mandarin text in Pinyin). But by some kind of magic, clicking on that link in the annotations file still takes you to the headline.
After all that mumbling, perhaps a solution. If I understand you correctly, you’d want the original Mandarin text in your annotation file as the text for the link.
For that end, I propose the following script that works on the currently selected MD files (not the annotations)
(() => {
const app = Application("DEVONthink");
/* Get all currently selected records that are of type MD and have an annottion
r.annotation() is a `function` only if there is an annotation, otherwise it's an òbject`
*/
app.selectedRecords().filter(r => r.recordType() === 'markdown' && typeof r.annotation() === 'function').forEach(r => {
/* get the annotation record for r */
const anno = r.annotation();
/* get all DT item links in the annotation record that are point to a "section" in the MD file */
const links = [... anno.plainText().matchAll(/\[.*?\]\(x-devonthink-item:.*\?section=(.*)\)/g)];
/* links is now an array of matching DT item links, with each item consisting of an array
whose first element is the complete match and the second element is the header id
*/
links.forEach(l => {
/* l is an array like ['[text](x-devonthink-item://UUID?section=HEADERID)', 'HEADERID']
thus l[0] = complete reference in the form of [text](URL)
l[1] = header ID part of the link
*/
const headerID = l[1];
/* Build a regular expression looking for the header with ID `headerID`:
E.g:
/<h(\d).*id=(["'])zhong-wen-biao-ti\2.*?>(.*?)<\/h\1>/
\1 = heading level captured by (\d)
\2 = quote used around the ID, either ' or ", captured by (['"])
*/
const RE = new RegExp(`<h(\\d+).*id=(["'])${headerID}\\2.*?>(.*?)<\\/h\\1>`);
/* Search the preview version of the MD file for this header */
const match = r.source().match(RE);
/* Precaution: If the header ID is not found, do nothing */
if (! match) return;
/* match is an array with four elements like so:
- complete header (<h1 … id="HEADERID"…>TEXT</h1>
- header level (1…6)
- quote for id attribute, ' or "
- TEXT <-- this is `match[3] below
*/
/* Build the new DT item link by replacing the original header ID with the one
found in the MD preview */
newLink = l[0].replace(headerID, match[3]);
/* here, `newlink` is
'(TEXT)[x-devonthink-item://UUID?section=HEADERID]'*/
/* Update the annotation file */
anno.plainText = anno.plainText().replaceAll(l[0], newLink);
})
})
})()
I’ve only tested this with one MD file and its annotation file, where the code did do what it should There may of course be other cases where it does not.
And I still think that the MD processor should leave the original text alone.