Skip to content
Snippets Groups Projects
Commit c7eda751 authored by Kent Richards's avatar Kent Richards :speech_balloon: Committed by Nicholas Mangold
Browse files

Issue #3453745 by kentr: Filter has false positives

parent fa82268b
No related branches found
No related tags found
1 merge request!14Issue #3453745: Fix filter false positives
......@@ -9,7 +9,8 @@ import hljs from 'https://unpkg.com/@highlightjs/cdn-assets@11.8.0/es/core.min.j
const initEvent = new Event("highlightjs:init");
const promises = Object.values(languages).map(language => {
return import(`https://unpkg.com/@highlightjs/cdn-assets@11.8.0/es/languages/${language}.min.js`)
.then(module => hljs.registerLanguage(language, module.default));
.then(module => hljs.registerLanguage(language, module.default))
.catch(e => console.error(`${e.name}: ${e.message}`));
});
if (drupalSettings.enableCopyButton == true) {
......
......@@ -23,21 +23,23 @@ class HighlightJs extends FilterBase {
public function process($text, $langcode) {
$enable_copy_button = \Drupal::config('highlightjs_input_filter.settings')->get('enable_copy_button');
$result = new FilterProcessResult($text);
if (stristr($text, '<code') === FALSE || stristr($text, 'language-') === FALSE) {
// Regex approximates the className regex in highlight.js itself,
// also matching the containing `<pre><code>` tags.
// Using a DOM parser as highlight.js does would probably be cleaner.
$pattern = '/<pre>\s*<code\s+class="\s*(?:[\w-]+\s+)?\b[\w-]*lang(?:uage)?-([\w-]+)\b/i';
// If no matches are found, return early.
if (!preg_match_all($pattern, $result, $matches)) {
return $result;
}
$pattern = "/language-[^\"]+/i";
if (preg_match_all($pattern, $result, $matches)) {
foreach ($matches as $match) {
foreach ($match as $language_id) {
$language = str_replace('language-', '', $language_id);
// Create a uniquely keyed array of languages so the
// BubbleableMetadata->mergeAttachments() method can properly
// merge the drupalSettings when adding attachments.
$languages[$language] = $language;
}
}
// If matches are found, process.
foreach ($matches[1] as $language) {
// Create a uniquely keyed array of languages so the
// BubbleableMetadata->mergeAttachments() method can properly
// merge the drupalSettings when adding attachments.
$languages[$language] = $language;
}
$result->addCacheableDependency($enable_copy_button);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment