-
Notifications
You must be signed in to change notification settings - Fork 142
Fix heading indexing for pure html headings #966
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
Conversation
yamgent
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making this change. 2 things in particular:
- Since they can now be accessed via an id, maybe consider expanding the scope of this PR by also adding the anchor icon to these headings + allowing it to appear on page nav.
- Sometimes authors may want to exclude/include these headings from search index. For Markdown headings, this can be done with attributes. Possible to implement such a scenario for pure html headings?
01c4468 to
7fe28b1
Compare
My added code is just a "guard clause" to give headings with no
No changes were needed from the original code for this too I realised I didn't update the section of the user guide you linked though. Thanks! |
yamgent
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, my apologies, I got a bit confused just now when reading the code. Some comments:
| // Give pure html <h1..6> tags an id with the same slugify function used in markdown-it-anchor | ||
| if (!$(heading).attr('id')) { | ||
| const slugifiedHeading = slugify($(heading).text(), { decamelize: false }); | ||
| $(heading).attr('id', slugifiedHeading); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized that you placed these code on the anchor plugin's logic. If the author turns off this plugin by putting this in site.json:
"pluginsContext": {
"anchors": {
"off": true
}
}Then the IDs for these pure html headings no longer generate (while the markdown ones still get their IDs).
With that in mind, is it possible to put this logic in Page.js instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will move it to _parse instead.
Would it be better to also remove markdown-it-anchor entirely to standardise where the id is set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to also remove markdown-it-anchor entirely to standardise where the
idis set?
Erm I am not sure what you meant by this question. markdown-it-anchor is a plugin to add clickable anchor icons. All heading ids will always be present regardless of whether markdown-it-anchor is enabled or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
markdown-it-anchoris a plugin to add clickable anchor icons. All heading ids will always be present regardless of whethermarkdown-it-anchoris enabled or not.
Hmm, I think that's markbind-plugin-anchors.js
Specifically this line again :
$(heading).append(ANCHOR_HTML.replace('#', `#${$(heading).attr('id')}`));
I just tried commenting this line out in lib/markdown-it/index.js
//.use(require('markdown-it-anchor'), { slugify: (str) => slugify(str, { decamelize: false }) })
anchors remain even after doing so
Enabling the permalink: true option as described in their docs would give this icon instead by default
markdown-it-anchor is only being used in MarkBind to provide ids to markdown headers, I think. markbind-plugin-anchors handles adding the actual ship ⚓️ icon defined using ANCHOR_HTML.
The two sound very similar haha 😅 Could have highlighted the difference more clearly, my bad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for the clarification. Yes let's remove markdown-it-anchor then.
| ## Including or Excluding Headings | ||
|
|
||
| **You can specify headings which are to be included or excluded from the index built by MarkBind's built-in search feature** using the `.always-index` or `.no-index` attributes. | ||
| **You can specify headings which are to be included or excluded from the index built by MarkBind's built-in search feature** using the `.always-index` or `.no-index` classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't usually call {.always-index} and {.no-index} as classes, because it does not look like one from the author's point of view. Perhaps a good compromise would be to include both (a.k.a. "attributes/classes").



What is the purpose of this pull request? (put "X" next to an item, remove the rest)
• [x] Bug fix
What is the rationale for this request?
Currently, headings specified in markdown format (
#,##, ... ) are correctly indexed and given anid, enabling them to be searched.
Headings specified with pure html
h1 ... h6are not.Let's fix this, since pure html headings can be useful in nested doms / panel
slotheaders.What changes did you make? (Give an overview)
Provide some example code that this change will affect:
Is there anything you'd like reviewers to focus on?
Would like some confirmation with #967.
( could add on the fix to this pr if its fine, since both pertain to heading indexing )
Testing instructions:
Add any
<h123456>of your choice to any site. ( there's a<h1>Landing Page Title</h1>insrc\template\default\index.md)The heading should show up in the search bar, if it obeys all other search rules as well:
always-index/no-index<h123..>tags used as slot headers for panels should also now be indexed correctly when the panel has theexpandedattributeProposed commit message: (wrap lines at 72 characters)
Fix heading indexing for pure html headings