From 09f76e712f427c8e47c2477f3b46dc963057f603 Mon Sep 17 00:00:00 2001 From: filesite Date: Fri, 8 Apr 2022 23:14:36 +0800 Subject: [PATCH] Markdown titles get function improved --- lib/DirScanner.php | 31 ++++++++++++++++++++++--------- www/content/Nginx.conf.md | 1 - www/css/manual.css | 5 +++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/DirScanner.php b/lib/DirScanner.php index d397288..36a766d 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -562,16 +562,29 @@ Class DirScanner { if (empty($this->scanResults[$id])) {return [];} $file = $this->scanResults[$id]; + $content = @file_get_contents($file['realpath']); + if (empty($content)) {return [];} + + # standardize line breaks + $content = str_replace(array("\r\n", "\r"), "\n", $content); + # remove surrounding line breaks + $content = trim($content, "\n"); + # split text into lines + $lines = explode("\n", $content); + $titles = []; - $content = file_get_contents($file['realpath']); - preg_match_all('/#(.+)/u', $content, $matches); - if (!empty($matches[1])) { - foreach($matches[1] as $title) { - $num = substr_count($title, '#'); - $titles[] = [ - 'name' => trim(str_replace('#', '', $title)), - 'heading' => 'h' . ($num+1), - ]; + if (!empty($lines)) { + foreach($lines as $line) { + preg_match_all('/^#(.+)/u', $line, $matches); + if (!empty($matches[1])) { + foreach($matches[1] as $title) { + $num = substr_count($title, '#'); + $titles[] = [ + 'name' => trim(str_replace('#', '', $title)), + 'heading' => 'h' . ($num+1), + ]; + } + } } } diff --git a/www/content/Nginx.conf.md b/www/content/Nginx.conf.md index 01740e2..8424ada 100644 --- a/www/content/Nginx.conf.md +++ b/www/content/Nginx.conf.md @@ -25,7 +25,6 @@ Nginx config: } # deny all other php - # location ~ \.php { deny all; } diff --git a/www/css/manual.css b/www/css/manual.css index 9b1845e..5c1a26b 100644 --- a/www/css/manual.css +++ b/www/css/manual.css @@ -13,7 +13,8 @@ .indexes h6{font-size:0.8em} .indexes a{background-color:transparent;color:#0969da;text-decoration:none} @media (max-width: 640px) { - .indexes{position:static;width:auto;padding-bottom:0.5em;margin-bottom:0.5em;border-bottom:1px solid hsla(210,18%,87%,1)} - .indexes h1{display:block;padding-left:0;padding-bottom:0.3em;text-align:center;font-size:2em;border-bottom:1px solid hsla(210,18%,87%,1)} + .indexes{position:static;width:auto;padding-bottom:0.5em;margin-bottom:0.5em} + .indexes h1{display:block;padding-left:0;padding-bottom:0.3em;text-align:center;font-size:2em} .content{margin-left:0;} + .content h1{display:none} } \ No newline at end of file