Skip to content

Commit e7043e5

Browse files
authored
Merge pull request #3425 from Blargian/fix_academic_anchors
Fix links to spans in academic_overview
2 parents 7d5b53e + f479be8 commit e7043e5

File tree

2 files changed

+149
-106
lines changed

2 files changed

+149
-106
lines changed

contribute/style-guide.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,41 @@ search. E.g don't search `/docs/observability/schema-design#using-maps` but `/ob
122122
reported by the failing broken link checker, the link is most likely found in a snippet which is
123123
imported by that page. Find the snippet location from the import statement at the top of the page.
124124

125+
## Broken anchors
126+
127+
Docusaurus also has a built-in broken anchor checker. Unfortunately it sometimes
128+
can give false positives.
129+
130+
### linking to spans
131+
132+
Sometimes you want to link to something other than a header. It is logical to
133+
use a span for this purpose. For instance if you want to link to an image.
134+
135+
```
136+
<span id="page-2-0"></span><img src={image_02}/>
137+
138+
As shown by [Figure 2,](#page-2-0)...
139+
```
140+
141+
Unfortunately, docusaurus' anchor checker will throw an error on this link:
142+
143+
```response
144+
- Broken anchor on source page path = /docs/academic_overview:
145+
-> linking to #page-1-0 (resolved as: /docs/academic_overview#page-1-0)
146+
```
147+
148+
Follow the steps below for the workaround:
149+
150+
- change the file from `.md` to `.mdx`
151+
- import `useBrokenLinks` hook with `import useBrokenLinks from "@docusaurus/useBrokenLinks";`
152+
- add the following component to the page:
153+
154+
```
155+
export function Anchor(props) {
156+
useBrokenLinks().collectAnchor(props.id);
157+
return <span style={{scrollMarginTop: "var(--ifm-navbar-height)"}} {...props}/>;
158+
}
159+
```
160+
- Replace `<span id="some-id"></span>` with `Anchor id="some-id"/>`
161+
125162

0 commit comments

Comments
 (0)