Added support for per-theme default markdown attributes (#2016)

* Fixed admin route detection

* Added support for per-theme default MD attributes

* Restore the subfolder check

In theory my changes would fix the original issue, but I'm finding I have other errors when testing it so.. I'm not gonna worry about it
This commit is contained in:
Belle Aerni authored and GitHub committed 2023-12-28 15:57:00 -08:00
1 parent bd65fec782
commit b0e2b4041c
6 files changed
+86 -28

No files matched your search

+24 -2
View File
@@ -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);
});
+2
View File
@@ -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);
}
+2 -2
View File
@@ -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'];
}
+22 -10
View File
@@ -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;
}
}
+18 -7
View File
@@ -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/"
}
"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"
}
}
}
+18 -7
View File
@@ -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/"
}
"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"
}
}
}