mirror of
https://github.com/zensical/zensical.git
synced 2026-09-24 07:15:34 +00:00
feature: add meta MkDocs plugin replacement
Signed-off-by: squidfunk <martin.donath@squidfunk.com>
This commit is contained in:
Vendored
+10
@@ -171,6 +171,16 @@ class TestPluginShimming:
|
||||
config = self._parse_yaml(tmp_path, plugins={"glightbox": {}})
|
||||
assert GlightboxExtension.name in config["markdown_extensions"]
|
||||
|
||||
def test_material_meta_plugin_is_normalized(self, tmp_path: Path) -> None:
|
||||
config = self._parse_yaml(
|
||||
tmp_path,
|
||||
plugins={"material/meta": {"meta_file": "defaults.yml"}},
|
||||
)
|
||||
assert config["plugins"]["meta"]["config"] == {
|
||||
"enabled": True,
|
||||
"meta_file": "defaults.yml",
|
||||
}
|
||||
|
||||
def test_mike_plugin_defaults_with_versioned_build(
|
||||
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
|
||||
@@ -1270,6 +1270,18 @@ def _convert_plugins(value: Any, config: dict) -> dict:
|
||||
search, "separator", '[\\s\\-_,:!=\\[\\]()\\\\"`/]+|\\.(?!\\d)', str
|
||||
)
|
||||
|
||||
# Normalize Material's meta plugin to an identifier that can be extracted
|
||||
# into the typed Rust configuration. Keep the original entry intact for
|
||||
# compatibility with consumers of the MkDocs plugin mapping.
|
||||
material_meta = plugins.get("material/meta")
|
||||
if material_meta is None:
|
||||
meta = {"enabled": False, "meta_file": ".meta.yml"}
|
||||
else:
|
||||
meta = dict(material_meta or {})
|
||||
set_default(meta, "enabled", True, bool)
|
||||
set_default(meta, "meta_file", ".meta.yml", str)
|
||||
plugins["meta"] = meta
|
||||
|
||||
# Define defaults for offline plugin
|
||||
offline = set_default(plugins, "offline", {"enabled": False}, dict)
|
||||
set_default(offline, "enabled", True, bool)
|
||||
|
||||
@@ -23,39 +23,24 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from datetime import date, datetime
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
from markdown import Markdown
|
||||
from yaml import SafeLoader
|
||||
|
||||
from zensical.config import get_config
|
||||
from zensical.extensions.autorefs import set_autorefs_page
|
||||
from zensical.extensions.context import ContextExtension, Page
|
||||
from zensical.extensions.links import LinksExtension
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Constants
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
FRONT_MATTER_RE = re.compile(
|
||||
r"^-{3}[ \r\t]*?\n(.*?\r?\n)(?:\.{3}|-{3})[ \r\t]*\n",
|
||||
re.UNICODE | re.DOTALL,
|
||||
)
|
||||
"""
|
||||
Regex pattern to extract front matter.
|
||||
"""
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Functions
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def render(content: str, path: str, url: str) -> dict:
|
||||
def render(content: str, path: str, url: str, metadata: str = "{}") -> dict:
|
||||
"""Render Markdown and return HTML.
|
||||
|
||||
This function returns rendered HTML as well as the table of contents and
|
||||
@@ -63,19 +48,10 @@ def render(content: str, path: str, url: str) -> dict:
|
||||
in order to support the specific syntax of Python Markdown. We're working
|
||||
on moving the entire rendering chain to Rust.
|
||||
"""
|
||||
# First, extract metadata - the Python Markdown parser brings a metadata
|
||||
# extension, but the implementation is broken, as it does not support full
|
||||
# YAML syntax, e.g. lists. Thus, we just parse the metadata with YAML.
|
||||
meta: dict = {}
|
||||
if match := FRONT_MATTER_RE.match(content):
|
||||
try:
|
||||
meta = yaml.load(match.group(1), SafeLoader)
|
||||
if isinstance(meta, dict):
|
||||
content = content[match.end() :].lstrip("\n")
|
||||
else:
|
||||
meta = {}
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
# Metadata inheritance and front matter are resolved in Rust before this
|
||||
# boundary. JSON keeps the call explicit and avoids reconstructing Python
|
||||
# objects one value at a time through the FFI.
|
||||
meta: dict = json.loads(metadata)
|
||||
|
||||
# Create page context and set it for autorefs.
|
||||
# We can stop setting the page if/when we vendor mkdocstrings.
|
||||
@@ -143,9 +119,6 @@ def render(content: str, path: str, url: str) -> dict:
|
||||
|
||||
|
||||
def _sanitize(value: Any) -> Any:
|
||||
# We currently don't have a null value for metadata in the Rust runtime
|
||||
if value is None:
|
||||
return ""
|
||||
if isinstance(value, (date, datetime)):
|
||||
return value.isoformat()
|
||||
if isinstance(value, dict):
|
||||
|
||||
Reference in New Issue
Block a user