lektor / lektor-tags

For each tag on your site, build a list of pages with that tag. This can be used for standard tag-based blog navigation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: Tags are case sensitive

svandragt opened this issue · comments

On Lektor 2.2 (desktop OSX download) with the setup from the README I can't get tag pages to work, they always display a 404.
Not sure what further information you require.

I'm running a default quick start with blog.
The macros/ blog_post.html contains after post.body (which renders the link /blog@tag/python in the output html):

{% if post.tags %}
  <ul>
    {% for t in post.tags -%}
      <li>
        <a href="{{ ('/blog@tag/' ~ t.lower())|url }}">
          All posts tagged {{ t }}
        </a>
    {% endfor %}
  </ul>
{% endif %}

Full templates/tags.html:

{% extends "layout.html" %}
{% block title %}{{ this.title }}{% endblock %}
{% block body %}
<article>
  <h2>Tag: {{ this.tag }}</h2>
  <p>Items:</p>
  <ul>
    {% for i in this.items %}
      <li><a href="{{ i|url }}">{{ i._id }}</a></li>
    {% else %}
      <li><em>No items.</em></li>
    {% endfor %}
  </ul>
</article>
{% endblock %}

blog-post.ini has this added:

[fields.tags]
type = strings

This is the backtrace when visiting a tagpage:
https://gist.github.com/03317edb0f905272a34c225a800a99e0

The problem here is that tags are not case sensitive.
If you have a tag called Web, then the tagpage is /tag/Web.
However the example template file lowercases the tag name in thje URL which then does not match the dictionary key causing KeyErrors.

Also tags can contain spaces, really it should be properly processed like a slug, then stored and retrieved that way.

commented

@svandragt Just browsing the open issues here, discovered yours, now trying to understand the problem.

Tested the lower case vs. title case thing and I'm seeing that both show up on my tags page, but both lead to a lower case URL, thus only the post(s) tagged with the lower case tag get listed. Are you suggesting these should be two tags? (Wouldn't it make more sense to consolidate them all into one?)

Regarding tags with spaces: are you saying the spaces should get replaced with another character (like a dash or underscore) in the URL, but display normally (with spaces) in posts and on the tags overview page?

commented

Ok, just discovered unmerged PR #3. (:

If your tag is "Apple News" I expected that to change to "apple-news" in the URL.
Because I also had "Apple news" in my WordPress export I ended up with two tags because of the case sensitivity, and broken links.

commented

@svandragt I've looked into this more and thought it over and have come to the conclusion that the part of your PR that is about the case of tags might not actually make full sense.

Reason being: people might well want to use tags that contain capital letters (maybe not even for the URLs, but within blog posts), and the variables you modified can actually be individually overridden in configs/tags.ini (you'd use url_path there to replace DEFAULT_URL_PATH_EXP).

Lektor's utils.py also contains a function called slugify (used to turn the project name into a slug during quickstart, among other things), which I'm using now to concatenate and lowercase my tag names for my URLs within the code of the lektor-tags plugin.

What I want to do though, and sadly haven't quite figured out yet, is to display my tags in posts exactly like they were spelled originally (which means I have to keep the tags as-is in templates, but "slugify" the URLs manually), but treat them as the same tag (lower case version) and list all posts containing it/them when looking up the tag page...

The traceback linked earlier is a duplicate of #2, and is fixed.

I agree that we should actually preserve case sensitivity, and not lowercase everything. Upper case letters and spaces are url safe. To change the url_path for tags, the default expression for that can be set in the config/tags.ini file. For example, you could use https://github.com/terminal-labs/lektor-slugify and have url_path = {{ this.parent.url_path }}tag/{{ tag|slug }}, which would alter the path the tag pages are produced at.

I've changed the README to remove the .lower() that was in numerous places, since doing that in a typical setup would break links to tag pages where that tag has any uppercase letters. 5a7d36d