Chinese/Japanese section links use transliterated text

:smiling_face_with_tear: I have a question about section links for Chinese and Japanese Markdown headings.

中文標題

DEVONthink generates:

x-devonthink-item://UUID?section=zhong-wen-biao-ti

The same happens with Annotation backlinks and Insert Quote.

This seems related to this older discussion:

Is this still the intended behavior? Is there any way to keep the original Chinese/Japanese characters in section= instead?

If section links have to use Latin characters, is there at least a way for the visible link text [zhong-wen-biao-ti] to display the original heading text correctly, e.g. [中文標題]?

No, there is no way to automatically produce a different item link.
Development will have to assess this.

If section links have to use Latin characters, is there at least a way for the visible link text [zhong-wen-biao-ti] to display the original heading text correctly, e.g. [中文標題]?

When used where?

I mean the link text generated automatically by Annotation / Insert Quote.

For example, when the source heading is:

### 中文標題

(中文標題 is transliterated by DEVONthink as zhong-wen-biao-ti.)

Insert Quote creates:

[zhong-wen-biao-ti](x-devonthink-item://UUID?section=zhong-wen-biao-ti)

My question is whether the visible link text could use the original heading instead:

[中文標題](x-devonthink-item://UUID?section=zhong-wen-biao-ti)

The section= part can remain transliterated if necessary.

Interesting. I wonder why they do that. Especially since leaving out the tone marks make the transliteration not very useful.

The tone marks are not really the main issue.:smiling_face_with_tear:

The problem is that Chinese, Japanese (including hiragana), and Korean headings cannot keep their original script in the generated section slug. As a result, Annotation / Insert Quote backlinks cannot display the original heading correctly.

English headings do not have this problem.:+1:

[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.

4 Likes

Thanks, this is very helpful :+1: — and the script is a clever workaround.

What I find especially interesting is using the rendered HTML to map the generated section ID back to the original heading text. I hadn’t thought of approaching it from the rendered side.

It also reminds me of {{TOC}}: DEVONthink/MultiMarkdown can already render a TOC where Chinese headings are displayed correctly and link to the corresponding heading internally. So conceptually this seems quite similar — keeping the internal target separate from the visible heading text.

I’ll test your script with more Chinese and Japanese headings. Thanks again!

That’s the only possibility I see, since the header id is not available in the MD file. And this ID is needed to identify the correct header… The really cool thing is that DT’s scripting suite provides the source attribute which gives us the rendered HTML.

It’s a regression, the next release will fix this.

What a shame. I was quite proud of that little script :wink:

3 Likes