-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace the header_links plugin with client-side generated anchors. (#…
…4165) * Replace the header_links plugin with client-side generated anchors. Fixes #4124 * Move anchor-link code into a separate script Also adds a couple comments, for context.
- Loading branch information
1 parent
737dac4
commit 7a878d2
Showing
4 changed files
with
32 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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,30 @@ | ||
// Add anchors to headings client-side, which prevents them from showing up | ||
// in RSS feeds. See https://github.com/facebook/react/issues/4124. | ||
(function() { | ||
var selector = '.inner-content h2, .inner-content h3, .inner-content h4'; | ||
var elements = document.querySelectorAll(selector); | ||
for (var i = 0; i < elements.length; i++) { | ||
var textMethod = document.body.textContent ? "textContent" : "innerText"; | ||
var roughText = elements[i][textMethod]; | ||
|
||
// Regex rule for making the title URL-friendly. | ||
var urlFriendlyText = roughText.trim() | ||
.toLowerCase() | ||
.replace(/\s+/g, '-') | ||
.replace(/[^A-Za-z0-9\-_.\p{Cyrillic}\p{Hangul}\p{Hiragana}\p{Katakana}\p{Han}]/g, ''); | ||
|
||
// Create the anchor we'll jump to. | ||
var anchor = document.createElement('a'); | ||
anchor.className = 'anchor'; | ||
anchor.name = urlFriendlyText; | ||
elements[i].insertBefore(anchor, elements[i].firstChild); | ||
|
||
// Create the clickable "#" icon. | ||
var hashLink = document.createElement('a'); | ||
var icon = document.createTextNode("#"); | ||
hashLink.appendChild(icon); | ||
hashLink.className = 'hash-link'; | ||
hashLink.href = '#' + urlFriendlyText; | ||
elements[i].appendChild(hashLink); | ||
} | ||
}()); |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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