pelican-page-hierarchy
pelican-page-hierarchy copied to clipboard
Add option to ignore slug when building save_as and url
Thanks to this plugin, content can be structured hierarchically, which is awesome. For a collaborative website, we'd like to take it one step further and have every page be a directory (for context: option 2 in https://github.com/tdwg/website/issues/24):
So instead of:
└── content/pages/ # PAGE_DIR
├── about.md # URL: pages/about/
├── projects.md # URL: pages/projects/
└── projects/ # (directory)
└── p1.md # URL: pages/projects/p1/
Do:
└── content/pages/ # PAGE_DIR
├── about/
│ └── index.md # URL: pages/about/
└── projects/
├── index.md # URL: pages/projects/
└── p1/
├── index.md # URL: pages/projects/p1/
└── file.pdf
So all content regarding e.g. projects (including the landing page pages/projects/
) are in the directory projects/
. It also allows to put static files much closer to the page (e.g. file.pdf
in the example above).
By default the plugin will generate paths including the slug so in the above case we end up with URLs such as /projects/index/index.html
. This can easily be solved by ignoring the page slug in this line of code:
https://github.com/akhayyat/pelican-page-hierarchy/blob/ad7c9879147bf82dc8c66e50ef464ecec4c8972c/page_hierarchy.py#L37
And since ignoring the slug should not be the default behaviour, it could be supported by adding a IGNORE_PAGE_SLUG = True
setting:
if 'IGNORE_PAGE_SLUG' in page.settings and page.settings['IGNORE_PAGE_SLUG']:
metadata['slug'] = path
else:
metadata['slug'] = os.path.join(path, page.slug)
- I've tested the above implementation and it seems to work fine. Page hierarchy (parents/children) are not affected.
- Since the slug is ignored, one does not have to use
index.md
- If you do use
index.md
, it's best to useSLUGIFY_SOURCE = "title"
to avoid warnings such asThere are x original (not translated) items with slug "index"
@akhayyat I'm writing this as an issue rather than a pull request, because 1) you might consider this an edge case you don't want to support in the plugin and 2) it would also require an update to the documentation, which you might want to discuss first. But since we find it useful, I thought it might be useful for others as well.
Thanks for your interest and willingness to contribute, @peterdesmet.
A curious question: would simply specially handling the index
slug solve your problem? So, whenever the slug is index
, the slug is ignored when constructing the URL (and perhaps the slug source is set to the title to avoid warnings) - without any settings.
Indeed, that was my initial solution, but dropped it because it triggered a lot of warnings. Didn’t think to set slug to title for those cases (only)! Good idea! It avoids an extra setting.