Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove anchorjs in toc filter #639

Merged
merged 5 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 53 additions & 29 deletions code/site/components/com_pages/template/filter/toc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@ class ComPagesTemplateFilterToc extends ComPagesTemplateFilterAbstract
protected function _initialize(KObjectConfig $config)
{
$config->append(array(
'min_level' => 1,
'min_level' => 2,
'max_level' => 6,
'anchor' => [
'enabled' => true,
'options' => [
'placement' => 'right',
'visibale' => 'hover',
'icon' => "",
'class' => null,
'truncate' => null,
'arialabel' => 'Anchor',
],
'selector' => 'article h2, article h3, article h4, article h5, article h6',
],
'anchor' => true,
'icon' => '#'
));

parent::_initialize($config);
Expand All @@ -46,37 +36,71 @@ public function filter(&$text)
*/

$matches = array();
if($this->isEnabled() && preg_match_all('#<h(['.$attributes['min'].'-'.$attributes['max'].'])\s*[^>]*>(.+?)</h\1>#is', $text, $headers))
if($this->isEnabled())
{
preg_match_all('#<h(['.$attributes['min'].'-'.$attributes['max'].'])\s*[^>]*>(.+?)</h\1>#is', $text, $headers);

foreach($headers[1] as $key => $level)
{
$content = $headers[2][$key];
$id = $this->_generateId($content);

$result = '<h'.$level.' id="'.$id.'">'.$content.'</h'.$level.'>';
$result = '<h'.$level.' id="'.$id.'" class="toc-anchor"><a href="#'.$id.'">'.$content.'</a></h'.$level.'>';
$text = str_replace($headers[0][$key], $result, $text);

if($this->getConfig()->anchor->enabled) {
$text .= $this->getTemplate()->helper('behavior.anchor', KObjectConfig::unbox($this->getConfig()->anchor));
if($this->getConfig()->anchor)
{
$icon = $this->getConfig()->icon;

if(strlen($icon) == 1) {
$icon = '"'.$icon.'"';
}

//Accessible anchor links, see https://codepen.io/johanjanssens/pen/PoWObpL
$text .= <<<ANCHOR
<style>
@media (hover: hover) {
.toc-anchor {
margin-left: -1em;
padding-left: 1em;
}
.toc-anchor a {
text-decoration: none;
pointer-events: none;
color: inherit !important;
}
.toc-anchor a::after {
content: $icon;
font-size: 0.8em;
float: left;
padding: 0 .15em; /* to make the content a bigger target */
pointer-events: auto;
margin-left: -1em;
visibility: hidden;
display: inline-block;
}
.toc-anchor:hover a::after {
visibility: visible;
}
}
</style>
ANCHOR;

}
}
}

/*
* Table of content
*/
$matches = array();
if(preg_match_all('#<ktml:toc\s*([^>]*)>#siU', $text, $matches))
{
foreach($matches[0] as $key => $match)
/*
* Table of content
*/
$matches = array();
if(preg_match_all('#<ktml:toc\s*([^>]*)>#siU', $text, $matches))
{
$toc = '';
if($this->isEnabled())
foreach($matches[0] as $key => $match)
{
$toc = '';
$attributes = array_merge($attributes, $this->parseAttributes($matches[1][$key]));

$headers = array();
if(preg_match_all('#<h(['.$attributes['min'].'-'.$attributes['max'].'])\s*[^>]*>(.+?)</h\1>#is', $text, $headers))
if($headers)
{
$toc = '<ul class="toc" itemscope itemtype="http://www.schema.org/SiteNavigationElement">';

Expand Down
38 changes: 0 additions & 38 deletions code/site/components/com_pages/template/helper/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,6 @@

class ComPagesTemplateHelperBehavior extends ComKoowaTemplateHelperBehavior
{
public function anchor($config = array())
{
$config = new ComPagesObjectConfig($config);
$config->append(array(
'debug' => $this->getConfig('pages.config')->debug,
'options' => array(
'placement' => 'right',
'visibale' => 'hover',
'icon' => "",
'class' => null,
'truncate' => null,
'arialabel' => 'Anchor',
),
'version' => '4.3.0',
'selector' => 'article h2, article h3, article h4, article h5, article h6',
));

$html = '';
if (!static::isLoaded('anchor'))
{
$selector = json_encode($config->selector);

$html .= '<ktml:script src="https://unpkg.com/anchor-js@'.$config->version.'/anchor.'.(!$config->debug ? 'min.js' : 'js').'" defer="defer" />';
$html .= <<<ANCHOR
<script>
document.addEventListener("DOMContentLoaded", function(event) {
anchors.options = $config->options
anchors.add($selector);if(document.querySelector('.no-anchor')!==null){anchors.remove('.no-anchor');}
})
</script>
ANCHOR;

static::setLoaded('anchor');
}

return $html;
}

public function prefetcher($config = array())
{
$config = new ComPagesObjectConfig($config);
Expand Down