New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide length of TableOfContents #1325

Closed
Alxandr opened this Issue Oct 25, 2017 · 5 comments

Comments

Projects
None yet
2 participants
@Alxandr

Alxandr commented Oct 25, 2017

Currently, you can only iter over TableOfContents. I would like to know the length of it.

@waylan

This comment has been minimized.

Member

waylan commented Oct 25, 2017

I assume you mean that you would like to have access to the length from within a template. Have you tried Jinja's builtin length filter? See how to use filters in templates here. How did that not meet your needs?

Finally, there is no TableOfContents in MkDocs. However, there is a nav (site navigation) and a page.toc (table of contents for an individual page). You'll need to specify which you mean. And as a reminder, both of those objected can contain nested items. You will only ever be able to get the length of an individual branch at a time.

@Alxandr

This comment has been minimized.

Alxandr commented Oct 26, 2017

Yes. It throws saying that no len function is available for the TableOfContents class. Currently I do toc | list | length, which is a pretty stupid hack considering the fact that the length is available on TableOfContents.item.

That being said, could I do toc.items | length maybe? I don't really use python or Jinja, so I might be going at this wrong :)

This is what I'm doing:

  {% set toc_ = page.toc %}

  <!--
    Hack: check whether the content contains a h1 headline. If it does, the
    top-level anchor must be skipped, since it would be redundant to the link
    to the current page that is located just above the anchor. Therefore we
    directly continue with the children of the anchor.

    NOTE: Modified to only do this if there is only 1 top level TOC entry.
  -->
  {% if toc_ | first is defined and "\x3ch1 id=" in page.content and toc_ | list | length == 1 %} {% set toc_ = (toc_ | first).children
  %} {% endif %}
@waylan

This comment has been minimized.

Member

waylan commented Oct 26, 2017

Oh, you want a __len__ method added to the mkdocs.toc.TableOfContents class. Yeah, I agree it is odd there isn't one already. While we're at it, I wonder if we should add any other methods (perhaps contains)?

@waylan waylan added the Enhancement label Oct 26, 2017

@waylan waylan added this to the 1.0.0 milestone Oct 26, 2017

@Alxandr

This comment has been minimized.

Alxandr commented Oct 26, 2017

Sorry. I don't really know what I'm doing, so I might be expressing my needs in the wrong way 😛 .

Anyways, getting this expression I'm doing here as a first class member might be a good idea, as it seems to be a thing multiple templates does (basically, a method/property/something that returns true if there is only a single top level entry). I don't know what the x3ch1 check is though.

Alternatively, provide some way of getting the children of the first entry if it's a single top leve, and the whole thing if not, as a convenience.

@waylan

This comment has been minimized.

Member

waylan commented Oct 26, 2017

Alternatively, provide some way of getting the children of the first entry if it's a single top leve, and the whole thing if not, as a convenience.

Sometimes it helps to understand what you are actually trying to accomplish. Now your request is more clear. Regardless, my proposed fix above will address your needs.

waylan added a commit to waylan/mkdocs that referenced this issue Oct 27, 2017

Add length support to toc.
This could be useful in templates. Fixes mkdocs#1325.

waylan added a commit to waylan/mkdocs that referenced this issue Oct 27, 2017

Add length support to toc.
This could be useful in templates. Fixes mkdocs#1325.

@waylan waylan closed this in #1329 Oct 27, 2017

waylan added a commit that referenced this issue Oct 27, 2017

Add length support to toc.
This could be useful in templates. Fixes #1325.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment