Skip to content

Commit 9d7e511

Browse files
authored
Release Notes for Desktop (#98)
1 parent db6f922 commit 9d7e511

File tree

4 files changed

+137
-8
lines changed

4 files changed

+137
-8
lines changed

app/Http/Controllers/ShowDocumentationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ protected function extractTableOfContents(string $document): array
238238
->map(function (string $line) {
239239
return [
240240
'level' => strlen(trim(Str::before($line, '# '))) + 1,
241-
'title' => $title = trim(Str::after($line, '# ')),
241+
'title' => $title = htmlspecialchars_decode(trim(Str::after($line, '# '))),
242242
'anchor' => Str::slug(Str::replace('`', 'code', $title)),
243243
];
244244
})

app/Support/GitHub.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Support;
44

5+
use App\Support\GitHub\Release;
6+
use Illuminate\Support\Collection;
57
use Illuminate\Support\Facades\Cache;
68
use Illuminate\Support\Facades\Http;
79

@@ -27,18 +29,25 @@ public static function laravel(): static
2729

2830
public function latestVersion()
2931
{
30-
$version = Cache::remember(
32+
$release = Cache::remember(
3133
$this->getCacheKey('latest-version'),
3234
now()->addHour(),
33-
function () {
34-
return $this->fetchLatestVersion();
35-
}
35+
fn () => $this->fetchLatestVersion()
3636
);
3737

38-
return $version['name'] ?? 'Unknown';
38+
return $release?->name ?? 'Unknown';
3939
}
4040

41-
private function fetchLatestVersion()
41+
public function releases(): Collection
42+
{
43+
return Cache::remember(
44+
$this->getCacheKey('releases'),
45+
now()->addHour(),
46+
fn () => $this->fetchReleases()
47+
) ?? collect();
48+
}
49+
50+
private function fetchLatestVersion(): ?Release
4251
{
4352
// Make a request to GitHub
4453
$response = Http::get('https://api.github.com/repos/'.$this->package.'/releases/latest');
@@ -48,11 +57,24 @@ private function fetchLatestVersion()
4857
return null;
4958
}
5059

51-
return $response->json();
60+
return new Release($response->json());
5261
}
5362

5463
private function getCacheKey(string $string): string
5564
{
5665
return sprintf('%s-%s', $this->package, $string);
5766
}
67+
68+
private function fetchReleases(): ?Collection
69+
{
70+
// Make a request to GitHub
71+
$response = Http::get('https://api.github.com/repos/'.$this->package.'/releases');
72+
73+
// Check if the request was successful
74+
if ($response->failed()) {
75+
return collect();
76+
}
77+
78+
return collect($response->json())->map(fn (array $release) => new Release($release));
79+
}
5880
}

app/Support/GitHub/Release.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace App\Support\GitHub;
4+
5+
/**
6+
* @property string $url
7+
* @property string $assets_url
8+
* @property string $upload_url
9+
* @property string $html_url
10+
* @property int $id
11+
* @property array $author
12+
* @property string $node_id
13+
* @property string $tag_name
14+
* @property string $target_commitish
15+
* @property string $name
16+
* @property bool $draft
17+
* @property bool $prerelease
18+
* @property string $created_at
19+
* @property string $published_at
20+
* @property array $assets
21+
* @property string $tarball_url
22+
* @property string $zipball_url
23+
* @property string $body
24+
* @property int $mentions_count
25+
*/
26+
class Release
27+
{
28+
private bool $withUserLinks = true;
29+
30+
private bool $convertLinks = true;
31+
32+
public function __construct(
33+
private array $data
34+
) {
35+
//
36+
}
37+
38+
public function __get(string $name)
39+
{
40+
return $this->data[$name] ?? null;
41+
}
42+
43+
public function __isset(string $name): bool
44+
{
45+
return isset($this->data[$name]);
46+
}
47+
48+
public function getBodyForMarkdown(): string
49+
{
50+
$body = $this->body;
51+
52+
// Convert any URLs to Markdown links
53+
if ($this->convertLinks) {
54+
$body = preg_replace(
55+
'/https?:\/\/[^\s]+\/pull\/(\d+)/',
56+
'[#$1]($0)',
57+
$body
58+
);
59+
60+
$body = preg_replace(
61+
'/(https?:\/\/(?![^\s]+\/pull\/\d+)[^\s]+)/',
62+
'[$1]($1)',
63+
$body
64+
);
65+
}
66+
67+
// Change any @ tags to markdown links to GitHub
68+
if ($this->withUserLinks) {
69+
$body = preg_replace(
70+
'/@([a-zA-Z0-9_]+)/',
71+
'[@$1](https://github.com/$1)',
72+
$body
73+
);
74+
}
75+
76+
return preg_replace('/^#/m', '###', $body);
77+
}
78+
79+
public function withoutUserLinks(bool $withoutUserLinks = true): static
80+
{
81+
$this->withUserLinks = ! $withoutUserLinks;
82+
83+
return $this;
84+
}
85+
86+
public function withoutLinkConversion(bool $convertLinks = true): static
87+
{
88+
$this->convertLinks = ! $convertLinks;
89+
90+
return $this;
91+
}
92+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Release Notes
3+
order: 1100
4+
---
5+
6+
@forelse (\App\Support\GitHub::electron()->releases()->take(10) as $release)
7+
## {{ $release->name }}
8+
**Released: {{ \Carbon\Carbon::parse($release->published_at)->format('F j, Y') }}**
9+
10+
{{ $release->getBodyForMarkdown() }}
11+
---
12+
@empty
13+
## We couldn't show you the latest release notes at this time.
14+
Not to worry, you can head over to GitHub to see the [latest release notes](https://github.com/NativePHP/electron/releases).
15+
@endforelse

0 commit comments

Comments
 (0)