From d3974b59cb6519081ac0630a76611b39c1b1512e Mon Sep 17 00:00:00 2001 From: Knah Tsaeb Date: Wed, 14 Oct 2020 12:18:12 +0200 Subject: [PATCH] Add browser lazy loading --- apps/apps.php | 36 ++++++++++++++++++++++++++++++++++++ index.php | 9 ++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/apps/apps.php b/apps/apps.php index 4ab5932..2abb66b 100644 --- a/apps/apps.php +++ b/apps/apps.php @@ -62,3 +62,39 @@ function makeMenu($getDir = null) } return $menu; } + +class ExtendParsedownToc extends ParsedownToc +{ + public function inlineImage($Excerpt) + { + if (!isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') { + return; + } + + $Excerpt['text'] = substr($Excerpt['text'], 1); + + $Link = $this->inlineLink($Excerpt); + + if ($Link === null) { + return; + } + + $Inline = array( + 'extent' => $Link['extent'] + 1, + 'element' => array( + 'name' => 'img', + 'attributes' => array( + 'src' => $Link['element']['attributes']['href'], + 'alt' => $Link['element']['text'], + 'loading' => 'lazy', + ), + ), + ); + + $Inline['element']['attributes'] += $Link['element']['attributes']; + + unset($Inline['element']['attributes']['href']); + + return $Inline; + } +} diff --git a/index.php b/index.php index 335fd36..c2cd24c 100644 --- a/index.php +++ b/index.php @@ -23,14 +23,13 @@ if (empty($file) || !file_exists('content/' . urldecode($dir) . '/' . urldecode( } } -$Parsedown = new ParsedownToc(); - $content = file_get_contents('content/' . urldecode($dir) . '/' . urldecode($file) . '.md'); $options = array('selectors' => array('h2', 'h3')); -$Parsedown = new ParsedownToc($options); -$body = $Parsedown->text($content); -$toc = $Parsedown->contentsList(); +$Parsedown = new ExtendParsedownToc($options); + +$body = $Parsedown->text($content); +$toc = $Parsedown->contentsList(); $body = str_replace("[ ]", '', $body); $body = str_replace("[x]", '', $body);