bakpakin / mendoza

A Static Site Generator
https://bakpakin.github.io/mendoza/
MIT License
60 stars 17 forks source link

WIP: Add id attribute to headers #12

Closed sogaiu closed 3 years ago

sogaiu commented 3 years ago

This is an initial sketch of providing an id attribute to headers that originate as markdown-ish things (i.e. 1-6 #s).

It targets only initial string elements of the content parameter for caph: https://github.com/bakpakin/mendoza/blob/2c0010a3e84cab860651da8b961410a2f84abaf4/mendoza/markup.janet#L73

All it does is strip occurences of the space character. Thus it's true that there is potential for collisions.

If that's a concern worth addressing, perhaps there could be a table that's populated and checked for collisions. For a collision, possibly some string can be appended to the checked name and the result could be checked against the table (possibly leading to further checks and appending -- but this should terminate at some point).

I'm not so sure collisions would be that frequent. Though perhaps there are some cases where people want to use the same header names within a page or that space removal could lead to collisions. For the latter case, perhaps that makes replacing inner spaces with - characters a better alternative.

An alternative approach to trying to come up with non-colliding names automatically might be to halt processing and inform the user that there was a name collision -- though it seems this would also require something like a table.

sogaiu commented 3 years ago

Added a commit to check for name collisions and if detected, respond by constructing a name that has not been used already.

There is now a table that stores names along with corresponding integer counts.

The integer is used as a basis for a new suffix for the id in case of collisions. Each time a collision is detected, the integer in the table is incremented.

Also, instead of stripping inner space characters, they are replaced with "-". Leading and trailing space characters are trimmed.

I don't understand how mendoza works well enough to know whether this table needs to be wiped from time-to-time.

sogaiu commented 3 years ago

Now using def instead of var.

Thanks @pyrmont :)

pepe commented 3 years ago

It looks ok to me, I will try this version of mendoza on my project, hopefuly today, and report back.

sogaiu commented 3 years ago

@pepe Thanks for taking a look!

Looking forward to hearing back -- whatever you find :)

sogaiu commented 3 years ago

Thanks for trying and reporting :+1:

The current implementation doesn't consider the possibility (apart from the space character and what trim handles) that there might be some characters in the header text that shouldn't live in an attribute.

Allegedly the only character one needs to be concerned about is the space character: https://mathiasbynens.be/notes/html5-id-class

I don't know whether this is true in practice.

Any opinions?

pepe commented 3 years ago

Well, that was my only concern too, cause the first id it generated was "What?" :-). But I would take it from the linked article, that it is OK.

sogaiu commented 3 years ago

I hope so too :)

Thanks for taking a look!

bakpakin commented 3 years ago

I like this, this is a useful feature and we can definitely add it. While the collision resolution isn't perfect it looks good enough to start.

sogaiu commented 3 years ago

@bakpakin Curious about what's problematic for the collision resolution. Any hints?

As I don't really understand what can end up in content, I wondered if these lines were missing some cases: https://github.com/sogaiu/mendoza/blob/f84e0ea98674a5eb938d98afcd7902766c0bc970/mendoza/markup.janet#L78-L79

In any case, looking forward to ids showing up :)