Skip to content

Commit 2867d41

Browse files
committed
Allow adding custom links to the web book navbar
1 parent 23f26de commit 2867d41

File tree

17 files changed

+95
-2
lines changed

17 files changed

+95
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and [Pydantic's HISTORY.md](https://github.com/pydantic/pydantic/blob/main/HISTORY.md), and this project *mostly* adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [UNRELEASED]
8+
9+
### Added
10+
11+
* Allow adding custom links to the web book navbar ([docs](https://hexdoc.hexxy.media/docs/guides/template)).
12+
713
## `1!0.1.0a18`
814

915
### Fixed

noxfile.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,18 @@ def dummy_setup(session: nox.Session):
507507
mod_name = "Dummy"
508508
author = "author"
509509
show_landing_text = true
510+
511+
[template.args.navbar]
512+
left = [
513+
{ text="<strong>Left</strong>", href="https://google.ca" },
514+
]
515+
center = [
516+
{ text="Center 1", href="https://google.ca", external=false },
517+
{ text="Center 2", href="https://google.ca", external=false, icon="box-arrow-down-right" },
518+
]
519+
right = [
520+
{ text="Right", href="https://google.ca", icon="box-arrow-up-left" },
521+
]
510522
"""
511523
),
512524
"nodemon.json": {

src/hexdoc/_templates/components/navbar.html.jinja

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
{% import "macros/formatting.html.jinja" as fmt with context -%}
2+
13
<nav class="navbar navbar-default navbar-static-top">
24
<div class="container">
3-
<!-- toggle and name -->
45
<div class="navbar-header">
56
<button
67
type="button"
@@ -17,9 +18,19 @@
1718
<a class="navbar-brand" href="{{ relative_site_url }}">{{ _("hexdoc."~props.modid~".title") }}</a>
1819
</div>
1920

20-
<!-- content -->
2121
<div class="collapse navbar-collapse" id="navbar-collapse">
22+
{% set navbar = navbar|default({}) %}
23+
{% if navbar.left|default(false) %}
24+
<ul class="nav navbar-nav navbar-left">
25+
{% for item in navbar.left %}
26+
{{ fmt.navbar_link(item) }}
27+
{% endfor %}
28+
</ul>
29+
{% endif %}
2230
<ul class="nav navbar-nav navbar-right">
31+
{% for item in navbar.center|default([]) %}
32+
{{ fmt.navbar_link(item) }}
33+
{% endfor %}
2334
<li class="dropdown">
2435
<a
2536
href=""
@@ -42,6 +53,9 @@
4253
>{{ lang_name }} <span class="caret"></span></a>
4354
<ul class="dropdown-menu dropdown-menu-left" id="lang-dropdown"></ul>
4455
</li>
56+
{% for item in navbar.right|default([]) %}
57+
{{ fmt.navbar_link(item) }}
58+
{% endfor %}
4559
</ul>
4660
</div>
4761
</div>

src/hexdoc/_templates/index.css.jinja

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ p.todo-note {
381381
border-radius: 6px 0 6px 6px;
382382
}
383383

384+
.external-link-icon {
385+
position: relative;
386+
bottom: 2px;
387+
margin-left: 2px;
388+
}
389+
384390
.nobr {
385391
white-space: nowrap;
386392
}

src/hexdoc/_templates/macros/formatting.html.jinja

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,20 @@
6262
<span class="collapse-recipe-hide">{{ _(base_key~".hide") }}</span>
6363
</summary>
6464
{%- endmacro %}
65+
66+
{% defaultmacro navbar_link(data) -%}
67+
{% set external = data.external|default(true) %}
68+
{% set icon = data.icon|default("box-arrow-up-right" if external else false) %}
69+
<li>
70+
<a
71+
href="{{ data.href }}"
72+
{% if external %}
73+
target="_blank"
74+
{% endif %}
75+
class="navbar-link"
76+
role="button"
77+
aria-haspopup="true"
78+
aria-expanded="false"
79+
>{{ data.text|safe }}{% if icon %} <i class="bi bi-{{ icon }} external-link-icon"></i>{% endif %}</a>
80+
</li>
81+
{%- enddefaultmacro %}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-up-right" viewBox="0 0 16 16">
2+
<path fill-rule="evenodd" d="M8.636 3.5a.5.5 0 0 0-.5-.5H1.5A1.5 1.5 0 0 0 0 4.5v10A1.5 1.5 0 0 0 1.5 16h10a1.5 1.5 0 0 0 1.5-1.5V7.864a.5.5 0 0 0-1 0V14.5a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h6.636a.5.5 0 0 0 .5-.5"/>
3+
<path fill-rule="evenodd" d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0z"/>
4+
</svg>
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import ExternalLinkIcon from "./_external_link_icon.md";
2+
3+
# Template Features
4+
5+
Some parts of the web book template can be customized by setting values in [`template.args`](pathname:///docs/api/hexdoc/core/properties.html#TemplateProps.args).
6+
7+
## Navbar
8+
9+
To add links to the navbar, add something like this to `hexdoc.toml`:
10+
11+
```toml title="doc/hexdoc.toml"
12+
[template.args.navbar]
13+
left = [
14+
{ text="<strong>Left</strong>", href="https://google.ca" },
15+
]
16+
center = [
17+
{ text="Center 1", href="https://google.ca", external=false },
18+
{ text="Center 2", href="https://google.ca", external=false, icon="box-arrow-down-right" },
19+
]
20+
right = [
21+
{ text="Right", href="https://google.ca", icon="box-arrow-up-left" },
22+
]
23+
```
24+
25+
![A screenshot of a navbar generated with the above configuration](example.png)
26+
27+
### Fields
28+
29+
|Key|Type|Description|Required?|
30+
|---|----|-----------|---------|
31+
|`text`|string/html|Text to be displayed in the navbar. May contain HTML.||
32+
|`href`|string|Link URL to open.||
33+
|`external`|boolean|If `true` (the default), this link will open in a new tab.||
34+
|`icon`|string|[Bootstrap icon id](https://icons.getbootstrap.com) to display. If `external` is `true`, defaults to <ExternalLinkIcon />.||

0 commit comments

Comments
 (0)