Skip to content

Commit 806eba7

Browse files
Add Bluesky shortcode support
1 parent fcfc088 commit 806eba7

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Here is the list of the available parsers:
160160
| Shortcode | Description | Parameters | Example |
161161
|----------------|-------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
162162
| `[twitter] ` | Get a Twitter card | `url` | `[twitter url="https://twitter.com/elonmusk/status/1585841080431321088"]` |
163+
| `[bluesky] ` | Get a BlueSky card | `url` | `[bluesky url="https://bsky.app/profile/adamparkhomenko.bsky.social/post/3liz5p73u6k2f"]` |
163164
| `[youtube]` | Get a Youtube (light) player | `url` | `[youtube url="https://www.youtube.com/watch?v=9bZkp7q19f0"]` |
164165
| `[spotify]` | Get a Spotify player | `url` or `uri` | `[spotify url="https://open.spotify.com/track/2TpxZ7JUBn3uw46aR7qd6V"]` |
165166
| `[faq]` | Create a `<details>` tag with embedded content | `title` | `[faq title="What is the answer to the ultimate question?"]42[/faq]` |

resources/src/css/app.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ html.dark .shortcode_tmdb .top-box .movie-info .release-date {
552552
margin-left: 3px;
553553
}
554554

555-
.twitter-card blockquote {
555+
.twitter-card blockquote,
556+
.bsky-card blockquote {
556557
display: none;
557558
}
558559

resources/views/bluesky.blade.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@if($html)
2+
<div class="flex justify-center bsky-card">
3+
{!! $html !!}
4+
</div>
5+
<aside class="shortcode_nocookie">
6+
{!! config('shortcode-plus.nocookie.text') !!} - bsky.app
7+
</aside>
8+
@else
9+
<p>Sorry, no post found.</p>
10+
@endif

src/LaravelShortcodePlusServiceProvider.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Murdercode\LaravelShortcodePlus;
44

55
use Illuminate\Support\Facades\Blade;
6+
use Murdercode\LaravelShortcodePlus\Shortcodes\BlueskyShortcode;
67
use Murdercode\LaravelShortcodePlus\Shortcodes\ButtonShortcode;
78
use Murdercode\LaravelShortcodePlus\Shortcodes\DisticoShortcode;
89
use Murdercode\LaravelShortcodePlus\Shortcodes\FacebookShortcode;
@@ -42,7 +43,9 @@ public function configurePackage(Package $package): void
4243
->hasMigration('create_laravel-shortcode-plus_table');
4344
}
4445

45-
public function packageRegistered() {}
46+
public function packageRegistered()
47+
{
48+
}
4649

4750
public function packageBooted(): void
4851
{
@@ -79,5 +82,6 @@ public function register(): void
7982
Shortcode::register('tiktok', TikTokShortcode::class);
8083
Shortcode::register('survey', SurveyShortcode::class);
8184
Shortcode::register('trivia', TriviaShortcode::class);
85+
Shortcode::register('bluesky', BlueskyShortcode::class);
8286
}
8387
}

src/Shortcodes/BlueskyShortcode.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Murdercode\LaravelShortcodePlus\Shortcodes;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use GuzzleHttp\Exception\RequestException;
8+
9+
class BlueskyShortcode
10+
{
11+
public function register($shortcode): string
12+
{
13+
$url = $shortcode->url ?? '';
14+
15+
if (empty($url)) {
16+
return 'No bsky.app parameter url defined';
17+
}
18+
19+
if (str_contains($url, 'bsky.app') === false && str_contains($url, 'bsky.app') === false) {
20+
return 'No bsky.app URL defined';
21+
}
22+
23+
$html = self::getOembed($url) ?? null;
24+
25+
if (!isset($html)) {
26+
return 'Cannot get bsky.app oEmbed';
27+
}
28+
29+
return view('shortcode-plus::bluesky', compact('html'))->render();
30+
}
31+
32+
/**
33+
* Get oEmbed data from Twitter
34+
* Note: Twitter sometimes returns 404 for valid URLs, so we retry a few times
35+
*
36+
* @throws GuzzleException
37+
*/
38+
private static function getOembed(string $url): ?string
39+
{
40+
$maxAttempts = 3;
41+
$attempt = 0;
42+
$response = null;
43+
44+
while ($attempt < $maxAttempts && $response === null) {
45+
try {
46+
$client = new Client;
47+
$res = $client->request('GET', 'https://embed.bsky.app/oembed', [
48+
'query' => [
49+
'url' => $url,
50+
],
51+
]);
52+
53+
if ($res->getStatusCode() == 200) {
54+
$response = $res->getBody()->getContents();
55+
} else {
56+
usleep(100000);
57+
$attempt++;
58+
}
59+
} catch (RequestException $e) {
60+
usleep(100000);
61+
$attempt++;
62+
}
63+
}
64+
65+
if ($response === null) {
66+
return null;
67+
}
68+
69+
$data = json_decode($response, true);
70+
71+
return $data['html'] ?? null;
72+
}
73+
}

0 commit comments

Comments
 (0)