worked out some kinks in breadcrumb generation

This commit is contained in:
Amy G. Bowersox 2024-08-19 22:33:04 -06:00
parent 2b5f9e9b43
commit eca318835e
2 changed files with 24 additions and 9 deletions

View File

@ -4,6 +4,11 @@ aliases
(Obsidian standard metadata) (Obsidian standard metadata)
List of alternative names that can be used to link to a particular page. 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 description
(Obsidian Publish standard metadata) (Obsidian Publish standard metadata)
The description for this page. The description for this page.

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import re
from typing import Any from typing import Any
from ..extension import Extension from ..extension import Extension
@ -10,8 +9,6 @@ from ..tree import SourceNode, SourceIndex
class Breadcrumbs(Extension): class Breadcrumbs(Extension):
LINK = re.compile(r'^\[\[(.+)(?:\|.+)?\]\]')
def __init__(self, config: dict[str, Any]): def __init__(self, config: dict[str, Any]):
""" """
Initialize the Breadcrumbs extension. Initialize the Breadcrumbs extension.
@ -35,9 +32,12 @@ class Breadcrumbs(Extension):
if node.publish: if node.publish:
target_str = node.metadata.get("parent", None) target_str = node.metadata.get("parent", None)
if target_str: if target_str:
m = self.LINK.match(str(target_str)) target_str = str(target_str).strip()
if m: if target_str[:2] == '[[' and target_str[-2:] == ']]':
target_str = m.group(1) target_str = target_str[2:-2].strip()
tmp = target_str.split('|')
if len(tmp) > 1:
target_str = tmp[0].strip()
if target_str == '.': if target_str == '.':
self._root_nodes.append(node) self._root_nodes.append(node)
else: else:
@ -56,6 +56,17 @@ class Breadcrumbs(Extension):
if target_node: if target_node:
self._parents[node] = 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: 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. 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 dict[str, Any]: If not ``None``, contains additional template variables to be added before
the template is rendered. the template is rendered.
""" """
breadcrumbs: list[dict[str, str]] = [{"name": "." if node in self._root_nodes else node.page_title, breadcrumbs: list[dict[str, str]] = [{"name": self._crumb_name(node), "link": ""}]
"link": ""}]
cur_node = node cur_node = node
while cur_node not in self._root_nodes: while cur_node not in self._root_nodes:
cur_node = self._parents.get(cur_node, None) cur_node = self._parents.get(cur_node, None)
if not cur_node: if not cur_node:
return None 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)}) "link": self.link_target(ctxt, node, cur_node)})
return {"breadcrumbs": breadcrumbs} return {"breadcrumbs": breadcrumbs}