vnphanquang / svelte-put

Useful svelte stuff to put in your projects

Home Page:https://svelte-put.vnphanquang.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generated table of contents does not reflect on header order if it is a number

nicholascostadev opened this issue · comments

I don't know if it is stated in the docs, but I'm having an issue where if the header is an integer, it gets rendered first in the list. I'm guessing that it's happening because objects are not always kept in order of insertion, so the final $tocStore.items is not follwing the correct order.

I created a sveltelab project for you to easily see this: sveltelab link
image

I guess by using a Map, it keeps the order of the inserted items, so it may be a solution for this issue, but I don't know what may be the performance implications of using it.

I'd like to know if it's a known issue or if there are any plans for it to be solved. I'm already taking a look, but I'm kinda lost since I never worked in a monorepo before

I managed to do it using a Map, but since it's not an object, the way to render the Toc, would be different, the example now looks like:

{#if Object.values($tocStore.items).length}
    <ul>
      {#each Object.values($tocStore.items) as tocItem}
        <li>
          <!-- svelte-ignore a11y-missing-attribute -->
          <!-- eslint-disable-next-line svelte/valid-compile -->
          <a use:toclink={{ store: tocStore, tocItem, observe: true }} />
        </li>
      {/each}
    </ul>
  {/if}

Using the Map, would be like this:

{#if $tocStore.items.size}
    <ul>
      {#each $tocStore.items.values() as tocItem}
        <li>
          <!-- svelte-ignore a11y-missing-attribute -->
          <!-- eslint-disable-next-line svelte/valid-compile -->
          <a use:toclink={{ store: tocStore, tocItem, observe: true }} />
        </li>
      {/each}
    </ul>
  {/if}

@nicholascostadev thanks a lot for the report and detailed suggestion. This is indeed a problem I didn't account for. Changing the public interface of items is breaking and a new major release. Let me try some experiments first to see if there is any alternative that doesn't require a breaking change 🙏

Looks like Map is indeed the way to go. This will be released in the next major release. Thanks again

Thank you for reaching out, is there an intended date for the next major release?

@nicholascostadev I will try within this week. Apologies for the delay. There is too much stuff going on for me at the moment 😅

That's ok, thank you for helping out!

Available in @svelte-put/toc@5.0.0. Thank you again for your help @nicholascostadev 🎉

You're welcome, you already helped me a lot at work with this library, it's an honor @vnphanquang!