如何向 docusaurus 中的元素添加 id ?

发布于 2025-01-12 08:08:26 字数 244 浏览 2 评论 0原文

他们在文档中对此进行了解释,但这仅适用于标题 标题

我还读到,通常这是通过 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2025-01-19 08:08:26

要添加 docusaurus 可以链接到的随机内联锚点(带有 id 标签),而不会收到 onBrokenAnchors 警告,这就是我所做的:

  1. 我制作了一个名为 的文件>Anchor.js 带有 JSX 组件。
  2. 在该文件中,我导入并使用了 useBrokenLinks docusaurus 模块我认为它是内置于 docusaurus 中的。它使用了 useBrokenLinks 返回的对象的 .collectAnchor() 方法
  3. 我将 .md 文件更改为 .mdx 文件。
  4. .mdx 文件中,我导入了 Anchor.js 组件并使用它来创建具有 id 的元素。

/docs/components/Anchor.js:

import useBrokenLinks from '@docusaurus/useBrokenLinks';

export function Anchor(props) {
  useBrokenLinks().collectAnchor(props.id);
  return <span style={{scrollMarginTop: "var(--ifm-navbar-height)"}} {...props}/>;
}

/docs/docs_page.mdx:

import { Anchor } from "/docs/components/Anchor.js";

If you want to [read more details about this](#details).

...other content

These are <Anchor id="details">the details</Anchor>:

1. Detail 1
2. Detail 2

您可以看到我还向 Anchor 组件添加了内联样式 (style={{scrollMarginTop: "var(- -ifm-navbar-height)"}})。如果没有这种风格,每当有人访问该锚点时,链接就会被网站的导航栏掩盖。

文档现在不谈论 .collectAnchor(),只是谈论 .collectLink(),所以也许有一些理由避免 .collectAnchor() code>,但我认为它们都只是让 docusaurus 将锚点放在其锚点列表上,以便 docusaurus 跟踪内联锚点,就像跟踪标题一样。到目前为止,它对我来说效果很好。

.collectLink() 相比,我更喜欢 .collectAnchor(),原因如下:

  1. 我可以在更少的地方使用 Anchor 组件。如果我必须使用链接类型组件,我就必须在每个链接上使用它。指向这些锚点的链接比实际锚点本身还要多。
  2. Anchor 组件更容易维护,因为如果我想更改 id,我只需在一处编辑 id - 重要的地方 -然后我会收到有关任何过期链接的有用 onBrokenAnchors 警告。如果我为每个单独的链接使用 .collectLink() 组件,则链接永远不会知道某些内容已损坏。

To add a random inline anchor (with an id tag) that docusaurus can link to without getting an onBrokenAnchors warning, this is what I did:

  1. I made a file called Anchor.js with a JSX component.
  2. In that file I imported and used the useBrokenLinks docusaurus module that I think comes built-in with docusaurus. It used the .collectAnchor() method of the object that useBrokenLinks returns.
  3. I changed my .md file to a .mdx file.
  4. In the .mdx file, I imported the Anchor.js component and used that to make the elements with the id.

/docs/components/Anchor.js:

import useBrokenLinks from '@docusaurus/useBrokenLinks';

export function Anchor(props) {
  useBrokenLinks().collectAnchor(props.id);
  return <span style={{scrollMarginTop: "var(--ifm-navbar-height)"}} {...props}/>;
}

/docs/docs_page.mdx:

import { Anchor } from "/docs/components/Anchor.js";

If you want to [read more details about this](#details).

...other content

These are <Anchor id="details">the details</Anchor>:

1. Detail 1
2. Detail 2

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:

  1. I can use the 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.
  2. The Anchor component is easier to maintain because if I want to change the id I just edit the id in one place - the place that matters - and then I get useful onBrokenAnchors 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文