Skip to content

Commit 03b09f8

Browse files
authored
Added anchor links to admonition blocks for direct referencing (JuliaDocs#2676)
1 parent 9bec14d commit 03b09f8

File tree

9 files changed

+46
-11
lines changed

9 files changed

+46
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## Unreleased
77

8+
### Changed
9+
10+
* Added anchor links to admonition blocks, making it possible to create direct links to specific admonitions. ([#2505], [#2676])
811
## Fixed
912

1013
* `@meta`, `@setup`, and `@docs` blocks no longer generate spurious entries in the search index. ([#1929], [#2675])

assets/html/scss/documenter/components/_admonition.scss

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ $admonition-border-color: () !default;
9494
// fa-circle-exclamation
9595
content: "\f06a";
9696
}
97+
98+
.admonition-anchor {
99+
opacity: 0;
100+
margin-left: 0.5em;
101+
font-size: 0.75em;
102+
color: inherit;
103+
text-decoration: none;
104+
transition: opacity 0.2s ease-in-out;
105+
106+
&:before {
107+
@include font-awesome;
108+
// fa-link
109+
content: "\f0c1";
110+
}
111+
112+
&:hover {
113+
opacity: 1 !important;
114+
text-decoration: none;
115+
}
116+
}
117+
118+
&:hover .admonition-anchor {
119+
opacity: 0.8;
120+
}
97121
}
98122

99123
details.admonition.is-details > .admonition-header {

assets/html/themes/catppuccin-frappe.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/html/themes/catppuccin-latte.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/html/themes/catppuccin-macchiato.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/html/themes/catppuccin-mocha.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/html/themes/documenter-dark.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/html/themes/documenter-light.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/html/HTMLWriter.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,16 +2393,24 @@ function domify(dctx::DCtx, node::Node, a::MarkdownAST.Admonition)
23932393
# apply a class
23942394
isempty(cat_sanitized) ? "" : ".is-category-$(cat_sanitized)"
23952395
end
2396-
2396+
node_repr = sprint(io -> show(io, node))
2397+
content_hash = bytes2hex(SHA.sha1(node_repr))[1:8]
2398+
admonition_id = if !isempty(a.title)
2399+
base_id = Documenter.slugify(a.title)
2400+
"$(base_id)-$(content_hash)"
2401+
else
2402+
"$(a.category)-$(content_hash)"
2403+
end
2404+
anchor_link = DOM.Tag(:a)[".admonition-anchor", :href => "#$(admonition_id)", :title => "Permalink"]()
23972405
inner_div = div[".admonition-body"](domify(dctx, node.children))
23982406
if a.category == "details"
23992407
# details admonitions are rendered as <details><summary> blocks
2400-
return details[".admonition.is-details"](
2401-
summary[".admonition-header"](a.title), inner_div
2408+
return details[".admonition.is-details", :id => admonition_id](
2409+
summary[".admonition-header"](a.title, anchor_link), inner_div
24022410
)
24032411
else
2404-
return div[".admonition$(colorclass)"](
2405-
header[".admonition-header"](a.title), inner_div
2412+
return div[".admonition$(colorclass)", :id => admonition_id](
2413+
header[".admonition-header"](a.title, anchor_link), inner_div
24062414
)
24072415
end
24082416
end

0 commit comments

Comments
 (0)