Skip to content

Commit c0dbff2

Browse files
committed
add bottom-border option (bottom divider)
1 parent 6e70452 commit c0dbff2

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Each link in the `links` list supports:
9191
- `text` (string, required): The text displayed for the link
9292
- `url` (string, optional): The URL the link points to (not needed if using `submenu`)
9393
- `target` (string, optional): The target attribute (e.g., `_blank` for new tab)
94+
- `bottom-border` (bool or int, optional): Add bottom divider of width 1px if true, or given value in px if number
9495
- `submenu` (list, optional): List of nested links for a submenu (see Nested Dropdowns below)
9596

9697
## Example: Using Shared Config File

USAGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Each link in the `links` list supports:
108108
| `text` | string | Yes | The link text |
109109
| `url` | string | Conditional | The target URL (can be relative or absolute). Required unless `submenu` is provided. Optional when using `submenu` to make the parent clickable. |
110110
| `target` | string | No | HTML target attribute (e.g., `_blank` for new tab) |
111+
| `bottom-border` | bool or int | No | Add bottom divider of width 1px if true, or given value in px if number |
111112
| `submenu` | list | No | List of nested links (creates a submenu). See Nested Dropdowns below. |
112113

113114
## Advanced Examples

mkdocs_header_dropdown/plugin.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,34 @@ class HeaderDropdownPlugin(BasePlugin):
3535
('dropdowns', config_options.Type(list, default=[])),
3636
)
3737

38+
def _normalize_links(self, links):
39+
"""
40+
Normalize `border-bottom` value. Default width is 1px if specified
41+
"""
42+
normalized = []
43+
for link in (links or []):
44+
if not isinstance(link, dict):
45+
normalized.append(link)
46+
continue
47+
item = dict(link) # shallow copy avoid mutating link
48+
value = item.get('border-bottom', None)
49+
style = ""
50+
width = 0
51+
if value is None:
52+
width = 0
53+
elif isinstance(value,bool):
54+
width = 1 if value else 0
55+
elif isinstance(value,str):
56+
valstr = value.lower()
57+
width = 1 if any(v in valstr for v in ['yes','true','on']) else 0
58+
elif isinstance(value,(int,float)):
59+
width = value
60+
if width>0:
61+
style += f"border-bottom: {width}px solid var(--md-default-fg-color--lightest);"
62+
item['extra_style'] = style
63+
normalized.append(item)
64+
return normalized
65+
3866
def _generate_links_from_yaml(self, data, parent_key=None):
3967
"""
4068
Recursively generate dropdown links from YAML structure.
@@ -162,6 +190,11 @@ def on_config(self, config: MkDocsConfig, **kwargs) -> MkDocsConfig:
162190
# 2. Add dropdowns from mkdocs.yml
163191
dropdowns.extend(self.config.get('dropdowns', []))
164192

193+
# Normalize link-level attributes
194+
for dropdown in dropdowns:
195+
if isinstance(dropdown, dict) and isinstance(dropdown.get('links'), list):
196+
dropdown['links'] = self._normalize_links(dropdown['links'])
197+
165198
# Add the dropdowns to extra so templates can access them
166199
config.extra['header_dropdowns'] = dropdowns
167200

mkdocs_header_dropdown/templates/partials/header-dropdown.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{# Top-level link with submenu - clickable link with arrow #}
2929
<a href="{{ link.url }}"
3030
{% if link.target %}target="{{ link.target }}"{% endif %}
31-
style="display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 1rem; color: var(--md-default-fg-color); text-decoration: none; font-size: 0.7rem;"
31+
style="display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 1rem; color: var(--md-default-fg-color); text-decoration: none; font-size: 0.7rem;{{ link.extra_style }}"
3232
onmouseover="this.style.backgroundColor='var(--md-default-fg-color--lightest)'"
3333
onmouseout="this.style.backgroundColor='transparent'">
3434
<span>{{ link.text }}</span>
@@ -38,7 +38,7 @@
3838
</a>
3939
{% else %}
4040
{# Top-level item with submenu only - not clickable #}
41-
<div style="display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 1rem; color: var(--md-default-fg-color); font-size: 0.7rem; cursor: pointer;"
41+
<div style="display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 1rem; color: var(--md-default-fg-color); font-size: 0.7rem; cursor: pointer;{{ link.extra_style }}"
4242
onmouseover="this.style.backgroundColor='var(--md-default-fg-color--lightest)'"
4343
onmouseout="this.style.backgroundColor='transparent'">
4444
<span>{{ link.text }}</span>
@@ -52,7 +52,7 @@
5252
{% for sublink in link.submenu %}
5353
<a href="{{ sublink.url }}"
5454
{% if sublink.target %}target="{{ sublink.target }}"{% endif %}
55-
style="display: block; padding: 0.5rem 1rem; color: var(--md-default-fg-color); text-decoration: none; font-size: 0.7rem"
55+
style="display: block; padding: 0.5rem 1rem; color: var(--md-default-fg-color); text-decoration: none; font-size: 0.7rem;{{ sublink.extra_style }}"
5656
onmouseover="this.style.backgroundColor='var(--md-default-fg-color--lightest)'"
5757
onmouseout="this.style.backgroundColor='transparent'">
5858
{{ sublink.text }}
@@ -63,7 +63,7 @@
6363
{% else %}
6464
<a href="{{ link.url }}"
6565
{% if link.target %}target="{{ link.target }}"{% endif %}
66-
style="display: block; padding: 0.5rem 1rem; color: var(--md-default-fg-color); text-decoration: none; font-size: 0.7rem"
66+
style="display: block; padding: 0.5rem 1rem; color: var(--md-default-fg-color); text-decoration: none; font-size: 0.7rem;{{ link.extra_style }}"
6767
onmouseover="this.style.backgroundColor='var(--md-default-fg-color--lightest)'"
6868
onmouseout="this.style.backgroundColor='transparent'">
6969
{{ link.text }}

0 commit comments

Comments
 (0)