Added meta tags

This commit is contained in:
Filip Znachor 2024-03-09 12:09:34 +01:00
parent 0bafd71c73
commit b27a122f31
3 changed files with 48 additions and 5 deletions

View file

@ -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 {

View file

@ -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
]);
}

View file

@ -1,7 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Wiki</title>
<title>{{ meta.title }}</title>
<meta name="theme-color" content="{{ meta.color }}">
<meta name="description" content="{{ meta.desc }}">
<meta property="og:site_name" content="{{ meta.site_name }}">
<meta property="og:title" content="{{ meta.title }}">
<meta property="og:description" content="{{ meta.desc }}">
<meta property="og:image" content="{{ meta.image }}">
<meta property="twitter:site_name" content="{{ meta.site_name }}">
<meta property="twitter:title" content="{{ meta.title }}">
<meta property="twitter:description" content="{{ meta.desc }}">
<meta property="twitter:image" content="{{ meta.image }}">
<meta property="twitter:card" content="summary">
<meta name="author" content="{{ meta.author }}">
<link rel="icon" href="{{ meta.icon }}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/assets/markdown.css">
<link rel="stylesheet" href="/assets/style.css">