diff options
Diffstat (limited to 'Digitigrade/Model/Note.php')
| -rw-r--r-- | Digitigrade/Model/Note.php | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php index d5d4e69..ac9cc3c 100644 --- a/Digitigrade/Model/Note.php +++ b/Digitigrade/Model/Note.php @@ -4,12 +4,16 @@ namespace Digitigrade\Model; use Digitigrade\Db; use Digitigrade\FormattingProvider\CommonMark; use Digitigrade\FormattingProvider\SanitisedHtml; +use Digitigrade\LinkPreview; use Digitigrade\Model\HomeTimelineItem; use Digitigrade\Notification\Notifyable; use Digitigrade\Timeline\TimelineIncludeable; use Digitigrade\Timeline\TimelineItem; +use JsonMapper; class Note extends PushableModel implements TimelineIncludeable, Notifyable { + public const EXTENSION_LINK_PREVIEWS = 'https://pawpub.entities.org.uk/extension/link-previews'; + public ?int $id; public string $uri; public \DateTimeImmutable $created; @@ -265,6 +269,45 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable { return htmlspecialchars($this->plainContent); } + /** + * Gets a list of LinkPreviews applicable to this note + * @return LinkPreview[] + */ + public function getLinkPreviews(): array { + if (($this->extensions[self::EXTENSION_LINK_PREVIEWS] ?? []) == []) { + return []; + } + $mapper = new JsonMapper(); + $results = []; + foreach ($this->extensions[self::EXTENSION_LINK_PREVIEWS] as $preview) { + $results[] = $mapper->map($preview, LinkPreview::class); + } + return $results; + } + + /** + * Generates new LinkPreviews for this note (but does not save them automatically) + * @return LinkPreview[] the new link previews + */ + public function generateLinkPreviews(): array { + $matches = []; + preg_match_all( + '`[a-z+]+://[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(([-a-zA-Z0-9@:(%_\+.~#?&/=]|\)[^\s])*)`', + $this->getFormattedContent('text/markdown') ?? $this->plainContent, + $matches + ); + $urls = $matches[0] ?? []; + $previews = array_filter( + array_map(fn($url) => LinkPreview::generate($url), $urls), + fn($p) => $p != null + ); + $this->extensions[self::EXTENSION_LINK_PREVIEWS] = array_map( + fn($p) => json_decode(json_encode($p)), + $previews + ); + return $previews; + } + public function renderAsHtml() { render_template('note', ['note' => $this]); } @@ -345,7 +388,7 @@ class Note extends PushableModel implements TimelineIncludeable, Notifyable { 'description' => $attachment->description ]; }, $this->attachments), - //'extensions' => [] + 'extensions' => $this->extensions ]; } }
\ No newline at end of file |
