Skip to content

Commit 3cd3acb

Browse files
committed
Fixed missing config key and improve error handling for invalid config paths
1 parent a6d674a commit 3cd3acb

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

src/ParsedownExtended.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class ParsedownExtended extends \ParsedownExtendedParentAlias
117117
'delimiters' => [['left' => '$$', 'right' => '$$']],
118118
],
119119
],
120+
'quotes' => true,
120121
'smartypants' => [
121122
'enabled' => false,
122123
'smart_angled_quotes' => true,
@@ -468,34 +469,39 @@ protected function processLinkElement($Excerpt)
468469
$config = $this->config();
469470

470471
// Fast fail for missing config or href
471-
if (
472-
!$config->get('links') ||
473-
!$Excerpt ||
474-
empty($Excerpt['element']['attributes']['href'])
475-
) {
472+
if (!$config->get('links') || !$Excerpt || empty($Excerpt['element']['attributes']['href'])) {
476473
return null;
477474
}
478475

479476
$href = $Excerpt['element']['attributes']['href'];
480477

481478
// Only process external links if enabled
482479
if ($this->isExternalLink($href)) {
483-
$extCfg = $config->get('links.external_links');
484-
if (!$extCfg) {
480+
if (!$config->get('links.external_links')) {
485481
return null;
486482
}
487483

488484
// Only build rel if needed
489485
$rel = [];
490-
if (!empty($extCfg['nofollow'])) $rel[] = 'nofollow';
491-
if (!empty($extCfg['noopener'])) $rel[] = 'noopener';
492-
if (!empty($extCfg['noreferrer'])) $rel[] = 'noreferrer';
493486

494-
if (!empty($extCfg['open_in_new_window'])) {
487+
if ($config->get('links.external_links.nofollow')) {
488+
$rel[] = 'nofollow';
489+
}
490+
if ($config->get('links.external_links.noopener')) {
491+
$rel[] = 'noopener';
492+
}
493+
if ($config->get('links.external_links.noreferrer')) {
494+
$rel[] = 'noreferrer';
495+
}
496+
497+
if ($config->get('links.external_links.open_in_new_window')) {
495498
$Excerpt['element']['attributes']['target'] = '_blank';
496499
}
500+
497501
if ($rel) {
498-
$Excerpt['element']['attributes']['rel'] = implode(' ', $rel);
502+
$existing = $Excerpt['element']['attributes']['rel'] ?? '';
503+
$relString = trim($existing . ' ' . implode(' ', $rel));
504+
$Excerpt['element']['attributes']['rel'] = $relString;
499505
}
500506
}
501507

@@ -548,8 +554,8 @@ private function isExternalLink(string $href): bool
548554
return false;
549555
}
550556

551-
// Use static cache for internal hosts set
552-
static $internalHostsSet = null;
557+
// Use cache for internal hosts set
558+
$internalHostsSet = null;
553559
if ($internalHostsSet === null) {
554560
$internalHostsSet = [];
555561
$internalHosts = $this->config()->get('links.external_links.internal_hosts');
@@ -1866,7 +1872,7 @@ protected function blockTable($Line, $Block = null)
18661872
protected function blockAlert($Line): ?array
18671873
{
18681874
// Check if alerts are enabled in the configuration settings
1869-
if (!$this->config()->get('alerts.enabled')) {
1875+
if (!$this->config()->get('alerts')) {
18701876
return null; // Return null if alert blocks are disabled
18711877
}
18721878

@@ -3313,6 +3319,9 @@ public function bind(int &$f, array &$p): void
33133319
public function get(string $path)
33143320
{
33153321
$path = $this->normalisePath($path);
3322+
if (!isset($this->schema[$path])) {
3323+
throw new \InvalidArgumentException("Invalid config path: {$path}");
3324+
}
33163325
if (isset($this->p2b[$path])) {
33173326
return ( ($this->features & $this->p2b[$path]) !== 0 );
33183327
}
@@ -3327,6 +3336,18 @@ public function set($path, $value = null): self
33273336
return $this;
33283337
}
33293338

3339+
if (is_string($path) && is_array($value) && !isset($this->schema[$path])) {
3340+
$prefix = $path . '.';
3341+
$hasChild = false;
3342+
foreach ($this->schema as $key => $_) {
3343+
if (strpos($key, $prefix) === 0) { $hasChild = true; break; }
3344+
}
3345+
if ($hasChild) {
3346+
foreach ($value as $k => $v) { $this->set($prefix . $k, $v); }
3347+
return $this;
3348+
}
3349+
}
3350+
33303351
$path = $this->normalisePath($path);
33313352

33323353
if (!isset($this->schema[$path])) {

0 commit comments

Comments
 (0)