janboddez / share-on-mastodon

Easily share WordPress posts on Mastodon.
https://jan.boddez.net/wordpress/share-on-mastodon
GNU General Public License v3.0
39 stars 5 forks source link

Small issue with footnotes #70

Closed edent closed 1 year ago

edent commented 1 year ago

Note sure if this is a problem with the plugin or WordPress.

This post contains Markdown Footnotes: https://shkspr.mobi/blog/2023/05/how-can-i-have-a-side-hustle-when-i-dont-want-to-pay-for-anything/

I've been reading various entrepreneur books and blog posts[^bad]. One thing they all emphasise is that success often comes from finding a problem that you yourself would pay money to solve.
[^bad]: I am aware that they paint a distorted picture of reality.

When shared to Mastodon, it gets rendered as I've been reading various entrepreneur books and blog posts1. One thing they all ...

That is, the 1 becomes a 1.

See https://mastodon.social/@Edent/110304539708556169

janboddez commented 1 year ago

So, the callback function you use to generate the status content must parse shortcodes (or Markdown, whatever). It probably outputs <sup>1</sup> or similar. Not sure if you then strip those tags or if Mastodon does (I think the former, Mastodon tends to just escape HTML it does not support).

You could probably adjust the callback to delete the footnote links altogether. Or stop it from parsing shortcodes and output the raw Markdown. Or …

janboddez commented 1 year ago

I.e., the plugin by default only posts a title and permalink. Anything beyond that is the product of a custom callback. If that callback itself calls, e.g., the_content(), then it will also apply all relevant filters …

Edit: I first wrote get_the_content() but that function does not filter $post->post_content.

janboddez commented 1 year ago

@edent Saw another occurrence of this on your Mastodon account, and got reminded of this issue. Wanted to check out https://shkspr.mobi/blog/2022/11/better-sharing-of-wordpress-posts-to-mastodon/ to see if I could help improve your callback function (something like preg_replace( '~<sup[^>]*>.*?</sup>~', '', $status )), but apparently the post content got replaced with a sort of Mastodon embed.

edent commented 1 year ago

Wooops! Think I've fixed it.

edent commented 1 year ago

A good blog post to look at might be https://shkspr.mobi/blog/2023/06/your-phone-is-a-cdo/

Which was auto shared as https://mastodon.social/@Edent/110576330656136247

janboddez commented 1 year ago

@edent That's the one I saw!

So, this is not an issue with the plugin. What happens is get_the_excerpt($post) (in your share_on_mastodon_status filter callback) strips all HTML.

In your HTML, there's something like, in this case, <sup id="fnref-43577-pro"><a href="#fn-43577-pro" class="jetpack-footnote" title="Read footnote.">1</a></sup>. And get_the_excerpt turns that particular bit of HTML into ... 1. So that's why it shows like that on Mastodon.

A possible solution is to "roll your own" excerpts, i.e., get $post->post_content, run it through the the_content filters, then appy a preg_replace() similar to the one above to get rid of the footnote links prior to stripping all HTML tags, and, finally, shorten it.

janboddez commented 1 year ago
// Dynamically generate an excerpt off the post content.
$excerpt = apply_filters( 'the_content', $post->post_content );
// Remove footnotes.
$excerpt = preg_replace( '~<sup[^>]*>.*?</sup>~', '', $excerpt );
// Actually generate the excerpt. This calls `wp_trim_words()` under the hood, which strips away HTML tags.
$excerpt = wp_trim_excerpt( $excerpt );

Or maybe you could temporarily unhook the Jetpack (?) filter/action that is responsible for generating the footnotes or something. (But you might then end up with something else in your markup that you don't necessarily want there.)

Untested, but you get the idea. And you could then use that "excerpt" instead.