@@ -92,6 +92,13 @@ class Markdown extends Parsedown
92
92
'h6 '
93
93
];
94
94
95
+ /**
96
+ * Optional attributes for anchor links.
97
+ *
98
+ * @var array $linkAttributes
99
+ */
100
+ private array $ linkAttributes = [];
101
+
95
102
/**
96
103
* Initialize Markdown.
97
104
*/
@@ -106,7 +113,7 @@ public function __construct()
106
113
/**
107
114
* Set host link.
108
115
*
109
- * @param string $link
116
+ * @param string $link The based host URL.
110
117
*
111
118
* @return self
112
119
*/
@@ -117,10 +124,26 @@ public function setLink(string $link): self
117
124
return $ this ;
118
125
}
119
126
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
+
120
143
/**
121
144
* Set id prefix for table of contents.
122
145
*
123
- * @param string $prefix
146
+ * @param string $prefix The id prefix for table of contents.
124
147
*
125
148
* @return self
126
149
*/
@@ -134,7 +157,7 @@ public function setIdPrefix(string $prefix): self
134
157
/**
135
158
* Set table of contents heading.
136
159
*
137
- * @param array $headings
160
+ * @param array $headings The supported headings for table of contents.
138
161
*
139
162
* @return self
140
163
*/
@@ -148,7 +171,7 @@ public function setHeadings(array $headings): self
148
171
/**
149
172
* Enable heading anchor link.
150
173
*
151
- * @param bool $anchors
174
+ * @param bool $anchors The enable/disable of heading anchor link.
152
175
*
153
176
* @return self
154
177
*/
@@ -162,7 +185,7 @@ public function headingAnchor(bool $anchors = true): self
162
185
/**
163
186
* Enable responsive table.
164
187
*
165
- * @param bool $responsive
188
+ * @param bool $responsive The enable/disable of responsive table.
166
189
*
167
190
* @return self
168
191
*/
@@ -176,7 +199,7 @@ public function responsiveTable(bool $responsive = true): self
176
199
/**
177
200
* Set if tables of contents should be enabled.
178
201
*
179
- * @param bool $enable
202
+ * @param bool $enable The enable/disable of table of contents.
180
203
*
181
204
* @return self
182
205
*/
@@ -191,7 +214,7 @@ public function tableOfContents(bool $enable = true): self
191
214
* Set media type
192
215
*
193
216
* @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`).
195
218
*
196
219
* @return self
197
220
*/
@@ -307,25 +330,31 @@ protected function element(array $Element)
307
330
$ Element ['attributes ' ]['class ' ] = 'table ' . ($ class ? ' ' : '' ) . $ class ;
308
331
$ markup = '<div class="table-responsive"><table ' ;
309
332
}
333
+
334
+ if ($ Element ['name ' ] === 'a ' && $ this ->linkAttributes ){
335
+ foreach ($ this ->linkAttributes as $ att => $ attrVal ){
336
+ $ markup .= " {$ att }= \"{$ attrVal }\"" ;
337
+ }
338
+ }
310
339
311
340
if (isset ($ Element ['attributes ' ])){
312
341
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
+ }
329
358
}
330
359
}
331
360
@@ -347,19 +376,19 @@ protected function element(array $Element)
347
376
$ markup .= $ attr . '> ' ;
348
377
349
378
if (!isset ($ Element ['nonNestables ' ])){
350
- $ Element ['nonNestables ' ] = [];
379
+ $ Element ['nonNestables ' ] = [];
351
380
}
352
381
353
382
if (isset ($ Element ['handler ' ])){
354
- $ markup .= $ this ->{$ Element ['handler ' ]}($ text , $ Element ['nonNestables ' ]);
383
+ $ markup .= $ this ->{$ Element ['handler ' ]}($ text , $ Element ['nonNestables ' ]);
355
384
}elseif (!$ permitRawHtml ){
356
- $ markup .= self ::escape ($ text , true );
385
+ $ markup .= self ::escape ($ text , true );
357
386
}else {
358
- $ markup .= $ text ;
387
+ $ markup .= $ text ;
359
388
}
360
389
361
390
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> ' ;
363
392
}
364
393
365
394
$ markup .= '</ ' .$ Element ['name ' ].'> ' ;
@@ -368,7 +397,6 @@ protected function element(array $Element)
368
397
}
369
398
370
399
$ markup .= $ Element ['name ' ] === 'table ' ? '</div> ' : '' ;
371
-
372
400
return $ markup ;
373
401
}
374
402
0 commit comments