Avoid replacing mentions inside links

This commit is contained in:
Alex Kirk 2022-12-13 11:34:07 +01:00
parent fed4fcb5b4
commit 840d144327
3 changed files with 31 additions and 0 deletions

View file

@ -23,8 +23,22 @@ class Mention {
* @return string the filtered post-content
*/
public static function the_content( $the_content ) {
$protected_tags = array();
$the_content = preg_replace_callback(
'#<a.*?href=[^>]+>.*?</a>#i',
function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
},
$the_content
);
$the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
$the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content );
return $the_content;
}

View file

@ -453,8 +453,22 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
public function the_content( $the_content ) {
$protected_tags = array();
$the_content = preg_replace_callback(
'#<a.*?href=[^>]+>.*?</a>#i',
function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
},
$the_content
);
$the_content = \preg_replace_callback( '/@(?:[a-zA-Z0-9_-]+)/', array( $this, 'replace_with_links' ), $the_content );
$the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content );
return $the_content;
}

View file

@ -21,6 +21,9 @@ class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP {
return array(
array( 'hallo @username@example.org test', 'hallo <a rel="mention" class="u-url mention" href="https://example.org/users/username">@<span>username</span></a> test' ),
array( 'hallo @pfefferle@notiz.blog test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span></a> test' ),
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test' ),
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@pfefferle@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@pfefferle@notiz.blog</a> test' ),
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@pfefferle@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@pfefferle@notiz.blog</a> test' ),
);
}