如何向 docusaurus 中的元素添加 id ?
他们在文档中对此进行了解释,但这仅适用于标题 标题。
我还读到,通常这是通过 [Some Text](/some/text/etc){#ID}
实现的(在 vanilla md 中),但在 Docusaurus 中这似乎并非如此去工作。
They explain it on their docs, but it's only for headings headings.
I've also read, that normally this is achieved (in vanilla md) like this [Some Text](/some/text/etc){#ID}
, but in Docusaurus this doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要添加 docusaurus 可以链接到的随机内联锚点(带有
id
标签),而不会收到onBrokenAnchors
警告,这就是我所做的:的文件>Anchor.js
带有 JSX 组件。useBrokenLinks
docusaurus 模块我认为它是内置于 docusaurus 中的。它使用了useBrokenLinks
返回的对象的.collectAnchor()
方法。.md
文件更改为.mdx
文件。.mdx
文件中,我导入了Anchor.js
组件并使用它来创建具有 id 的元素。/docs/components/Anchor.js:
/docs/docs_page.mdx:
您可以看到我还向
Anchor
组件添加了内联样式 (style={{scrollMarginTop: "var(- -ifm-navbar-height)"}}
)。如果没有这种风格,每当有人访问该锚点时,链接就会被网站的导航栏掩盖。文档现在不谈论
.collectAnchor()
,只是谈论.collectLink()
,所以也许有一些理由避免.collectAnchor()
code>,但我认为它们都只是让 docusaurus 将锚点放在其锚点列表上,以便 docusaurus 跟踪内联锚点,就像跟踪标题一样。到目前为止,它对我来说效果很好。与
.collectLink()
相比,我更喜欢.collectAnchor()
,原因如下:Anchor
组件。如果我必须使用链接类型组件,我就必须在每个链接上使用它。指向这些锚点的链接比实际锚点本身还要多。Anchor
组件更容易维护,因为如果我想更改id
,我只需在一处编辑id
- 重要的地方 -然后我会收到有关任何过期链接的有用onBrokenAnchors
警告。如果我为每个单独的链接使用.collectLink()
组件,则链接永远不会知道某些内容已损坏。To add a random inline anchor (with an
id
tag) that docusaurus can link to without getting anonBrokenAnchors
warning, this is what I did:Anchor.js
with a JSX component.useBrokenLinks
docusaurus module that I think comes built-in with docusaurus. It used the.collectAnchor()
method of the object thatuseBrokenLinks
returns..md
file to a.mdx
file..mdx
file, I imported theAnchor.js
component and used that to make the elements with the id./docs/components/Anchor.js:
/docs/docs_page.mdx:
You can see I also added an inline style to the
Anchor
component (style={{scrollMarginTop: "var(--ifm-navbar-height)"}}
). Without that style the link gets covered up by the nav bar of the site whenever someone goes to that anchor.The docs do not talk about
.collectAnchor()
right now, just.collectLink()
, so maybe there is some reason to avoid.collectAnchor()
, but I think both of them just make docusaurus put the anchor on its list of anchors so that docusaurus keeps track of the inline anchor the same way it keeps track of headings. So far it has worked fine for me.I like
.collectAnchor()
better than.collectLink()
for a couple of reasons:Anchor
component in fewer places. If I had to use a link-type component, I would have to use it on every link. There are more links to those anchors than there are actual anchors themselves.Anchor
component is easier to maintain because if I want to change theid
I just edit theid
in one place - the place that matters - and then I get usefulonBrokenAnchors
warnings about any links that are out of date. If I used a.collectLink()
component for each individual link, the links would never know that something had broken.