Asciidoc:如何为锚点创建可点击的锚点

问题描述

我有一个使用 asciidoc 的 jekyll 博客站点。现在我希望在将鼠标悬停在标题上时出现一个“锚”符号,例如在 GitHub 上:

GitHub heading hover

单击符号时,浏览器应跳转标题,因此 HTML 输出应如下所示:

* checking CRAN incoming feasibility ...Warning: unable to access index for repository https://github.com/RomanHornung/drat/src/contrib:
  cannot open URL 'https://github.com/RomanHornung/drat/src/contrib/PACKAGES'

Suggests or Enhances not in mainstream repositories:
  BOLTSSIRR
Availability using Additional_repositories specification:
  BOLTSSIRR   no   ?
  ?            ?   https://github.com/RomanHornung/drat
Additional repositories with no packages:
  https://github.com/RomanHornung/drat

等效的 asciidoc 标记是:

<h2 id="whats-in-a-rainbow-table">
    <a href="#whats-in-a-rainbow-table"></a>
    What's in a Rainbow table?
</h2>

这对一些 CSS 有效且看起来不错,但我不想为每个标题编写这么多标记。有没有办法自动执行此操作?

解决方法

添加“永久链接”图标最好使用自定义 JavaScript,添加到用于生成 HTML 的主题中。

您可以使用“docinfo”文件添加自定义 JavaScript。请参阅我对 this question 的回答以了解这样做的策略。

您的自定义 JavaScript 应在页面加载时运行,搜索所有标题,创建永久链接锚点,并将其注入到标题标记中。