From 67a35cdf3e7d75e0b9e8fd2095c73f28479d26cd Mon Sep 17 00:00:00 2001 From: tp_dhu Date: Wed, 30 Apr 2025 02:16:39 +0100 Subject: [PATCH] updated parsedown helper to include href --- app/extensions/ParsedownHelper.php | 71 +++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/app/extensions/ParsedownHelper.php b/app/extensions/ParsedownHelper.php index ef6ee8c..85cca6b 100644 --- a/app/extensions/ParsedownHelper.php +++ b/app/extensions/ParsedownHelper.php @@ -2,22 +2,25 @@ class ParsedownHelper extends \Prefab { - static public function render($args) { + static public function render($args) + { + + # inline #markdown here + if(isset($args['@attrib']) && isset($args['@attrib']['inline']) && + $args['@attrib']['inline'] === 'true'){ - if(isset($args['@attrib']) && $args['@attrib']['inline'] === 'true'){ - - $return = \Parsedown::instance()->text($args[0]); - - return ' -
- '.$return.' -
- '; + return self::instance()->inline($args); } - // return '
'.print_r($args,1).'
'; + # href + if(isset($args['@attrib']) && isset($args['@attrib']['href'])) + { + return self::instance()->load_href($args); + } + + # token {@variable} $content = $args[0]; $content_token = \Template::instance()->token($content); @@ -30,6 +33,52 @@ class ParsedownHelper extends \Prefab { function build($content){ return \ParsedownTableExtension::instance()->text($content); } + + + static private function inline($args){ + $return = \Parsedown::instance()->text($args[0]); + + return ' +
+ '.$return.' +
+ '; + } + + static private function load_href($args){ + $href= $args['@attrib']['href'] ?? ''; + if(!$href){ + return ''; + } + + $ui = Base::instance()->get('UI'); + $dirs = preg_split('#[;]+#', $ui, -1, PREG_SPLIT_NO_EMPTY); + + // look for the file in each UI dir + $file = ''; + foreach ($dirs as $dir) { + // normalize trailing slash + $base = rtrim($dir, '/').'/'; + // resolve relative paths + $candidate = realpath($base . $href) ?: $base . $href; + // print_r("

".$candidate . "

"); + if (is_readable($candidate)) { + $file = $candidate; + break; + } + } + if(!$file) { + return "

File not found: {$href}

"; + } + + $text = file_get_contents($file); + $md = \Parsedown::instance()->text($text); + + return ' + + '.$md.' + '; + } } \Template::instance()->extend('parsedown', 'ParsedownHelper::render'); \ No newline at end of file