-
Hello, I am using the insider version. I am using For example these articles named
Using:
So the callcentertags.md file would show a list like this:
Thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Thanks for suggesting. I'm not really sure I understand:
What do you mean by it? Could you maybe create a full-blown example, so that we can evaluate if it's possible to add this functionality or implement it with things we already have available? |
Beta Was this translation helpful? Give feedback.
-
I don't know if this is helpful or particularly good, but I knocked this up today with some help (ahem chat-gpt). And it covers my needs. I can list pages by tag and group them by folder. import re
from mkdocs.plugins import BasePlugin
from mkdocs.utils import normalize_url
from urllib.parse import urlsplit
class NavGenerator(BasePlugin):
# This class defines a MkDocs plugin to generate navigation links based on tags and folders.
def on_nav(self, nav, config, files):
# Called when the site navigation is being built.
self.nav = nav # The site's navigation object.
self.files = files # The list of files in the site.
def on_post_page(self, output, page, config):
# Called after a page is rendered.
# Searches for the {nav ...} pattern in the rendered HTML and replaces it with generated navigation.
match = re.search(r'\{nav(?:\s+([g])?\s*(.*))?\}', output)
if match:
group_folders = match.group(1) == 'g' # Check if folders should be grouped.
tags_to_filter = match.group(2).strip().split() if match.group(2) else page.meta.get('tags', [])
# Extract tags to filter from the match, or use page's own tags if none specified.
filtered_nav = self._format_links_by_folder(tags_to_filter, page, config, group_folders)
output = output.replace(match.group(0), filtered_nav)
# Replace the {nav ...} pattern with the generated navigation HTML.
return output
def _format_links_by_folder(self, tags_to_filter, current_page, config, group_folders):
# Generates navigation links grouped by folders.
folder_groups = {} # Dictionary to store pages grouped by folder names.
for file in self.files:
if file.page is not None and self._page_has_tags(file.page, tags_to_filter):
folder_name = self._extract_folder_name(file.page.url) if group_folders else ''
# Extract the folder name if grouping is enabled.
if folder_name not in folder_groups:
folder_groups[folder_name] = []
folder_groups[folder_name].append(file.page)
# Group pages by folder name.
result = '<div class="navtags">'
for folder, pages in folder_groups.items():
if group_folders:
result += f'<h3 class="navtagheading">{folder.capitalize()}</h3>\n' if folder else ''
# Add a heading for each folder.
result += '<ul class="navtaglist">\n'
for page in pages:
result += f'<li><a href="../../{page.url}">{page.title}</a></li>\n'
# Generate a list item for each page.
result += '</ul>\n'
result += '</div>'
return result
def _page_has_tags(self, page, tags_to_filter):
# Determines if a page has the specified tags.
if not tags_to_filter:
return True # Include all pages if no filter is specified.
page_tags = set(page.meta.get('tags', []))
# Split tags into 'any', 'all', and 'exclude' categories.
any_tags = {tag for tag in tags_to_filter if not tag.startswith('+') and not tag.startswith('-')}
all_tags = {tag.lstrip('+') for tag in tags_to_filter if tag.startswith('+')}
exclude_tags = {tag.lstrip('-') for tag in tags_to_filter if tag.startswith('-')}
# Check for tag matches in each category.
any_match = any(tag in page_tags for tag in any_tags) if any_tags else True
all_match = all(tag in page_tags for tag in all_tags)
exclude_match = not any(tag in page_tags for tag in exclude_tags)
return any_match and all_match and exclude_match
# Return True if the page matches the tag criteria.
def _extract_folder_name(self, url):
# Extracts the folder name from a page URL.
path_parts = urlsplit(url).path.strip('/').split('/')
return path_parts[0].capitalize() if path_parts else 'Root'
# Capitalize and return the folder name, default to 'Root' if no folder. |
Beta Was this translation helpful? Give feedback.
https://github.com/alanpt/mkdocs-tag-navigation-generator