-
Notifications
You must be signed in to change notification settings - Fork 851
Fix inline markup leaking into docs heading anchors #18197
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Shared helpers for turning heading text into anchor slugs. | ||
| // | ||
| // Some headings embed inline markup (an icon <span>, a beta label, etc.). Left | ||
| // untouched, that markup leaks into generated anchor IDs — e.g. a heading like | ||
| // `## <span ...><IconToggle /></span> Feature flags` would produce an ID like | ||
| // `span-classnameinline-blockicontoggle-...-feature-flags` instead of | ||
| // `feature-flags`. Stripping the markup before slugging keeps section links clean | ||
| // and consistent across the element ID, the table of contents, and search. | ||
|
|
||
| const GithubSlugger = require('github-slugger') | ||
|
|
||
| // Matches a balanced pair of inline HTML/JSX tags, e.g. `<span ...>...</span>`. | ||
| // Kept identical to the regex used by `formatToc` in gatsby/createPages.ts so the | ||
| // element ID, the sidebar table of contents, and Algolia fragments all agree. | ||
| const INLINE_MARKUP_REGEX = /\s*<([a-z]+).+?>.+?<\/\1>/g | ||
|
|
||
| // Remove inline markup and trim, so a heading like `<span>...</span> Feature flags` | ||
| // yields `Feature flags` rather than ` Feature flags` (a leading space would otherwise | ||
| // slug to `-feature-flags`). After removing balanced tag pairs, any stray angle | ||
| // brackets are dropped too so no partial/malformed tag can survive — the result is | ||
| // always safe as plain text (this also feeds github-slugger, which strips the rest). | ||
| const stripHeadingMarkup = (value = '') => | ||
| typeof value === 'string' ? value.replace(INLINE_MARKUP_REGEX, '').replace(/[<>]/g, '').trim() : '' | ||
Check failureCode scanning / CodeQL Incomplete multi-character sanitization High
This string may still contain
<script Error loading related location Loading |
||
|
|
||
| // Slugify a heading's text, ignoring any inline markup it contains. Pass an | ||
| // existing GithubSlugger instance to keep per-page de-duplication counters in sync. | ||
| const slugifyHeading = (value, slugger = new GithubSlugger()) => slugger.slug(stripHeadingMarkup(value)) | ||
|
|
||
| module.exports = { stripHeadingMarkup, slugifyHeading, INLINE_MARKUP_REGEX } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| const visit = require('unist-util-visit') | ||
| const GithubSlugger = require('github-slugger') | ||
| const { slugifyHeading } = require('../../gatsby/utils/headingSlug') | ||
|
|
||
| // Concatenate a node's text content, mirroring `mdast-util-to-string`. Inline | ||
| // HTML/JSX nodes expose their raw markup as `value`, which is exactly what we | ||
| // want to feed to the slugger (and then strip) so this matches what | ||
| // `gatsby-remark-autolink-headers` would otherwise produce. | ||
| const getNodeText = (node) => { | ||
| if (typeof node.value === 'string') { | ||
| return node.value | ||
| } | ||
| if (Array.isArray(node.children)) { | ||
| return node.children.map(getNodeText).join('') | ||
| } | ||
| return '' | ||
| } | ||
|
|
||
| // Runs BEFORE gatsby-remark-autolink-headers to assign a clean anchor ID to every | ||
| // heading, stripping any inline markup (icon spans, beta labels, etc.) so it never | ||
| // leaks into the URL. autolink-headers only patches an ID when one isn't already | ||
| // set, so the value we write here wins. | ||
| module.exports = ({ markdownAST }) => { | ||
| const slugger = new GithubSlugger() | ||
|
|
||
| visit(markdownAST, 'heading', (node) => { | ||
| const id = slugifyHeading(getNodeText(node), slugger) | ||
|
|
||
| node.data = node.data || {} | ||
| node.data.id = id | ||
| node.data.htmlAttributes = node.data.htmlAttributes || {} | ||
| node.data.htmlAttributes.id = id | ||
| node.data.hProperties = node.data.hProperties || {} | ||
| node.data.hProperties.id = id | ||
| }) | ||
|
|
||
| return markdownAST | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "name": "gatsby-remark-heading-slug", | ||
| "version": "0.1.0", | ||
| "main": "index.js" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.