2020-04-29 20:09:37 -04:00
|
|
|
/* eslint-disable consistent-return */
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Syntax Highlighter
|
|
|
|
//
|
|
|
|
// Applies a syntax highlighting color scheme CSS class to any element with the
|
|
|
|
// `js-syntax-highlight` class
|
|
|
|
//
|
|
|
|
// ### Example Markup
|
|
|
|
//
|
|
|
|
// <div class="js-syntax-highlight"></div>
|
|
|
|
//
|
2016-12-08 19:15:08 -05:00
|
|
|
|
2021-08-11 08:10:59 -04:00
|
|
|
export default function syntaxHighlight($els = null) {
|
|
|
|
if (!$els) return;
|
|
|
|
|
|
|
|
const els = $els.get ? $els.get() : $els;
|
|
|
|
const handler = (el) => {
|
|
|
|
if (el.classList.contains('js-syntax-highlight')) {
|
|
|
|
// Given the element itself, apply highlighting
|
|
|
|
return el.classList.add(gon.user_color_scheme);
|
|
|
|
}
|
|
|
|
// Given a parent element, recurse to any of its applicable children
|
|
|
|
const children = el.querySelectorAll('.js-syntax-highlight');
|
|
|
|
if (children.length) {
|
|
|
|
return syntaxHighlight(children);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// In order to account for NodeList returned by document.querySelectorAll,
|
|
|
|
// we should rather check whether the els object is iterable
|
|
|
|
// instead of relying on Array.isArray()
|
|
|
|
const isIterable = typeof els[Symbol.iterator] === 'function';
|
|
|
|
|
|
|
|
if (isIterable) {
|
|
|
|
els.forEach((el) => handler(el));
|
|
|
|
} else {
|
|
|
|
handler(els);
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2017-12-13 04:26:44 -05:00
|
|
|
}
|