Skip to content

Commit

Permalink
feat: metadata: use mkdocs util in order to also support MultiMarkdow…
Browse files Browse the repository at this point in the history
…n style
  • Loading branch information
phil65 committed Sep 17, 2023
1 parent 6017b9d commit afe2371
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions mknodes/pages/metadata.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from __future__ import annotations

import dataclasses
import re

from typing import Self

from mkdocs.utils import meta

from mknodes.data import datatypes
from mknodes.utils import yamlhelpers


HEADER = "---\n{options}---\n"

HEADER_RE = re.compile(r"\A-{3}\n([\S\s]*)^-{3}(\n|$)", re.MULTILINE)


@dataclasses.dataclass
class Metadata:
Expand All @@ -36,21 +35,16 @@ def __post_init__(self):

@classmethod
def parse(cls, text: str) -> tuple[Self, str]:
dct = {}
if match := HEADER_RE.match(text):
content = match[1]
dct = yamlhelpers.load_yaml(content)
if hide := dct.pop("hide", None):
dct["hide_toc"] = "toc" in hide
dct["hide_nav"] = "navigation" in hide
dct["hide_path"] = "path" in hide
dct["hide_tags"] = "tags" in hide
if search := dct.pop("search", None):
dct["search_boost"] = search.get("boost")
dct["exclude_from_search"] = search.get("exclude")
text = text[match.span()[1] :]
# TODO: right now, additional metadata would lead to an exception
return cls(**dct), text
text, metadata = meta.get_data(text)
if hide := metadata.pop("hide", None):
metadata["hide_toc"] = "toc" in hide
metadata["hide_nav"] = "navigation" in hide
metadata["hide_path"] = "path" in hide
metadata["hide_tags"] = "tags" in hide
if search := metadata.pop("search", None):
metadata["search_boost"] = search.get("boost")
metadata["exclude_from_search"] = search.get("exclude")
return cls(**metadata), text

def __getitem__(self, index: str):
return dataclasses.asdict(self)[index]
Expand Down

0 comments on commit afe2371

Please sign in to comment.