Add mentions
rodzic
f95a23af9e
commit
838c071cee
|
@ -20,6 +20,7 @@ Optionally, upload an `icon.png` to make the user look nice.
|
|||
* The `index.php` file performs a specific action depending on the path requested.
|
||||
* Log files are saved as .txt in the `/logs` directory.
|
||||
* Post files are saved as .json in the `/posts` directory.
|
||||
* This has sloppy support for linking #hashtags, https:// URls, and @ mentions.
|
||||
|
||||
## Requirements
|
||||
|
||||
|
|
36
index.php
36
index.php
|
@ -485,8 +485,8 @@ HTML;
|
|||
// Get any hashtags
|
||||
$hashtags = [];
|
||||
$hashtag_pattern = '/(?:^|\s)\#(\w+)/'; // Beginning of string, or whitespace, followed by #
|
||||
preg_match_all( $hashtag_pattern, $content, $matches );
|
||||
foreach ($matches[1] as $match) {
|
||||
preg_match_all( $hashtag_pattern, $content, $hashtag_matches );
|
||||
foreach ($hashtag_matches[1] as $match) {
|
||||
$hashtags[] = $match;
|
||||
}
|
||||
|
||||
|
@ -501,15 +501,43 @@ HTML;
|
|||
|
||||
// Add HTML links for hashtags into the text
|
||||
$content = preg_replace(
|
||||
'/(?<!\S)#([0-9\p{L}]+)/u',
|
||||
$hashtag_pattern,
|
||||
"<a href='https://{$server}/tag/$1'>#$1</a>",
|
||||
$content
|
||||
);
|
||||
|
||||
// Detect user mentions
|
||||
$usernames = [];
|
||||
$usernames_pattern = '/@(\S+)@(\S+)/'; // This is a *very* sloppy regex
|
||||
preg_match_all( $usernames_pattern, $content, $usernames_matches );
|
||||
foreach ( $usernames_matches[0] as $match ) {
|
||||
$usernames[] = $match;
|
||||
}
|
||||
|
||||
// Construct the mentions value for the note object
|
||||
// This goes in the generic "tag" property
|
||||
// TODO: Add this to the CC field
|
||||
foreach ( $usernames as $username ) {
|
||||
list( $null, $user, $domain ) = explode( "@", $username );
|
||||
$tags[] = array(
|
||||
"type" => "Mention",
|
||||
"href" => "https://{$domain}/@{$user}",
|
||||
"name" => "{$username}"
|
||||
);
|
||||
|
||||
// Add HTML links to usernames
|
||||
$username_link = "<a href=\"https://{$domain}/@{$user}\">$username</a>";
|
||||
$content = str_replace( $username, $username_link, $content );
|
||||
|
||||
}
|
||||
|
||||
// Construct the content
|
||||
$content = "<p>{$content}</p>";
|
||||
|
||||
return ["HTML" => $content, "TagArray" => $tags];
|
||||
return [
|
||||
"HTML" => $content,
|
||||
"TagArray" => $tags
|
||||
];
|
||||
}
|
||||
|
||||
// "One to stun, two to kill, three to make sure"
|
||||
|
|
Ładowanie…
Reference in New Issue