From eca318835ea55b00bd0761c35b3d6341b731ae10 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Mon, 19 Aug 2024 22:33:04 -0600 Subject: [PATCH] worked out some kinks in breadcrumb generation --- doc/metadata-values.txt | 5 ++++ src/dragonglass/extensions/breadcrumbs.py | 28 +++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/doc/metadata-values.txt b/doc/metadata-values.txt index d653d0b..62502f8 100644 --- a/doc/metadata-values.txt +++ b/doc/metadata-values.txt @@ -4,6 +4,11 @@ aliases (Obsidian standard metadata) List of alternative names that can be used to link to a particular page. +crumb_name + (Breadcrumbs extension) + The string to display as the title for this page when listed as a breadcrumb. + If not present, the page title is used. + description (Obsidian Publish standard metadata) The description for this page. diff --git a/src/dragonglass/extensions/breadcrumbs.py b/src/dragonglass/extensions/breadcrumbs.py index aaa87f5..5205eb5 100644 --- a/src/dragonglass/extensions/breadcrumbs.py +++ b/src/dragonglass/extensions/breadcrumbs.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -import re from typing import Any from ..extension import Extension @@ -10,8 +9,6 @@ from ..tree import SourceNode, SourceIndex class Breadcrumbs(Extension): - LINK = re.compile(r'^\[\[(.+)(?:\|.+)?\]\]') - def __init__(self, config: dict[str, Any]): """ Initialize the Breadcrumbs extension. @@ -35,9 +32,12 @@ class Breadcrumbs(Extension): if node.publish: target_str = node.metadata.get("parent", None) if target_str: - m = self.LINK.match(str(target_str)) - if m: - target_str = m.group(1) + target_str = str(target_str).strip() + if target_str[:2] == '[[' and target_str[-2:] == ']]': + target_str = target_str[2:-2].strip() + tmp = target_str.split('|') + if len(tmp) > 1: + target_str = tmp[0].strip() if target_str == '.': self._root_nodes.append(node) else: @@ -56,6 +56,17 @@ class Breadcrumbs(Extension): if target_node: self._parents[node] = target_node + def _crumb_name(self, node:SourceNode) -> str: + """ + Returns the "crumb name" for a node. + + Args: + node (SourceNode): The source node to return the "crumb name" of. + """ + if node in self._root_nodes: + return '.' + return node.metadata.get("crumb_name", node.page_title) + def supply_template_vars(self, ctxt: Any, node: SourceNode) -> dict[str, Any] | None: """ Called to supply additional template variables to the template to be rendered. @@ -68,13 +79,12 @@ class Breadcrumbs(Extension): dict[str, Any]: If not ``None``, contains additional template variables to be added before the template is rendered. """ - breadcrumbs: list[dict[str, str]] = [{"name": "." if node in self._root_nodes else node.page_title, - "link": ""}] + breadcrumbs: list[dict[str, str]] = [{"name": self._crumb_name(node), "link": ""}] cur_node = node while cur_node not in self._root_nodes: cur_node = self._parents.get(cur_node, None) if not cur_node: return None - breadcrumbs.insert(0, {"name": "." if cur_node in self._root_nodes else cur_node.page_title, + breadcrumbs.insert(0, {"name": self._crumb_name(cur_node), "link": self.link_target(ctxt, node, cur_node)}) return {"breadcrumbs": breadcrumbs}