diff --git a/src/DataSource.php b/src/DataSource.php index 26ec8f1..9f51e32 100644 --- a/src/DataSource.php +++ b/src/DataSource.php @@ -8,9 +8,38 @@ class DataSource { $this->docs_path = realpath("../docs/"); } - public function getMarkdown(string $file): ?string { - $markdown = file_get_contents($file); - return $markdown ?? null; + private function extractFrontMatter($content) { + $frontMatter = []; + $parts = preg_split('/^---\R/m', $content, 2, PREG_SPLIT_NO_EMPTY); + if (count($parts) > 1) { + $frontMatterSection = trim($parts[0]); + $lines = explode("\n", $frontMatterSection); + foreach ($lines as $line) { + $parts = explode(':', $line, 2); + if (count($parts) === 2) { + $key = trim($parts[0]); + $value = trim($parts[1]); + $frontMatter[$key] = $value; + } + } + } + return $frontMatter; + } + + private function stripFrontMatter($content) { + $parts = preg_split('/^---\R/m', $content, 2, PREG_SPLIT_NO_EMPTY); + if (count($parts) > 1) { + return trim($parts[1]); + } + return $content; + } + + public function getMarkdown(string $file): array { + $content = file_get_contents($file); + if (!$content) return [null, []]; + $frontmatter = $this->extractFrontMatter($content); + $markdown = $this->stripFrontMatter($content); + return [$markdown, $frontmatter]; } public function getMenu(string $file): ?array { diff --git a/src/Engine.php b/src/Engine.php index 88ee68e..c182d40 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -41,10 +41,11 @@ class Engine { $data = new DataSource(); $file = $data->getFile($this->slug); $menu = $data->getMenu($file); - $markdown = $data->getMarkdown($file); + [$markdown, $meta] = $data->getMarkdown($file); if (!$markdown) return $this->template("404", ["menu" => $menu]); return $this->template("page", [ "content" => $this->parsedown->text($markdown), + "meta" => array_merge($this->config["meta"], $meta), "menu" => $menu ]); } diff --git a/view/base.twig b/view/base.twig index 750aa78..43c54dc 100644 --- a/view/base.twig +++ b/view/base.twig @@ -1,7 +1,20 @@ - Wiki + {{ meta.title }} + + + + + + + + + + + + +