-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadid.php
31 lines (26 loc) · 1.38 KB
/
headid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
// Adds ID attributes to all headers that are in fields processed by kirbytext.
function retitle($match) {
// Characters in the $delete array will be removed
// Characters in the $hyphenate array will be changed to hyphens
$delete = c::get('headid-delete', array(':','(',')','?','.','!','$',',','%','^','&',"'",';','"','[',']','{','}','|','`','#'));
$hyphenate = c::get('headid-hyphenate', array(' ','~','@','*','+','=','/','>','<'));
list($_unused, $h2, $title) = $match;
preg_match('/id=\"(.*)\"/',$_unused,$idmatches);
preg_match('/name=\"(.*)\"/',$_unused,$namematches);
if (empty($idmatches) && empty($namematches)) {
$id = strip_tags($title);
$id = strtolower(str_replace($delete,'',str_replace($hyphenate,'-',$id)));
$id = preg_replace('/<\/?a[^>]*>/','',$id);
return "<$h2 id='$id'><a href='#$id'>$title</a></$h2>";
} elseif (!empty($idmatches) && empty($namematches)) {
return "<$h2 $idmatches[0]><a href='#$idmatches[1]'>$title</a></$h2>";
} elseif (empty($idmatches) && !empty($namematches)) {
return "<$h2 id='$namematches[1]'><a href='#$namematches[1]'>$title</a></$h2>";
}
}
// These filters run after all markdown and kirbytext is processed.
kirbytext::$post[] = function($kirbytext, $value) {
$value = preg_replace_callback("#<(h[1-6]).*>(.*?)</\\1>#", "retitle", $value);
return $value;
};