Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/components/DocLink.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { getLinkToKey } from "../lib/doc-index";

interface Props {
src: string;
section?: string;
}

const { src } = Astro.props;
const { src, section } = Astro.props;
const rawLink = await getLinkToKey(src);

const missing = rawLink === undefined;
const link = rawLink ?? "";
const link = rawLink ? rawLink + (section ? "#" + section : "") : "";
---

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ The `DocLink` component renders an inline link to a page associated with a doc k

#### Props

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

#### Slots

Expand Down
20 changes: 20 additions & 0 deletions src/content/docs/development/guide/doc-everything.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,26 @@ for more information about `std::move`.
Check out <DocLink src="/cpp/library/utilities/move">this page</DocLink> for more information about `std::move`.
</Card>

By setting the `section` attribute, you can link to a specific section (or any HTML elements with corresponding `id` attribute) of a CppDoc page.

```mdx
import DocLink from "@components/DocLink.astro";

<DocLink
src="/cpp/library/standard_library"
section="standard-library-hardening">Standard library hardening</DocLink>
allows turning some instances of undefined behavior in the standard library
into a contract violation.
```

<Card title="Preview">
<DocLink
src="/cpp/library/standard_library"
section="standard-library-hardening">Standard library hardening</DocLink>
allows turning some instances of undefined behavior in the standard library
into a contract violation.
</Card>

<Aside type="tip">
If the specified page does not exist yet, the `DocLink` component will render as a plain text span without a link.
</Aside>
Expand Down