-
Notifications
You must be signed in to change notification settings - Fork 1
/
anchor-headings.php
executable file
·59 lines (49 loc) · 1.72 KB
/
anchor-headings.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* A kirby field-method which enumerates heading-elements, generates IDs for
* anchor-links and inserts custom markup based on your options.
*
* @package Kirby CMS
* @author Dennis Kerzig <hi@wottpal.com>
* @version 0.4.0
*
*/
// https://github.com/slaith/phpQuery
include_once __DIR__ . DS . 'vendor' . DS . 'phpQuery'. DS . 'phpQuery.php';
include_once __DIR__ . DS . 'helpers.php';
function headingAnchors($field)
{
// Options (see `README.md`)
$options = [
'toplevel_only' => c::get('anchorheadings.toplevel.only', true),
'heading_min' => c::get('anchorheadings.heading.min', 2),
'heading_max' => c::get('anchorheadings.heading.max', 3),
'enum_start' => c::get('anchorheadings.enum.start', 1),
'enum_seperator' => c::get('anchorheadings.enum.seperator', "."),
'id_prefix' => c::get('anchorheadings.id.prefix', false),
'id_prepend_enum' => c::get('anchorheadings.id.prepend.enum', true),
'id_rules' => c::get('anchorheadings.id.rules', [
'/[_ ~\/,.]/' => '-',
'/ä/' => 'ae', '/ö/' => 'oe', '/ü/' => 'ue',
'/Ä/' => 'Ae', '/Ö/' => 'Oe', '/Ü/' => 'Ue',
'/ß/' => 'ss', '/ẞ/' => 'SS',
'/[^A-Za-z0-9\-]/' => '',
'/-+$/' => '',
'/-{2,}/' => '-',
'/([A-Z]+)/' => function($x) { return strtolower($x[1]); }
]),
'markup' => c::get('anchorheadings.markup', "
<a href='#{id}'>{enum}.</a> {heading}
")
];
// Parse document with PHPQuery
$doc = phpQuery::newDocument($field->value());
// Insert anchors
insertAnchors($options);
return $doc->html();
}
/**
* Sets the method as field method, so you can use it like:
* <?= $page->text()->kirbytext()->headingAnchors() ?>
*/
field::$methods['headingAnchors'] = 'headingAnchors';