public function ReadmehelpMarkdown::convertLeadingHash in README Help 8
Wraps lines preceded with up to 6 "#" + " " text lines into h1-6 tag.
The single "# " can be used just once at the very beginning of a text. Other hash signs + space sets can be used multiple times in a text. Additionally, "#" sign turns into anchor link, so the heading can be used as a local or external link target.
Parameters
string $text: The string to be filtered.
Return value
string The HTML source with headings.
1 call to ReadmehelpMarkdown::convertLeadingHash()
- ReadmehelpMarkdown::process in src/
Plugin/ Filter/ ReadmehelpMarkdown.php - Performs the filter processing.
File
- src/
Plugin/ Filter/ ReadmehelpMarkdown.php, line 365
Class
- ReadmehelpMarkdown
- Provides a filter for markdown.
Namespace
Drupal\readmehelp\Plugin\FilterCode
public function convertLeadingHash($text) {
foreach (range(1, 6) as $i) {
$hash = str_pad('#', $i, "#");
$j = $i > 1 ? '\\n' . $hash : '^' . $hash;
$text = preg_replace_callback('/' . $j . '(\\s+\\w.*?)\\n/', function ($matches) use ($i) {
if ($match = !empty($matches[1]) ? rtrim($matches[1]) : '') {
$id = trim(Html::getUniqueId($match), '-');
$match = "<h{$i} class=\"h-{$i}\"><a id=\"{$id}\" href=\"#{$id}\" class=\"anchor\">#</a>{$match}</h{$i}>";
}
return $match;
}, $text);
}
return $text;
}