Skip to content

Commit

Permalink
Merge pull request #15 from joapuiib/feature/include_page_icons
Browse files Browse the repository at this point in the history
Feature: include page icons
  • Loading branch information
EddyLuten authored Feb 21, 2025
2 parents 02ba4fb + 2ef0cee commit 8df1bc4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 11 additions & 7 deletions alias/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ def find_anchor_by_id(anchors: list[MarkdownAnchor], anchor_id: str) -> Markdown
return None


def get_page_title(page_src: str, meta_data: dict):
def get_page_title(page_src: str, meta_data: dict, include_icon: bool = False):
"""Returns the title of the page. The title in the meta data section
will take precedence over the H1 markdown title if both are provided."""
return (
meta_data['title']
if 'title' in meta_data and isinstance(meta_data['title'], str)
else get_markdown_title(page_src)
)
if 'title' in meta_data and isinstance(meta_data['title'], str):
title = meta_data['title']
if include_icon and 'icon' in meta_data and isinstance(meta_data['icon'], str):
icon = ':' + meta_data['icon'].replace('/', '-') + ':'
title = f'{icon} {title}'
else:
title = get_markdown_title(page_src)
return title


def get_alias_names(meta_data: dict):
Expand Down Expand Up @@ -154,6 +157,7 @@ class AliasPlugin(BasePlugin):
config_scheme = (
('verbose', config_options.Type(bool, default=False)),
('use_anchor_titles', config_options.Type(bool, default=False)),
('use_page_icon', config_options.Type(bool, default=False)),
)
aliases = {}
log = logging.getLogger(f'mkdocs.plugins.{__name__}')
Expand Down Expand Up @@ -225,7 +229,7 @@ def on_files(self, files: Files, /, **_):
meta_data['alias']['text']
# if meta_data['alias'] is a dictionary and 'text' is a key
if isinstance(meta_data['alias'], dict) and 'text' in meta_data['alias']
else get_page_title(source, meta_data)
else get_page_title(source, meta_data, self.config['use_page_icon'])
),
'url': file.src_uri,
'anchors': anchors,
Expand Down
14 changes: 14 additions & 0 deletions test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ def test_get_page_title_4():
assert get_page_title(source, meta) == 'The actual title'


def test_get_page_title_5():
"""Test page with both meta title and icon enabled"""
source = "# Not the title\n\nsome text\n"
meta = {'title': 'The actual title', 'icon': 'path/to/my-icon'}
assert get_page_title(source, meta, include_icon=True) == ':path-to-my-icon: The actual title'


def test_get_page_title_6():
"""Test page with both meta title and icon but disabled"""
source = "# Not the title\n\nsome text\n"
meta = {'title': 'The actual title', 'icon': 'path/to/my-icon'}
assert get_page_title(source, meta, include_icon=False) == 'The actual title'


def test_get_alias_name_1():
"""Test alias from meta extraction"""
meta = {'alias': 'my-alias'}
Expand Down

0 comments on commit 8df1bc4

Please sign in to comment.