diff --git a/src/di.php b/src/di.php index 7f1fd3d17..1fcd38c23 100644 --- a/src/di.php +++ b/src/di.php @@ -11,6 +11,7 @@ declare(strict_types=1); */ use FOSSBilling\Environment; +use League\CommonMark\Extension\DefaultAttributes\DefaultAttributesExtension; use Lcharette\WebpackEncoreTwig\EntrypointsTwigExtension; use Lcharette\WebpackEncoreTwig\JsonManifest; use Lcharette\WebpackEncoreTwig\TagRenderer; @@ -854,9 +855,30 @@ $di['table_export_csv'] = $di->protect(function (string $table, string $outputNa * * @return string */ -$di['parse_markdown'] = $di->protect(function (?string $content) { - $parser = new League\CommonMark\GithubFlavoredMarkdownConverter(['html_input' => 'escape', 'allow_unsafe_links' => false, 'max_nesting_level' => 50]); +$di['parse_markdown'] = $di->protect(function (?string $content, bool $addAttributes = true) use ($di) { $content ??= ''; + $defaultAttributes = []; + + // If we are defining the default attributes, build the list and add them to the config + if ($addAttributes) { + $attributes = $di['mod_service']('theme')->getDefaultMarkdownAttributes(); + foreach ($attributes as $class => $attributes) { + $reflectionClass = new \ReflectionClass($class); + $fqcn = $reflectionClass->getName(); + $defaultAttributes[$fqcn] = $attributes; + } + } + + $parser = new League\CommonMark\GithubFlavoredMarkdownConverter([ + 'html_input' => 'escape', + 'allow_unsafe_links' => false, + 'max_nesting_level' => 50, + 'default_attributes' => $defaultAttributes, + ]); + + if ($addAttributes) { + $parser->getEnvironment()->addExtension(new DefaultAttributesExtension); + } return $parser->convert($content); }); diff --git a/src/index.php b/src/index.php index 888e83d4e..81ddac71f 100644 --- a/src/index.php +++ b/src/index.php @@ -47,9 +47,11 @@ if ($url === '/run-patcher') { $di['session']; if (strncasecmp($url, ADMIN_PREFIX, strlen(ADMIN_PREFIX)) === 0) { + define("ADMIN_AREA", true); $appUrl = str_replace(ADMIN_PREFIX, '', preg_replace('/\?.+/', '', $url)); $app = new Box_AppAdmin([], $debugBar); } else { + define("ADMIN_AREA", false); $appUrl = $url; $app = new Box_AppClient([], $debugBar); } diff --git a/src/install/install.php b/src/install/install.php index 527dcfb3d..6ee4fbfae 100644 --- a/src/install/install.php +++ b/src/install/install.php @@ -61,9 +61,9 @@ $loader->register(); $protocol = FOSSBilling\Tools::isHTTPS() ? 'https' : 'http'; // Detect if FOSSBilling is behind a proxy server -if(!empty($_SERVER['HTTP_X_FORWARDED_HOST'])){ +if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) { $host = $_SERVER['HTTP_X_FORWARDED_HOST']; -}else{ +} else { $host = $_SERVER['HTTP_HOST']; } diff --git a/src/modules/Theme/Service.php b/src/modules/Theme/Service.php index 8b3476c21..c9daa587e 100644 --- a/src/modules/Theme/Service.php +++ b/src/modules/Theme/Service.php @@ -377,7 +377,7 @@ class Service implements InjectionAwareInterface public function getCurrentRouteTheme(): string { - if ($this->isRouteAdmin()) { + if (defined("ADMIN_AREA") && ADMIN_AREA == true) { return $this->getCurrentAdminAreaTheme()['code']; } @@ -407,6 +407,27 @@ class Service implements InjectionAwareInterface return $encoreInfo; } + public function getDefaultMarkdownAttributes(): array + { + if (defined("ADMIN_AREA") && ADMIN_AREA == true) { + $config = $this->getThemeConfig(false); + } else { + $config = $this->getThemeConfig(true); + } + + if (is_array($config['markdown_attributes'])) { + $attributes = $config['markdown_attributes']; + foreach ($attributes as $class => $defaults) { + if (!class_exists($class)) { + unset($attributes[$class]); + } + } + return $attributes; + } else { + return []; + } + } + protected function getEncoreJsonPath($filename): string { return $this->getThemesPath() . $this->getCurrentRouteTheme() . DIRECTORY_SEPARATOR . 'build' . DIRECTORY_SEPARATOR . "{$filename}.json"; @@ -418,13 +439,4 @@ class Service implements InjectionAwareInterface return $config['use_admin_default_encore'] ?? false; } - - protected function isRouteAdmin() - { - if (str_starts_with($_SERVER['REQUEST_URI'] ?? $_SERVER['PATH_INFO'], ADMIN_PREFIX)) { - return true; - } - - return false; - } } diff --git a/src/themes/admin_default/manifest.json b/src/themes/admin_default/manifest.json index 952965b8b..616dfbc1c 100644 --- a/src/themes/admin_default/manifest.json +++ b/src/themes/admin_default/manifest.json @@ -1,8 +1,19 @@ { - "name": "Admin area FOSSBilling Theme", - "version": "1.0.0", - "description": "Copy this theme and create your own admin area theme", - "icon": "screenshot.jpg", - "author": "FOSSBilling", - "author_url": "https://fossbilling.org/" -} \ No newline at end of file + "name": "Admin area FOSSBilling Theme", + "version": "1.0.0", + "description": "Copy this theme and create your own admin area theme", + "icon": "screenshot.jpg", + "author": "FOSSBilling", + "author_url": "https://fossbilling.org/", + "markdown_attributes": { + "\\League\\CommonMark\\Extension\\Table\\Table": { + "class": "table table-hover" + }, + "\\League\\CommonMark\\Extension\\CommonMark\\Node\\Inline\\Image": { + "class": "img-fluid" + }, + "\\League\\CommonMark\\Extension\\CommonMark\\Node\\Block\\BlockQuote": { + "class": "blockquote" + } + } +} diff --git a/src/themes/huraga/manifest.json b/src/themes/huraga/manifest.json index 4f53fb7b9..4e1a49429 100644 --- a/src/themes/huraga/manifest.json +++ b/src/themes/huraga/manifest.json @@ -1,8 +1,19 @@ { - "name": "FOSSBilling Huraga", - "version": "1.0.0", - "description": "Default client area theme", - "icon": "screenshot.jpg", - "author": "FOSSBilling", - "author_url": "https://www.fossbilling.org/" -} \ No newline at end of file + "name": "FOSSBilling Huraga", + "version": "1.0.0", + "description": "Default client area theme", + "icon": "screenshot.jpg", + "author": "FOSSBilling", + "author_url": "https://www.fossbilling.org/", + "markdown_attributes": { + "\\League\\CommonMark\\Extension\\Table\\Table": { + "class": "table table-hover" + }, + "\\League\\CommonMark\\Extension\\CommonMark\\Node\\Inline\\Image": { + "class": "img-fluid" + }, + "\\League\\CommonMark\\Extension\\CommonMark\\Node\\Block\\BlockQuote": { + "class": "blockquote" + } + } +}