Skip to content

Commit c21336d

Browse files
authored
feat: add attribute "section" to DocLink (#76)
* feat: add attr `section` to DocLink * feat: update doc for `DocLink`
1 parent cda73f7 commit c21336d

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/components/DocLink.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { getLinkToKey } from "../lib/doc-index";
33
44
interface Props {
55
src: string;
6+
section?: string;
67
}
78
8-
const { src } = Astro.props;
9+
const { src, section } = Astro.props;
910
const rawLink = await getLinkToKey(src);
1011
1112
const missing = rawLink === undefined;
12-
const link = rawLink ?? "";
13+
const link = rawLink ? rawLink + (section ? "#" + section : "") : "";
1314
---
1415

1516
{

src/content/docs/development/guide/component-docs-for-llm.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ The `DocLink` component renders an inline link to a page associated with a doc k
234234

235235
#### Props
236236

237-
- `src` (required): The doc key (e.g., `"cpp.library.utilities.move"`) or absolute path (e.g., `"/cpp/library/utilities/move"`).
237+
- `src` (required): The absolute path (e.g., `"/cpp/library/utilities/move"`).
238+
- `section` (optional): The `id` of the HTML element to jump to, typically the `id` of heading tags generated from Markdown headings.
238239

239240
#### Slots
240241

src/content/docs/development/guide/doc-everything.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,26 @@ for more information about `std::move`.
357357
Check out <DocLink src="/cpp/library/utilities/move">this page</DocLink> for more information about `std::move`.
358358
</Card>
359359

360+
By setting the `section` attribute, you can link to a specific section (or any HTML elements with corresponding `id` attribute) of a CppDoc page.
361+
362+
```mdx
363+
import DocLink from "@components/DocLink.astro";
364+
365+
<DocLink
366+
src="/cpp/library/standard_library"
367+
section="standard-library-hardening">Standard library hardening</DocLink>
368+
allows turning some instances of undefined behavior in the standard library
369+
into a contract violation.
370+
```
371+
372+
<Card title="Preview">
373+
<DocLink
374+
src="/cpp/library/standard_library"
375+
section="standard-library-hardening">Standard library hardening</DocLink>
376+
allows turning some instances of undefined behavior in the standard library
377+
into a contract violation.
378+
</Card>
379+
360380
<Aside type="tip">
361381
If the specified page does not exist yet, the `DocLink` component will render as a plain text span without a link.
362382
</Aside>

0 commit comments

Comments
 (0)