Skip to content

Instantly share code, notes, and snippets.

@somidad
Last active March 21, 2024 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somidad/deae80a7d33601114ad9359b8779741c to your computer and use it in GitHub Desktop.
Save somidad/deae80a7d33601114ad9359b8779741c to your computer and use it in GitHub Desktop.
Enable a link to a block in a note using Obsidian GitHub Publisher

Same article on dev.to

TL;DR

Here's my configuraiton (in data.json):

    "censorText": [
      {
        "entry": "/ (\\^\\w+)$/gm",
        "replace": " <a id=\"$1\"></a>",
        "flags": "",
        "after": true
      },
      {
        "entry": "/#(\\^\\w+)\\)/gm",
        "replace": "#user-content-$1)",
        "flags": "",
        "after": true
      }
    ],

Background

What I miss

The plugin does not convert a unique block identifier ^identifier to an HTML anchor <a id="^identifier"></a>. Thus, a link to a block in a note [[note#^identifier]] or [text](note#^identifier) does not work.

1st try

I configured the plugin to replace a unique block identifier to an HTML anchor:

  • Regular expression to match: / (\^\w+)$/gm
  • Text to substitue: <a id="$1"></a>

Surprise by GitHub Markdown renderer

Later, I found that GitHub Markdown renderer adds a prefix user-content- to each HTML ID, e.g. id="^identifier" becomes id="user-content-^identifier".

2nd try

I configured the plugin to add the prefix to each link:

  • Regular expression to match: /#(\^\w+)\)/gm
  • Text to substitute: #user-content-$1)

And everything is okay now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment