Code Monkey home page Code Monkey logo

Comments (6)

pkra avatar pkra commented on September 28, 2024

Oh yeah, that's an easy web component, I think.

from laml-wc.

pkra avatar pkra commented on September 28, 2024

The hardest part is probably around IDs / hashes. Cool URLs don't change and all that.

Do we expect authors to specify them? If we generate them, they probably shouldn't be random. Basing them on the heading content is usually a good plan but could lead to duplicates (e.g., each chapter having a section "Exercises").

from laml-wc.

pkra avatar pkra commented on September 28, 2024

Very quick experiment:

class toc extends HTMLElement {
  constructor() {
    super()
  }
  connectedCallback() {
    document.querySelectorAll('h-').forEach(node => { if (!node.id) node.id = self.crypto.randomUUID() })
    let headings = document.querySelectorAll('h-[aria-level="2"]');
    this.insertAdjacentHTML('beforeend',
      `<nav aria-label="table of contents">
        <ul>
        ${[...headings].map(node => 
           `<li>
                <a href="#${node.id}">${node.innerHTML}</a> 
               <ul>
                ${[...node.parentNode.querySelectorAll('h-[aria-level="3"]')].map(nested => 
                      `<li><a href="#${nested.id}">${nested.innerHTML}</li>`).join('')}
                </ul>
             </li>`).join('')}
         </ul>
       </nav>`)
  }
}
customElements.define('toc-', toc);

from laml-wc.

scoskey avatar scoskey commented on September 28, 2024

Laughed about the cool URI's...

I guess the author could supply, otherwise it gets generated?

from laml-wc.

pkra avatar pkra commented on September 28, 2024

I guess the author could supply, otherwise it gets generated?

I suspect authors don't want to do that for each chapter & section (and whatever else they want in a customized TOC).

It's probably safe to generate them from heading content initially and consider duplicate IDs an author error. If we feel like it, we can later add a simple counter suffix when detecting duplicate IDs.

from laml-wc.

pkra avatar pkra commented on September 28, 2024

Ugh - sorry. Shouldn't reply so early. I copied but didn't register "or gets generated".

So yes, I agree vehemently. 🤣

from laml-wc.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.