Add URl links and better hashtag support

merge-requests/5/head
Terence Eden 2024-02-17 09:30:50 +00:00
rodzic 00a78af84c
commit cddbe3e1c5
1 zmienionych plików z 12 dodań i 2 usunięć

Wyświetl plik

@ -7,6 +7,7 @@
* For educational purposes only.
* It produces an Actor who can be followed.
* It can send messages to followers.
* Those messages can have linkable URls and hashtags.
* It saves logs about requests it receives and sends.
* It is NOT suitable for production use.
* This code is "licenced" under CRAPL v0 - https://matt.might.net/articles/crapl/
@ -467,13 +468,22 @@ HTML;
}
// Content can be plain text. But to add clickable links and hashtags, it needs to be turned into HTML.
// Tags are also included separately in the message
// Tags are also included separately in the note
function process_content( $content ) {
global $server;
// Convert any URls into hyperlinks
$link_pattern = '/\bhttps?:\/\/\S+/iu';
$replacement = function ( $match ) {
$url = htmlspecialchars( $match[0], ENT_QUOTES, "UTF-8" );
return "<a href=\"$url\">$url</a>";
};
$content = preg_replace_callback( $link_pattern, $replacement, $content );
// Get any hashtags
$hashtags = [];
preg_match_all( '/#(\w+)/', $content, $matches );
$hashtag_pattern = '/(?:^|\s)\#(\w+)/'; // Beginning of string, or whitespace, followed by #
preg_match_all( $hashtag_pattern, $content, $matches );
foreach ($matches[1] as $match) {
$hashtags[] = $match;
}