在 Docusaurus 中,是否可以保留从文档标题创建的锚点中的大写字母?

In Docusaurus, is there can I preserve capitalization in anchors created from headings in docs?

我正在使用 Docusaurus 发布开源库 API 的文档。我的源 markdown 文件的标题对应于库中 类 的方法和属性的名称。这一切都很好。

然而,在HTML中创建的锚都是小写的。我希望他们尊重降价文件中使用的大写字母。

例如,这个降价 header:

###.doSomething()

生成以下 HTML:

<h3>
    <a aria-hidden="true" tabindex="-1" class="..." id="dosomething"></a>. 
    <code>.doSomething()</code>
    <a class="..." href="#dosomething" title="...">#</a>
</h3>

如您所见,驼峰式转换为小写。我想保持大写不变。可能吗?

P.S。 markdown 文件是根据 jsdoc 注释自动生成的。在 jsdoc 中,指向方法或属性的链接包括大写。

TL;DR

  1. 在您的项目中,导航到目录 node_modules > github-slugger

  2. 打开index.js文件;

  3. 删除if (!maintainCase) string = string.toLowerCase()

函数会变成这样:

function slugger (string, maintainCase) {
  if (typeof string !== 'string') return ''
  // if (!maintainCase) string = string.toLowerCase() <-- remove this!

  return string.trim()
    .replace(specials, '')
    .replace(emoji(), '')
    .replace(whitespace, '-')
}

重要!

  1. 如果您 update docusaurus,您可能需要再次应用此更改;

  2. 由于缓存,可能需要编辑文档才能看到更改。在这里,我编辑了 doc 文件并且它起作用了;

  3. 进行此更改后,您必须重新启动 docusaurus 服务。并且可能需要使用 npm cache clear --force;

    清除缓存
  4. 此外,我认为您应该删除文件夹 .docusaurus 以强制重建所有文档。

结果

.md 文件:

---
id: intro
---

# Random title

### WriNTinG with CaSeS

test 1

### .doAnotherThink()

test 3

### .doCamelCaseWithSeveralLETTERS()

test 3

路线图

我必须说这个很难。首先,我试图找到 anchor 标签,然后是描述。最终我找到了 github-slugger,看起来就是这样。

但是一旦我进行了更改,文档就没有任何变化!所以经过几个小时的尝试,我放弃了……然后,出于好奇,我决定通过添加第二个具有相同名称的 header 来查看 slug 函数对文档做了什么。还有——运气! — 它保留了原来的情况。

事实证明,您可以简单地使用显式 ID 来解决此问题:https://docusaurus.io/docs/next/markdown-features/headings#explicit-ids