Skip to content

Commit

Permalink
Closes #34: exclude svg use from lrc
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Oct 9, 2024
1 parent 78ca211 commit c5d1b02
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/BeaconLrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ class BeaconLrc {

_getLazyRenderElements() {
const elements = document.querySelectorAll('[data-rocket-location-hash]');
const svgUseTargets = this._getSvgUseTargets();

if (elements.length <= 0) {
return [];
}

const validElements = Array.from(elements).filter(element => !this._skipElement(element));
const validElements = Array.from(elements).filter(element => {
if (this._skipElement(element)) {
return false;
}
if (svgUseTargets.includes(element)) {
this.logger.logColoredMessage(`Element skipped because of SVG: ${element.tagName}`, 'orange');
return false;
}
return true;
});


return validElements.map(element => ({
element: element,
Expand Down Expand Up @@ -135,6 +146,21 @@ class BeaconLrc {
: 'No hash detected';
}

_getSvgUseTargets() {
const useElements = document.querySelectorAll('use');
const targets = new Set();

useElements.forEach(use => {
let parent = use.parentElement;
while (parent && parent !== document.body) {
targets.add(parent);
parent = parent.parentElement;
}
});

return Array.from(targets);
}

getResults() {
return this.lazyRenderElements;
}
Expand Down

0 comments on commit c5d1b02

Please sign in to comment.