Skip to content
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

multi-word tags should be hyphenated #23

Closed
oupala opened this issue Aug 24, 2015 · 16 comments
Closed

multi-word tags should be hyphenated #23

oupala opened this issue Aug 24, 2015 · 16 comments

Comments

@oupala
Copy link

oupala commented Aug 24, 2015

With old version of metalsmith-tags (something like 0.6.x), multi-word tags were hyphenated :

the tag my tag would be converted to my-tag

With the last version of metalsmith-tags (0.10.x or 0.11.x), tags are not hyphenated any-more.

This causes some problem, espcially when linking to the tag page. The generated page is hypenated indeed : topics/my-tag/index.html whereas the link to this page is not hyphenated topics/my tag/.

This leads to invalid links.

As a consequence, the fix should hyphenate the link to the tag 'my-tag :

<a href="/tags/my tag/">my tag</a>

should become :

<a href="/tags/my-tag/">my tag</a>

The label my tag could also be converted to my-tag or not, I don't have any strong opinion on this.

@brianbrewer
Copy link
Contributor

If you're using Handlebars, you can register a helper to make the strings safe for links (regex copied from lib/index.js):

Handlebars.registerHelper("safe", function (context) {
    return context.replace(/ /g, '-');
});

And use it in a template (where this is the string with the tag name):

{{#each tags}}
    <a href="/tags/{{safe this}}">{{this}}</a>
{{/each}}

@oupala
Copy link
Author

oupala commented Aug 25, 2015

@brianbrewer I think that your answer is a workaround to make the plugin usable.

But I think this plugin should be usable by default, without the need of registering an additionnal helper.

@brianbrewer
Copy link
Contributor

I completely understand, linking to a tag page is hassle without manipulating the tag string some other way, so I've made a fork and added a hyphenate option to set whether to hyphenate the multi-word tags or not. Currently it's default false to not break anything.

Using it should just be a case of:

.use(tags({
    handle: 'tags',
    path:'topics/:tag.html',
    layout:'/partials/tag.hbt',
    sortBy: 'date',
    reverse: true,
    skipMetadata: false,
    hyphenate: true
  }));

Hopefully that should make all tags with spaces in them hyphenates at all points, I was thinking about maybe adding hyphenated and spaced tags to the metadata separately for completeness. I'll just mention @hswolff to get his opinion on this.

@oupala
Copy link
Author

oupala commented Aug 26, 2015

Your fork fixes the problem on my side, you should create a pull request as it should be merged.

The hyphenate option should be set to true as default value.

Not only all tags with spaces should be hyphenated, but your option hyphenate: true should not be an option as the plugin does not work without it as long as some tags contain spaces (and tags without space are not affected by this new behaviour).

@oupala
Copy link
Author

oupala commented Aug 26, 2015

I checked an old website generated by metalsmith and metalsmith-tags and it appears that the default behaviour of metalsmith-tags used to hyphenate multi-word tags.

As a consequence, not hyphenatig multi-word tags is something I would call a bug.

@brianbrewer
Copy link
Contributor

I see, I've looked through the changes made that affected this version 0.9.0, it looks #5 is a previous issue about how spaces are represented using - instead.

The suggested way to link to them is to rely on path metadata provided by metalsmith-permalinks and using the global tags[tagname] metadata to get to it, the reliance on the existence of a path metadata for each file is an assumption of the presence of metalsmith-permalinks.
EDIT: There doesn't seem to be a reference to the generated tag page at all in the tags metadata, with or without metalsmith-permalinks

I'm currently going through the process of changing the plugin so it uses hyphens as default, with some sort of keepSpaces: true option.
EDIT: Since it doesn't serve any purpose to keep the spaces, I've removed the option completely, the changes will be reflected in my fork later, I'll make a PR tonight

@oupala
Copy link
Author

oupala commented Aug 27, 2015

I'm hardly waiting for your pull request to be merged.

@brianbrewer you might have forgotten to make your pull request...

@oupala
Copy link
Author

oupala commented Aug 27, 2015

@brianbrewer your previous comment makes me think about #21 and metalsmith/permalinks#37.

I had suspected that metalsmith-permalinks and metalsmith-tags were closely tied. Do you know how they should be used ? In what order ? With what configuration ?

@brianbrewer
Copy link
Contributor

Really not sure, my configuration has metalsmith-tags -> metalsmith-pagination -> metalsmith-permalinks but in order to keep the path metadata for each file, I had to use the skipMetadata: true for metalsmith-tags

Sorry I couldn't be of more help

@oupala
Copy link
Author

oupala commented Aug 27, 2015

@brianbrewer I know that we're talking about something else than the main subject of this issue, but why would metalsmith-tags changes path metadata for each file?

@oupala
Copy link
Author

oupala commented Aug 28, 2015

Just to credit another plugin, this issue has been discovered adding the metalsmith-broken-link-checker plugin. It found some broken links and these links were all tag related.

Thanks for that @davidxmoody !

@hswolff
Copy link
Collaborator

hswolff commented Aug 30, 2015

So just pushed up 0.12.0 which offers a solution to this issue. I've added a new property for each page tagsUrlSafe which holds an array of url safe tag values.

See my usage of it on my blog: hswolff/blog@29f7799

Another solution I was considering was to change the tags array to hold objects that looked something like:

{
   url: 'this-is-a-tag',
   display: 'this is a tag'
}

But I felt that would be a bigger change.

Let me know what you think!

@hswolff hswolff closed this as completed Aug 30, 2015
@oupala
Copy link
Author

oupala commented Aug 31, 2015

Thank you @hswolff for fixing this bug. I can confirm that your fix is also fixing the bug on my side.

I just had to replace the tags tag by tagsUrlSafe in my handlebars templates.

I still wonder why you created another tag tagsUrlSafe and why not keep the old tags and fix its value?

What for is the old tags tag?

@hswolff
Copy link
Collaborator

hswolff commented Aug 31, 2015

For display purposes, that's what issue #5 was concerned with. If the tag name is open source then it should be displayed as open source in the page, but linked to as open-source. Hence the two arrays :)

@oupala
Copy link
Author

oupala commented Aug 31, 2015

Ok, I understand the point.

But I don't think it is a good idea.

If the tag name is open source, how can you be sure that it is one tag whose name is open source, or two tags which are open and source?

I don't mind as your implementation makes any solution possible. But I don't really see any usecase where issue #5 happens.

However, if someone created this issue, it means that he should have met this usecase...

And by the way, I don't have any opinion on what is the best solution between the current one, and the other solution you're talking about. I let you and other experts decide.

@hswolff
Copy link
Collaborator

hswolff commented Aug 31, 2015

Right now we're just using , to distinguish between different tags, so that's who we know. Yeah just trying to appease both use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants