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

Fix truncation of ids and menu titles #23

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion anchors.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function onPluginsInitialized()

public function onTwigExtensions()
{
$config = $this->config->get('plugins.anchors.selectors');
$config = $this->config->get('plugins.anchors');
require_once(__DIR__ . '/twig/AnchorsTwigExtension.php');
$this->grav['twig']->twig->addExtension(new AnchorsTwigExtension($config));
}
Expand Down
12 changes: 7 additions & 5 deletions twig/AnchorsTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Grav\Plugin;

use Grav\Common\Config\Config;

class AnchorsTwigExtension extends \Twig_Extension
{

/** @var Config $config */
private $config;

public function __construct($config)
{
$this->config = $config;
$this->config = new Config($config);
}

public function getName()
Expand All @@ -34,7 +36,7 @@ public function getFunctions()
public function anchorsFunction($content, $tag = 'h2', $terms)
{

$configTags = array_map('trim', explode(',',$this->config));
$configTags = array_map('trim', explode(',',$this->config['selectors']));

if (in_array($tag, $configTags)){
$textMenu = [];
Expand Down Expand Up @@ -80,7 +82,7 @@ private function getHtmlTag($itens)

$html .= '<ul class="items-menus-page">';
foreach($itens as $item){
$html .= '<li><a href="#'.$this->getUrl($item).'">'.$this->textLimit($item, 50, false).'</a></li>';
$html .= '<li><a href="#'.$this->getUrl($item).'">'.$this->textLimit($item, $this->config->get('truncate_menu_title', 50), false).'</a></li>';
}
$html .= '</ul>';

Expand All @@ -106,7 +108,7 @@ private function getUrl($text)
$urlAnchor1 = str_replace('–', '-', str_replace($rx1, '', $text));
$urlAnchor2 = preg_replace($rx2, '', $urlAnchor1);
$urlAnchor3 = preg_replace($rx3, '-', $urlAnchor2);
$urlAnchor4 = $this->textLimit(preg_replace($rx4, '-', $urlAnchor3), 64);
$urlAnchor4 = $this->textLimit(preg_replace($rx4, '-', $urlAnchor3), $this->config->get('truncate', 64));
$urlAnchor5 = preg_replace($rx5, '', $urlAnchor4);

$urlAnchorFinal = strtolower($urlAnchor5);
Expand Down