From 00a78af84cbbe3cb059a53341049f9a7039d3b8a Mon Sep 17 00:00:00 2001 From: Terence Eden Date: Fri, 16 Feb 2024 16:09:05 +0000 Subject: [PATCH] Add hashtag support --- index.php | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index d7c9b01..ff8e73b 100644 --- a/index.php +++ b/index.php @@ -367,6 +367,9 @@ HTML; // Get the posted content $content = $_POST["content"]; + // Process the content into HTML to get hashtags etc + list( "HTML" => $content, "TagArray" => $tags ) = process_content( $content ); + // Current time - ISO8601 $timestamp = date( "c" ); @@ -386,7 +389,8 @@ HTML; "attributedTo" => "https://{$server}/{$username}", "content" => $content, "contentMap" => ["en" => $content], - "to" => ["https://www.w3.org/ns/activitystreams#Public"] + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "tag" => $tags ]; // Construct the Message @@ -462,6 +466,40 @@ HTML; die(); } + // 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 + function process_content( $content ) { + global $server; + + // Get any hashtags + $hashtags = []; + preg_match_all( '/#(\w+)/', $content, $matches ); + foreach ($matches[1] as $match) { + $hashtags[] = $match; + } + + // Construct the tag value for the note object + $tags = []; + foreach ( $hashtags as $hashtag ) { + $tags[] = array( + "type" => "Hashtag", + "name" => "#{$hashtag}", + ); + } + + // Add HTML links for hashtags into the text + $content = preg_replace( + '/(?#$1", + $content + ); + + // Construct the content + $content = "

{$content}

"; + + return ["HTML" => $content, "TagArray" => $tags]; + } + // "One to stun, two to kill, three to make sure" die(); die();