GithubHelp home page GithubHelp logo

generate multi-level menus about headings HOT 7 CLOSED

oupala avatar oupala commented on August 21, 2024
generate multi-level menus

from headings.

Comments (7)

aumouvantsillage avatar aumouvantsillage commented on August 21, 2024 2

metalsmith-headings generates a flat data structure :

var headings = [
    { id: "foo1", tag: "h1", text: "foo1 text" },
    { id: "foo11", tag: "h2", text: "foo11 text" },
    { id: "foo12", tag: "h2", text: "foo12 text" },
    { id: "foo2", tag: "h1", text: "foo2 text" },
    { id: "foo3", tag: "h1", text: "foo3 text" }
];

As far as I can see, the list of headings is ordered by tags, like this:

var headings = [
    { id: "foo1", tag: "h1", text: "foo1 text" },
    { id: "foo2", tag: "h1", text: "foo2 text" },
    { id: "foo3", tag: "h1", text: "foo3 text" },
    { id: "foo11", tag: "h2", text: "foo11 text" },
    { id: "foo12", tag: "h2", text: "foo12 text" }
];

To generate a multi-level menu, the list of headings should preserve the ordering.

from headings.

oupala avatar oupala commented on August 21, 2024

I'm now sure that the generated html is not always correct.

If you have the following headings :

var headings = [
    { id: "foo1", tag: "h1", text: "foo1 text" },
    { id: "foo2", tag: "h2", text: "foo11 text" }
];

My JSFiddle example will generate the following html :

<!-- toc -->
<div class="col-md-3">
    <div class="sidebar hidden-print affix-top toc" role="complementary">
        <ul class="nav sidenav">
            <li></li><li><a href="#foo1">foo1 text</a></li>
            <li><a href="#foo2">foo2 text</a>
    </div>
</div>

You can notice the useless <li></li> at the beginning of line 5 which should be like :

            <li><a href="#foo1">foo1 text</a></li>

You can also notice the missing </li></ul> at the end of line 6 which should be like :

            <li><a href="#foo2">foo2 text</a></li></ul>

I definetly have to find a smarter way to generate my menu. Any help gladly welcome !

Maybe @hegemonic (who is the author of the pull request that adds the tag level to metadata : #1) has an idea on this...

from headings.

oupala avatar oupala commented on August 21, 2024

In addition, the second flaws mentionned in my first comment annoys me a lot :

html is generated by helper whereas it should be generated by the template

I'm pretty sure that I'm using Handlebars helpers the wrong way.

Here again, any help gladly welcome !

from headings.

jmjf avatar jmjf commented on August 21, 2024

The problem in your JSFiddle seems to be that you're outputting a </li> at the beginning of lines 31 and 37, meaning you assume an open <li> that isn't there for the first item in the list. So you need to skip the </li> for the first item.

And it seems like the headings output should be available somewhere in @options.root, so would be available in a Handlebars partial. That might let you avoid a helper altogether with a recursively called partial.

Hope that helps. If I think of anything else or get a break to look at the issue, I'll follow up. (Or maybe it spurs some ideas for others who can jump in.)

from headings.

oupala avatar oupala commented on August 21, 2024

My problem is that I need to close the <li> before opening a new one.

I need to do that because I'm not closing <li> after opening it.

And that's because I need to leave the <li> open in case there is a subitem.

And unfortunately, I don't get your point about @options.root, probably because my knowledge is too weak on handlebars. Can you please give me some more information about that ? (maybe another JSFiddle... ?)

from headings.

oupala avatar oupala commented on August 21, 2024

Problem fixed using a fork: xiphiaz/metalsmith-headings (see this message).

This plugin could be fixed when pull request #5 will be merged.

from headings.

oupala avatar oupala commented on August 21, 2024

Very good news, thanks a lot!

from headings.

Related Issues (5)

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.