{"version":3,"file":"toc_controller-BSnUFVKq.js","names":[],"sources":["../../../app/javascript/entrypoints/site_controllers/toc_controller.js"],"sourcesContent":["import { Controller } from \"@hotwired/stimulus\"\n\n// Builds the article table of contents from the headings of the rich-text body,\n// gives each heading an id, and highlights the section being read.\nexport default class extends Controller {\n static targets = [\"content\", \"list\", \"box\"]\n\n connect() {\n const headings = [...this.contentTarget.querySelectorAll(\"h1, h2, h3\")]\n if (headings.length < 2) return\n\n const used = new Set()\n this.links = headings.map((heading) => {\n heading.id ||= this.uniqueId(heading.textContent, used)\n const link = document.createElement(\"a\")\n link.href = `#${heading.id}`\n link.textContent = heading.textContent.trim()\n if (heading.tagName === \"H3\") link.classList.add(\"sub\")\n this.listTarget.appendChild(link)\n return link\n })\n this.boxTarget.hidden = false\n this.observe(headings)\n }\n\n disconnect() {\n this.observer?.disconnect()\n }\n\n observe(headings) {\n if (!(\"IntersectionObserver\" in window)) return\n this.observer = new IntersectionObserver((entries) => {\n entries.forEach((entry) => {\n if (!entry.isIntersecting) return\n this.links.forEach((link) => link.toggleAttribute(\"aria-current\", link.hash === `#${entry.target.id}`))\n })\n }, { rootMargin: \"-20% 0px -70% 0px\" })\n headings.forEach((heading) => this.observer.observe(heading))\n }\n\n uniqueId(text, used) {\n const base = text.toLowerCase().normalize(\"NFD\").replace(/[̀-ͯ]/g, \"\")\n .replace(/[^a-z0-9]+/g, \"-\").replace(/^-|-$/g, \"\") || \"section\"\n let id = base\n let index = 2\n while (used.has(id) || document.getElementById(id)) id = `${base}-${index++}`\n used.add(id)\n return id\n }\n}\n"],"mappings":"4GAIA,EAAA,cAA6B,CAAW,CACtC,OAAO,QAAU,CAAC,UAAW,OAAQ,MAAM,CAE3C,SAAU,CACR,IAAM,EAAW,CAAC,GAAG,KAAK,cAAc,iBAAiB,aAAa,CAAC,CACvE,GAAI,EAAS,OAAS,EAAG,OAEzB,IAAM,EAAO,IAAI,IACjB,KAAK,MAAQ,EAAS,IAAK,GAAY,CACrC,EAAQ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