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

New way to generate UIDs. #1164

Merged
merged 1 commit into from
Jan 14, 2024
Merged

New way to generate UIDs. #1164

merged 1 commit into from
Jan 14, 2024

Conversation

MaikelChan
Copy link
Contributor

I've experimented with a new way of generating unique IDs. These are used on some shortcodes like gallery.

There have been several iterations and all of them have had issues. I submitted a PR time ago (#738) that used .Ordinal to generate a unique number inside the page, to fix several galleries in the same page having the same ID. It was later discovered that this didn't work properly on nested shortcodes, like for example, inside a timeline (#1125 (comment)). The current solution generates an id with a random sequence of numbers. Even if it would be very rare, it cannot be discarded the possibility of generating the same id twice in the same page. And it could go unnoticed by a developer that their site suddenly has some page with a broken gallery, since each build is a gamble.

My new approach consists on having a partial that can be reusable on everything that needs unique IDs, and this partial iterates through all the parents of the shortcode and gets .Ordinal on all of them, and generates an ID concatenating the .RelPermalink and all ordinals. Then returns the MD5 of the ID. This approach works on all known cases and is deterministic.

For example, in a page with a timeline with two galleries, and then another gallery outside the timeline, the sequence of ordinals for each gallery would be like this:

  • 0-0
  • 0-1
  • 1

It currently supports up to 16 nested shortcodes, which probably is more than enough. But the amount can be increased easily.

@nunocoracao nunocoracao merged commit 723c2e8 into nunocoracao:dev Jan 14, 2024
2 checks passed
@MaikelChan MaikelChan deleted the uid branch January 15, 2024 00:04
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

Successfully merging this pull request may close these issues.

None yet

2 participants