-
Notifications
You must be signed in to change notification settings - Fork 36
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
Idea: Option to generate TOC in pages using placeholder? #39
Comments
That's an interesting idea. However, I think it is indeed outside of the scope of this plugin. The good news is that since awesome-pages simply modifies the existing MkDocs nav structure, building a new plugin on top of it shouldn't be too hard. In fact I already put together a little prototype which seems to work quite well: from mkdocs.plugins import BasePlugin
from mkdocs.utils import normalize_url
class InlineNavigationPlugin(BasePlugin):
def on_nav(self, nav, config, files):
self.nav = nav
def on_post_page(self, output, page, config):
if '{nav}' in output:
children = page.parent.children if page.parent else self.nav.items
siblings = [child for child in children if child != page]
return output.replace('{nav}', self._format_links(siblings, page, config))
def _format_links(self, items, page, config):
result = '<ul>'
for item in items:
result += '<li>'
if item.is_section:
result += item.title
result += self._format_links(item.children, page, config)
else:
url = normalize_url(item.url, page)
result += f'<a href="{url}">{item.title}</a>'
result += '</li>'
result += '</ul>'
return result I won't be publishing this as a package - I've already got enough on my hands. But feel free to use the code, modify it or even publish it as a plugin yourself. Refer to the MkDocs documentation on developing plugins. |
Okay thanks. What would be the easiest setup to import this using I quickly created a github repository with some basic setup: |
Yeah you should be able to just install from a git repo:
|
Seems to not work. The setup.py should be correct as aI have a |
You're missing the packages=find_packages(exclude=['*.tests', '*.tests.*']) |
Still not working. I did add the packages part: |
Strange, I get a different error:
What you're definitely still missing is an empty |
Still getting the same issue. EDIT: I'm dumb! I have a typo. |
One last question: I assume I add all the folders and files to the |
I don't know. All unnecessary files get removed from my package when I publish it to PyPI. No idea how it would work when installing from a git repo. Another thing: The plugin does not actually depend on awesome-pages. It also works with the navigation that MkDocs produces out of the box. |
FYI https://github.com/oprypin/mkdocs-literate-nav is a more direct approach to this. The end result is the same: you get both an actual nav and also have it displayed as part of the page. The difference is that the approach is opposite: you write the nav as part of the page in the first place. |
Yeah and that's the issue. |
If you will never care about the order of those posts, and will not care about browseability of that page on GitHub, sure. |
Not sure if this would be either out of scope, or even impossible to implement, but how about having a way to generate a TOC inside a page, by using a specific placeholder for it?
Example
The following structure is present:
I now set up a
.pages.yml
file inassets
to ignore the folder and one inposts
with the following structure:Finally, do I add the following to my
index.md
:The
{nav}
placeholder would now take all pages it can find in theposts
directory and also go through any additional directories found there. It will however skip the file it is used in.Once it has all pages, will it generate a list that could look like this:
It would take the names to display from either the nav-section in the
.pages
file, or from the pages themself.Why?
This plugin is useful for when you want to have the nav updated without needing to always set it yourself, but won't obviously work for when you have a manual nav inside a specific file.
For me is that the case with a blog I have. While I no longer need to update the nav myself do I always have to update the list in the index.md of my posts directory, which is frustrating sometimes.
Having a way to automate this using a placeholder or similar would really help a lot.
I hope this is somewhat doable.
The text was updated successfully, but these errors were encountered: