-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Trim footnote anchors from excerpts #52518
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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,25 @@ | ||
<?php | ||
/** | ||
* Compatiblity filters for improved footnotes support. | ||
* | ||
* In core, this could be fixed directly in key functions. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Trims footnote anchors from content. | ||
* | ||
* @param string $content HTML content. | ||
* @return string Filtered content. | ||
*/ | ||
function gutenberg_trim_footnotes( $content ) { | ||
if ( ! doing_filter( 'get_the_excerpt' ) ) { | ||
return $content; | ||
} | ||
|
||
static $footnote_pattern = '_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_'; | ||
return preg_replace( $footnote_pattern, '', $content ); | ||
} | ||
|
||
add_filter( 'the_content', 'gutenberg_trim_footnotes' ); |
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
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.
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.
This is working pretty well, thanks! I'm not sure what else we could do here.
Looks like it handles most of the inline changes we can make to the footnote sup
I only think with regexes like these, especially for new functionality, it would be great to have unit tests. That way when breaking changes come down the line we can react immediately.
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.
Also tested in Chinese and Cyrillic scripts just to make sure no i18n funny business was messing with things.
Looks good!
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.
Could we use the HTML tag processor here? Cc @dmsnell
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.
Also interesting to know this.
When doing the backport I had a quick look at whether it could remove tags like it could with attributes/classes and I couldn't find anything.
The other option would be a DOM parser to strip the
<sup />
and its contents but not sure if that would be a faster way to do it.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.
Is there any other possible tag inside of here besides
<sup>
and<a>
?The Tag Processor cannot easily remove this pattern, but the HTML Processor would be able to if I added
<sup>
support and the ability to remove a tag.This could all plausibly happen for the WordPress 6.4 release, no promises.
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.
Also there's an "ugly" way to do it with the Tag Processor by subclassing and accessing internals, but the HTML Processor is working well enough I'm not experimenting with that approach anymore.
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 don't believe so because
WP_HTML_Tag_Processor
doesn't have aremove_tag
method. If it did, you could do something like this:But @dmsnell would know better 😄
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.
(Oops race condition)
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'm just flagging this with @dmsnell as another place we're processing HTML 😄