worked out some kinks in breadcrumb generation
This commit is contained in:
parent
2b5f9e9b43
commit
eca318835e
|
@ -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.
|
||||
|
|
|
@ -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}
|
||||
|
|
Loading…
Reference in New Issue
Block a user