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

Simple solution for global unique header IDs #3262

Closed
wants to merge 14 commits into from

Conversation

dbolack-ab
Copy link
Collaborator

Solves Issue #2832.

Trusts that MarkedGFMHeadingId will make the ids unique per page and header level and then massages them to have what should be a global uniq name via the hooks: postprocess system.

# Header 1
# Header 1
# Header 1
## Header 2
### Header 3
#### Header 4

<h1 id="p0-H1-header-1-1">Header 1</h1>
<h1 id="p0-H1-header-1-2">Header 1</h1>
<h2 id="p0-H2-header-2">Header 2</h2>
<h3 id="p0-H3-header-3">Header 3</h3>
<h4 id="p0-H4-header-4">Header 4</h4>

@dbolack-ab dbolack-ab self-assigned this Jan 22, 2024
@dbolack-ab
Copy link
Collaborator Author

Code tests are failing due to an issue with jest and dom that I do not have enough knowledge of to solve.

@dbolack-ab dbolack-ab added the 🔍 R0 - Needs first review 👀 PR ready but has not been reviewed label Jan 22, 2024
@ericscheid
Copy link
Collaborator

The useful thing for putting id attributes on headings is that an author can then link to those headings.

See this brew for an example: https://homebrewery.naturalcrit.com/share/SyXKaZmQM
Every maneuvre listed on the 1st page links to the relevant section in the text.

This only remains useful if the id values don't change.

Adding or removing a page early in the brew will cause all the headings in this solution to change. And not in an obviously broken way either (i.e. clicking the link to #p7-H3-header-3 will still link to a header with that id .. despite the intended destination now being located on p8 or p6).

@dbolack-ab
Copy link
Collaborator Author

Okay. I'll go to plan b.

dbolacksn and others added 2 commits January 22, 2024 18:58
This uses the GFMHeaderID Profix feaure to tag automatically generated
IDs and then swap them out with Page and Header Type prefixes to result
in a render unique tag.
@calculuschild calculuschild temporarily deployed to homebrewery-pr-3262 January 25, 2024 22:11 Inactive
@calculuschild
Copy link
Member

@ericscheid This now keeps the original ID from users, but still applies a page number and counter: #p1-myID-1.

Is this any better for your use case, or do you have an alternate preferred behavior? I understand this may still be an issue when deleting or adding pages because the #pX will change, but at least myID will remain intact.

@calculuschild calculuschild added 🔍 R1 - Reviewed - Needs Discussion 💬 PR not okayed yet; needs reevaluation or closure and removed 🔍 R0 - Needs first review 👀 PR ready but has not been reviewed labels Jan 26, 2024
@ericscheid
Copy link
Collaborator

@ericscheid This now keeps the original ID from users, but still applies a page number and counter: #p1-myID-1.

Is this any better for your use case, or do you have an alternate preferred behavior? I understand this may still be an issue when deleting or adding pages because the #pX will change, but at least myID will remain intact.

This is no better. The id still changes, and only exact matches are used for linking.

The numbering will also change if headings are inserted or deleted or rearranged on a given page.

@calculuschild
Copy link
Member

calculuschild commented Jan 27, 2024

The numbering will also change if headings are inserted or deleted or rearranged on a given page.

Well, this is going to be true of any solution that creates unique identifiers for duplicated names. Rearranging the headings in text would always correspond to a rearrangement of the unique IDs being generated, no?

Unless I'm missing something, what naming scheme would you propose that is immune to reordering of the text?

Or maybe if you don't mind making a short example document showing what rearrangements of headers you expect to be stable? So we are all working from the same requirements.

@ericscheid
Copy link
Collaborator

The numbering will also change if headings are inserted or deleted or rearranged on a given page.

Well, this is going to be true of any solution that creates unique identifiers for duplicated names. Rearranging the headings in text would always correspond to a rearrangement of the unique IDs being generated, no?

Unless I'm missing something, what naming scheme would you propose that is immune to reordering of the text?

Or maybe if you don't mind making a short example document showing what rearrangements of headers you expect to be stable? So we are all working from the same requirements.

The issue isn't the instability of "unique identifiers for duplicated names", but rather that even for non-duplicated names the ids are unstable. For example .. this document

## foo
## bar 
## baz

should have the same ids as

## baz
## bar 
## foo

and even this

## foo
### bar 
#### baz

@calculuschild
Copy link
Member

I see
And ideally this should persist across pages, no?

### foo

\page

## bar
## baz

@ericscheid
Copy link
Collaborator

I see, and ideally this should persist across pages, no?

Absolutely.

Bottom line: the fix for one specific case shouldn't break a more general case.

@dbolack-ab
Copy link
Collaborator Author

@ericscheid This now keeps the original ID from users, but still applies a page number and counter: #p1-myID-1.
Is this any better for your use case, or do you have an alternate preferred behavior? I understand this may still be an issue when deleting or adding pages because the #pX will change, but at least myID will remain intact.

This is no better. The id still changes, and only exact matches are used for linking.

The numbering will also change if headings are inserted or deleted or rearranged on a given page.

There is no way to prevent this other than by the user forcing an id via { }.

Even if we could maintain a global list without massaging the IDs ( similar to how we have in the PR for reflinks ) nothing guarantees the integrity of auto-generated header ID reflinks staying valid. It is an unreliable method of internal document linking and should be discouraged. Even in a single-page document.

@ericscheid
Copy link
Collaborator

There is no way to prevent this other than by the user forcing an id via { }.

Expanding the scope of a problem from specific to general cannot be excused by saying there's already a problem with an edge case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔍 R1 - Reviewed - Needs Discussion 💬 PR not okayed yet; needs reevaluation or closure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants