Hyper¶
About¶
A component to render hyperlinks with a few bells and whistles,
implementing the Sphinx role hyper
.
Features¶
The hyper
role aims to expand the features of the traditional
Sphinx :ref:
/ {ref}
role everyone loves so much for its powerful
linking capabilities.
- Syntax Extension:
The standard role expression syntax of
{ref}
is extended, by adding a third slot for conveying additional options to Sphinx roles, building upon fundamentals of Sphinx referencing and linking. A typical markup snippet using the{hyper}
role in MyST syntax looks like this.{hyper}`label <resource> {option1=value1,option2=value2}`
The most basic variant is to just slap an arbitrary resource identifier into its value slot. That can be a Sphinx reference of any kind, or just an URL.
{hyper}`resource`
- Referencing and Linking:
The role implementation
HyperRefRole
inherits all the niceties fromAnyXRefRole
. Linking features are enhanced by providing rST / MyST cross-over compatibility, and a few other gimmicks like automatically deriving link labels from<title>
elements of HTML pages on arbitrary URLs.- Layout and Style:
Other than rendering plain HTML hyperlink anchors, the
hyper
role provides an array of options to present hyperlinks differently. Hyper currently supports:Badge
The Badge Hyper renders hyperlinks using the bdg-* roles from sphinx-design.
Button
The Button Hyper renders hyperlinks using the button directive from sphinx-design.
Card
The Card Hyper renders hyperlinks using the card directive from sphinx-design.
Shield
A Shield-typed Hyper renders hyperlinks using badges from Shields.io, building upon the shield directive from sphinx-design-elements.
Synopsis¶
A hyperlink that navigates to a URL, rendered in different variants, defined using the markup outlined below.
- Badge:
- Button:
- Card:
- Shield:
- Text:
Usage¶
:Badge:
{hyper}`https://community.panodata.org/t/technical-advancements-in-sphinx/278 {type=badge,outline=true,short-title=true}`
:Button:
{hyper}`https://community.panodata.org/t/technical-advancements-in-sphinx/278 {type=button,outline=true,short-title=true}`
:Card:
{hyper}`https://community.panodata.org/t/technical-advancements-in-sphinx/278 {type=card,short-title=true}`
:Shield:
{hyper}`https://community.panodata.org/t/technical-advancements-in-sphinx/278 {type=shield,label=Navigate to,short-title=true}`
:Text:
{hyper}`https://community.panodata.org/t/technical-advancements-in-sphinx/278 {short-title=true}`
Warning
Currently only works with MyST. reStructuredText is not supported yet, but support can be added.
Fundamentals
The hyper
role provides a few configuration options,
mimicking and expanding {ref}
.
Item
Description and Syntax
A basic hyper without any options, referencing an URL.
{hyper}`https://example.org` \
{hyper}`mailto:test@example.org`
Referencing a URL with a self-defined link label.
{hyper}`Read More <https://example.org>`
Linking and Referencing
Links to project-local references can be defined by using reference labels in both reStructuredText and MyST syntax.
{hyper}`infocard-directive` \
{hyper}`#infocard-directive`
Using a traditional intersphinx reference is the most compact way to run links to other Sphinx projects.
{hyper}`sd:sd-cards` \
{hyper}`Card Layouts <sd:sd-cards>`
MyST reference syntax is also supported for intersphinx links.
{hyper}`inv:sd:*:doc#index` \
{hyper}`inv:sd:*:label#sd-grids` \
{hyper}`inv:sd#sd-cards`
Indirect references are defined out-of-band from the link definition, for example at the end of the page.
In MyST, indirect references are defined by, for example:
[label]: reference
[label]: #reference
[label]: inv:project#reference
[label]: url
Note: This only works in MyST.
{hyper}`Grid Table <[gridtable]>` \
{hyper}`Sphinx Design <[sphinx-design]>` \
{hyper}`Example Domain <[example-org]>`
[gridtable]: gridtable-directive
[sphinx-design]: inv:sd#index
[example-org]: https://example.org/
Variants¶
The hyper
role provides a few rendering variants.
Badge¶
Adding the option {type=badge}
renders the hyperlink using a badge based
on the sphinx-design bdg-*
roles.
Options
- color
Set the color of the button (background and font). One of the semantic color names:
primary
,secondary
,success
,danger
,warning
,info
,light
,dark
,muted
. The default value isprimary
.- outline
Display button in outlined color style variant. Use
outline=true
to toggle that option.
Fundamentals
Item
Description and Syntax
A basic badge hyperlink for URLs, without any options.
{hyper}`https://example.org {type=badge}`
Use project-local references.
{hyper}`shield-directive {type=badge}` \
{hyper}`Shield Badges <shield-directive> {type=badge}`
Use intersphinx references.
{hyper}`sd:sd-cards {type=badge}` \
{hyper}`Card Layouts <sd:sd-cards> {type=badge}`
Layout
Because hyper
roles render as inline elements, like bdg-*
badges,
multiple instances can be placed side by side, even when written down
spanning multiple lines.
{hyper}`1 <https://example.org> {type=badge,outline=true}`
{hyper}`2 <https://example.org> {type=badge,outline=true}`
{hyper}`3 <https://example.org> {type=badge,outline=true}`
Tip
For purposely applying line breaks, use the backslash \
as line
continuation character.
Style
The color
option determines the color.
{hyper}`https://example.org {type=badge,color=info}`
The outline
option draws the badge in a different style.
{hyper}`https://example.org {type=badge,outline=true}`
Text input accepts Unicode glyphs.
{hyper}`🌻 Read More 🍀 <https://example.org> {type=badge,outline=true}`
Card¶
Adding the option {type=card}
renders the hyperlink using a card based
on the sphinx-design card
directive, it
accepts the same options, and the additional icon
option.
Options
The card
Hyper understands the same options like offered by the card
directive, see card options.
Item
Description and Syntax
Fundamentals
A basic card hyperlink for URLs, without any options.
{hyper}`https://example.org {type=card}`
Use project-local references.
{hyper}`shield-directive {type=card}` \
{hyper}`Shield Badges <shield-directive> {type=card}`
Use intersphinx references.
{hyper}`sd:sd-cards {type=card}` \
{hyper}`Card Layouts <sd:sd-cards> {type=card}`
Title, Header, Footer
The title
, header
, and footer
options directly translate to the
corresponding slots of the card element.
{hyper}`https://example.org {type=card,title=title,header=header,footer=footer}`
The value of the link title / label can be defined on behalf of the {ref}
syntax, as usual, or by using the content
option.
{hyper}`content <https://example.org> {type=card}` \
{hyper}`https://example.org {type=card,content=content}`
Style
Shield¶
Adding the option {type=shield}
renders the hyperlink using a badge based
on the shield
directive implementation.
Options
Please refer to the corresponding shield
options, in order
to learn about how this component can be configured.
Fundamentals
Item
Description and Syntax
A basic shield hyperlink.
{hyper}`https://example.org {type=shield}` \
{hyper}`mailto:test@example.org {type=shield}`
Using a self-defined linked label works the same way with {type=shield}
.
{hyper}`Read More <https://example.org> {type=shield}`
Link to project-local references, with link labels.
{hyper}`Info Card <infocard-directive> {type=shield}` \
{hyper}`Info Card <#infocard-directive> {type=shield}`
Using a traditional intersphinx reference is the most compact way to run links to other Sphinx projects.
{hyper}`sd:sd-cards {type=shield}` \
{hyper}`Card Layouts <sd:sd-cards> {type=shield}`
MyST reference syntax is also supported for intersphinx links.
{hyper}`inv:sd:*:doc#index {type=shield}` \
{hyper}`inv:sd:*:label#sd-grids {type=shield}` \
{hyper}`inv:sd#sd-cards {type=shield}`
Layout
A shield with an additional label, rendered on the left-hand side.
{hyper}`https://example.org {type=shield,label=Read}`
When using an additional shield label
on the left-hand side, the
text on the right-hand side can be defined through the link label,
or the message
option.
{hyper}`More <https://example.org> {type=shield,label=Read}` \
{hyper}`https://example.org {type=shield,label=Read,message=More}`
Because hyper
roles render as inline elements, multiple instances can
be placed side by side, even when written down spanning multiple lines.
{hyper}`1 <https://example.org> {type=shield}`
{hyper}`2 <https://example.org> {type=shield}`
{hyper}`3 <https://example.org> {type=shield}`
Tip
For purposely applying line breaks, use the backslash \
as line
continuation character.
Style
About defining colors, an icon, and the style/shape of the shield.
The corresponding options accept the same values like the
shield
directive.
Use the color
option to adjust the background color.
{hyper}`https://example.org {type=shield,color=darkgreen,logo=Markdown}`
Left vs. right background colors can be defined by using the label-color
vs. message-color
options, see Colors. For single-colored shields,
the color
option can be used as an alias for message-color
.
{hyper}`https://example.org {type=shield,label=Read,message=More,label-color=darkgreen,message-color=orange}` \
{hyper}`https://example.org {type=shield,label=Read,message=More,label-color=hsl(270,60%,70%),message-color=rgb(255,0,153)}` \
{hyper}`https://example.org {type=shield,message=Read More,color=#123456}`
Text input accepts Unicode glyphs.
{hyper}`https://example.org {type=shield,label=🌻 Read,message=More 🍀}`
Select an image from a collection of brand depictions, using the
logo
option.
{hyper}`CrateDB <https://cratedb.com> {type=shield,color=gray,logo=CrateDB}`
The style
option determines the style / shape of the shield.
{hyper}`https://example.org {type=shield,style=flat}` \
{hyper}`https://example.org {type=shield,style=flat-square}` \
{hyper}`https://example.org {type=shield,style=plastic}` \
{hyper}`https://example.org {type=shield,style=for-the-badge}` \
{hyper}`https://example.org {type=shield,style=social}`
Text¶
The textual output variant and its features are outlined at the Fundamentals section.
Gallery¶
A few more examples, about shortcuts and intersphinx linking.
Backlog¶
Todo
There are a few errors in here: sd:index
, sd:badges_buttons
, and
doc:badges_buttons
can’t be resolved well. See “Intersphinx across
the board”.
Todo
Link label resolution does not work yet with project-local links, e.g. infocard-directive, #infocard-directive.
Todo
rgb() / hsl() colors do not work, yet. They contain values separated by commas, which confuses the naive role option parser.
Todo
Resolving myst:syntax/admonitions
and myst:syntax/math
does not work yet,
due to ambiguity errors.
– More than one target found for ‘myst’ cross-reference [myst.xref_ambiguous]
Todo
Maybe use a different way of having shortcuts, not by using dedicated roles, but integrated into the options instead. Also, leverage shield-level style presets, as outlined in their backlog.