Skip to content

Commit 2c5b925

Browse files
authored
Update Markdown.php
1 parent 095db63 commit 2c5b925

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

src/Markdown.php

+57-29
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ class Markdown extends Parsedown
9292
'h6'
9393
];
9494

95+
/**
96+
* Optional attributes for anchor links.
97+
*
98+
* @var array $linkAttributes
99+
*/
100+
private array $linkAttributes = [];
101+
95102
/**
96103
* Initialize Markdown.
97104
*/
@@ -106,7 +113,7 @@ public function __construct()
106113
/**
107114
* Set host link.
108115
*
109-
* @param string $link
116+
* @param string $link The based host URL.
110117
*
111118
* @return self
112119
*/
@@ -117,10 +124,26 @@ public function setLink(string $link): self
117124
return $this;
118125
}
119126

127+
/**
128+
* Set a default attribute for anchor links.
129+
*
130+
* @param array<string,string> $attributes The attributes for anchor links.
131+
*
132+
* @return self
133+
*/
134+
public function setLinkAttributes(array $attributes): self
135+
{
136+
$this->linkAttributes = $attributes;
137+
138+
return $this;
139+
}
140+
141+
142+
120143
/**
121144
* Set id prefix for table of contents.
122145
*
123-
* @param string $prefix
146+
* @param string $prefix The id prefix for table of contents.
124147
*
125148
* @return self
126149
*/
@@ -134,7 +157,7 @@ public function setIdPrefix(string $prefix): self
134157
/**
135158
* Set table of contents heading.
136159
*
137-
* @param array $headings
160+
* @param array $headings The supported headings for table of contents.
138161
*
139162
* @return self
140163
*/
@@ -148,7 +171,7 @@ public function setHeadings(array $headings): self
148171
/**
149172
* Enable heading anchor link.
150173
*
151-
* @param bool $anchors
174+
* @param bool $anchors The enable/disable of heading anchor link.
152175
*
153176
* @return self
154177
*/
@@ -162,7 +185,7 @@ public function headingAnchor(bool $anchors = true): self
162185
/**
163186
* Enable responsive table.
164187
*
165-
* @param bool $responsive
188+
* @param bool $responsive The enable/disable of responsive table.
166189
*
167190
* @return self
168191
*/
@@ -176,7 +199,7 @@ public function responsiveTable(bool $responsive = true): self
176199
/**
177200
* Set if tables of contents should be enabled.
178201
*
179-
* @param bool $enable
202+
* @param bool $enable The enable/disable of table of contents.
180203
*
181204
* @return self
182205
*/
@@ -191,7 +214,7 @@ public function tableOfContents(bool $enable = true): self
191214
* Set media type
192215
*
193216
* @param string $context The media context type (e.g, `audio`, `video`).
194-
* @param string $type The attribute of media type (e.g, `audio/ogg; codecs=opus`).
217+
* @param string $type The attribute of media type (e.g, `audio/ogg; codecs=opus`).
195218
*
196219
* @return self
197220
*/
@@ -307,25 +330,31 @@ protected function element(array $Element)
307330
$Element['attributes']['class'] = 'table' . ($class ? ' ' : '') . $class;
308331
$markup = '<div class="table-responsive"><table';
309332
}
333+
334+
if($Element['name'] === 'a' && $this->linkAttributes){
335+
foreach($this->linkAttributes as $att => $attrVal){
336+
$markup .= " {$att}=\"{$attrVal}\"";
337+
}
338+
}
310339

311340
if (isset($Element['attributes'])){
312341
foreach ($Element['attributes'] as $name => $value){
313-
if ($value === null){
314-
continue;
315-
}
316-
317-
$value = self::escape($value);
318-
319-
if($Element['name'] === 'a'){
320-
if (!filter_var($value , FILTER_VALIDATE_URL)) {
321-
$markup .= ' ' . $name . '="' . $this->hostLink . '/' . ltrim($value, '/') . '"';
322-
} else {
323-
$target = str_starts_with($value, $this->hostLink)?:' target="_blank" rel="noopener noreferrer"';
324-
$markup .= ' ' . $name . '="' . $value . '"' . $target;
325-
}
326-
}else{
327-
$markup .= ' '.$name.'="'.$value.'"';
328-
}
342+
if ($value === null){
343+
continue;
344+
}
345+
346+
$value = self::escape($value);
347+
348+
if($Element['name'] === 'a'){
349+
if (!filter_var($value , FILTER_VALIDATE_URL)) {
350+
$markup .= ' ' . $name . '="' . $this->hostLink . '/' . ltrim($value, '/') . '"';
351+
} else {
352+
$target = str_starts_with($value, $this->hostLink)?:' target="_blank" rel="noopener noreferrer"';
353+
$markup .= ' ' . $name . '="' . $value . '"' . $target;
354+
}
355+
}else{
356+
$markup .= ' '.$name.'="'.$value.'"';
357+
}
329358
}
330359
}
331360

@@ -347,19 +376,19 @@ protected function element(array $Element)
347376
$markup .= $attr . '>';
348377

349378
if (!isset($Element['nonNestables'])){
350-
$Element['nonNestables'] = [];
379+
$Element['nonNestables'] = [];
351380
}
352381

353382
if (isset($Element['handler'])){
354-
$markup .= $this->{$Element['handler']}($text, $Element['nonNestables']);
383+
$markup .= $this->{$Element['handler']}($text, $Element['nonNestables']);
355384
}elseif (!$permitRawHtml){
356-
$markup .= self::escape($text, true);
385+
$markup .= self::escape($text, true);
357386
}else{
358-
$markup .= $text;
387+
$markup .= $text;
359388
}
360389

361390
if($this->anchor && $headings){
362-
$markup .= '<a class="anchor-link" href="#' . $id . '" title="Permalink to this headline"></a>';
391+
$markup .= '<a class="anchor-link" href="#' . $id . '" title="Permalink to this headline"></a>';
363392
}
364393

365394
$markup .= '</'.$Element['name'].'>';
@@ -368,7 +397,6 @@ protected function element(array $Element)
368397
}
369398

370399
$markup .= $Element['name'] === 'table' ? '</div>' : '';
371-
372400
return $markup;
373401
}
374402

0 commit comments

Comments
 (0)