Skip to content

Commit

Permalink
Merge pull request #29 from oveleon/develop
Browse files Browse the repository at this point in the history
Bugfixes for automatic glossary conversion / linking
  • Loading branch information
zoglo authored Jun 2, 2023
2 parents 8c9707c + ea21953 commit c3762d3
Show file tree
Hide file tree
Showing 8 changed files with 2,344 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/composer.lock
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ markupAttr: {
<td>PopperJS options -> check https://popper.js.org/docs/v2/</td>
</tr>
<tr>
<td>includes</td>
<td>includes (array)</td>
<td>
<pre>
'body',
Expand All @@ -612,6 +612,15 @@ markupAttr: {
</td>
<td>Readable nodes for glossary item markup. Elements (and thus it's children) that are not listed within the includes, will not be converted.</td>
</tr>
<tr>
<td>excludeClasses (array)</td>
<td>
<pre>
'gl-none'
</pre>
</td>
<td>Readable nodes matching one or more classes within excludeClasses will not be converted.</td>
</tr>
<tr>
<td>route: {<br/>&nbsp;&nbsp;&nbsp;prefix:<br/>&nbsp;&nbsp;&nbsp;suffix:<br/>&nbsp;&nbsp;&nbsp;cache:<br/>}</td>
<td>&nbsp;<br>'/api/glossary/item/'<br/>'/html'<br/>true<br/>&nbsp;</td>
Expand Down
2,261 changes: 2,261 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
}
],
"devDependencies": {
"webpack": "^5.4.0",
"webpack-cli": "^4.2.0"
"webpack": "^5.85",
"webpack-cli": "^5.1"
},
"dependencies": {
"@popperjs/core": "^2.10.2"
"@popperjs/core": "^2.11"
}
}
50 changes: 34 additions & 16 deletions src/EventListener/BreadcrumbListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Oveleon\ContaoGlossaryBundle\EventListener;

use Contao\Config;
use Contao\CoreBundle\Exception\RouteParametersException;
use Contao\Input;
use Contao\StringUtil;
use Oveleon\ContaoGlossaryBundle\Glossary;
Expand All @@ -27,28 +28,45 @@ class BreadcrumbListener
{
public function __invoke(array $items): array
{
$alias = Input::get('items');
$alias = Input::get('items', false, true);

// Set the item from the auto_item parameter
if (!isset($_GET['items']) && isset($_GET['auto_item']) && Config::get('useAutoItem')) {
$alias = Input::get('auto_item');
if (!isset($_GET['items']) && isset($_GET['auto_item']) && Config::get('useAutoItem'))
{
$alias = Input::get('auto_item', false, true);
}

if ($alias && ($glossaryItem = GlossaryItemModel::findPublishedByIdOrAlias($alias)) !== null) {
// Mark the last item as inactive
$items[\count($items) - 1]['href'] = $GLOBALS['objPage']->getFrontendUrl();
$items[\count($items) - 1]['isActive'] = false;

// Add the new item
$items[] = [
'isRoot' => false,
if ($alias && ($glossaryItem = GlossaryItemModel::findPublishedByIdOrAlias($alias)) !== null)
{
$breadcrumbItem = [
'isRoot' => false,
'isActive' => true,
'href' => Glossary::generateUrl($glossaryItem),
'title' => StringUtil::specialchars($glossaryItem->keyword),
'link' => $glossaryItem->keyword,
'data' => $glossaryItem->row(),
'class' => '',
'href' => Glossary::generateUrl($glossaryItem),
'title' => StringUtil::specialchars($glossaryItem->keyword),
'link' => $glossaryItem->keyword,
'data' => $glossaryItem->row(),
'class' => '',
];

try {
$href = $GLOBALS['objPage']->getFrontendUrl();
} catch (RouteParametersException $e) {
$href = '';
}

if (!strlen($href))
{
// In case the route alias already exists on the parent page, and it requires an item, simply replace the item
$items[\count($items) - 1] = $breadcrumbItem;
}
else
{
$items[\count($items) - 1]['href'] = $href;
$items[\count($items) - 1]['isActive'] = false;

// Append new item
$items[] = $breadcrumbItem;
}
}

return $items;
Expand Down
31 changes: 28 additions & 3 deletions src/EventListener/GeneratePageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Contao\Config;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\FrontendTemplate;
use Contao\Input;
use Contao\LayoutModel;
use Contao\PageModel;
use Contao\PageRegular;
Expand Down Expand Up @@ -72,11 +73,18 @@ public function __invoke(PageModel $pageModel, LayoutModel $layoutModel, PageReg
}

// Load glossary configuration template
$objTemplate = new FrontendTemplate($objRootPage->glossaryConfigTemplate ?: 'config_glossary_default');

$objTemplate = new FrontendTemplate($objRootPage->glossaryConfigTemplate ?: 'config_glossary_default');
$objGlossaryItems = GlossaryItemModel::findPublishedByPids($glossaryArchives, ['order' => "LENGTH(tl_glossary_item.keyword) DESC"]);
$glossaryConfig = null;

$glossaryConfig = null;
$pageId = $pageModel->id;
$alias = Input::get('items');

// Set the item from the auto_item parameter
if (!isset($_GET['items']) && isset($_GET['auto_item']) && Config::get('useAutoItem'))
{
$alias = Input::get('auto_item');
}

if (null !== $objGlossaryItems)
{
Expand All @@ -87,6 +95,23 @@ public function __invoke(PageModel $pageModel, LayoutModel $layoutModel, PageReg
// Check if keywords exist
if (array_filter($arrKeywords = StringUtil::deserialize($objGlossaryItem->keywords, true)))
{
switch ($objGlossaryItem->source)
{
case 'default':
if ($alias === $objGlossaryItem->alias)
{
continue 2; // Skip config entry if we are on the same page
}
break;

case 'internal':
if ($pageId === $objGlossaryItem->jumpTo)
{
continue 2; // Skip config entry if we are on the same page
}
break;
}

$arrGlossaryItems[] = [
'id' => $objGlossaryItem->id,

Expand Down
9 changes: 6 additions & 3 deletions src/Resources/public/scripts/Glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export class Glossary
'mark,abbr',
'sub,sup'
],
excludeClasses: [ // Exclude-classes to skip allowed nodes
'gl-none'
],
route: { // API settings
prefix: '/api/glossary/item/',
suffix: '/html',
Expand Down Expand Up @@ -173,7 +176,7 @@ export class Glossary
*/
_isValidNode(node)
{
return node.nodeType === Node.TEXT_NODE || (node.nodeType === Node.ELEMENT_NODE && !!node.matches(this.options.includes.join(',')))
return node.nodeType === Node.TEXT_NODE || (node.nodeType === Node.ELEMENT_NODE && !!node.matches(this.options.includes.join(',')) && !(!!node.classList.length && node.matches(this.options.excludeClasses.map(c=>'.'+c).join(','))))
}

/**
Expand Down Expand Up @@ -210,7 +213,7 @@ export class Glossary
// Lookbehind in JS regular expressions ( ?>= ) : https://caniuse.com/js-regexp-lookbehind
if (this.isNewIE)
{
const matchRgx = new RegExp("(?:>|^|\\()(" + match + ")\\b", 'gu')
const matchRgx = new RegExp("(?:>|^|\\()(" + match + ")(?!\-)\\b", 'gu')
let sentence = []

for (let word of node.textContent.split(' '))
Expand All @@ -229,7 +232,7 @@ export class Glossary
else
{
// Lookbehind regex for other browsers
const matchRgx = new RegExp("(?<!glc=\"1\">)(?<=\\s|>|^|\\()(" + match + ")\\b", 'gu')
const matchRgx = new RegExp("(?<!glc=\"1\">)(?<=\\s|>|^|\\()(" + match + ")(?!\-)\\b", 'gu')

if (matchRgx.test(node.textContent))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/scripts/dist/main.js

Large diffs are not rendered by default.

0 comments on commit c3762d3

Please sign in to comment.