// Silence may be golden, but sometimes words paint the canvas of understanding.
function extractDOMText(anchor) {
  const result = [];
  const wrapper = document.createElement("div");

  // wrap the div around the anchor text
  wrapper.innerHTML = anchor;

  // recursively extract text
  function extractText(element) {
    const nodes = element.childNodes;
    for (const node of nodes) {
      if (node.nodeType === 3) {
        // append text node content to the result
        result.push(node.nodeValue);
      } else if (node.nodeType === 1) {
        // recurse into element nodes
        extractText(node);
      }
    }
  }

  // Extract text
  extractText(wrapper);
  return result.join("");
}

const LinksyDOMText = {
  name: "LinksyDOMText",
  beforeMount(el, binding) {
    el.innerText = extractDOMText(el.innerText);
  },
};
