Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/ShowDocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ protected function extractTableOfContents(string $document): array
->map(function (string $line) {
return [
'level' => strlen(trim(Str::before($line, '# '))) + 1,
'title' => $title = trim(Str::after($line, '# ')),
'title' => $title = htmlspecialchars_decode(trim(Str::after($line, '# '))),
'anchor' => Str::slug(Str::replace('`', 'code', $title)),
];
})
Expand Down
36 changes: 29 additions & 7 deletions app/Support/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Support;

use App\Support\GitHub\Release;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

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

public function latestVersion()
{
$version = Cache::remember(
$release = Cache::remember(
$this->getCacheKey('latest-version'),
now()->addHour(),
function () {
return $this->fetchLatestVersion();
}
fn () => $this->fetchLatestVersion()
);

return $version['name'] ?? 'Unknown';
return $release?->name ?? 'Unknown';
}

private function fetchLatestVersion()
public function releases(): Collection
{
return Cache::remember(
$this->getCacheKey('releases'),
now()->addHour(),
fn () => $this->fetchReleases()
) ?? collect();
}

private function fetchLatestVersion(): ?Release
{
// Make a request to GitHub
$response = Http::get('https://api.github.com/repos/'.$this->package.'/releases/latest');
Expand All @@ -48,11 +57,24 @@ private function fetchLatestVersion()
return null;
}

return $response->json();
return new Release($response->json());
}

private function getCacheKey(string $string): string
{
return sprintf('%s-%s', $this->package, $string);
}

private function fetchReleases(): ?Collection
{
// Make a request to GitHub
$response = Http::get('https://api.github.com/repos/'.$this->package.'/releases');

// Check if the request was successful
if ($response->failed()) {
return collect();
}

return collect($response->json())->map(fn (array $release) => new Release($release));
}
}
92 changes: 92 additions & 0 deletions app/Support/GitHub/Release.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace App\Support\GitHub;

/**
* @property string $url
* @property string $assets_url
* @property string $upload_url
* @property string $html_url
* @property int $id
* @property array $author
* @property string $node_id
* @property string $tag_name
* @property string $target_commitish
* @property string $name
* @property bool $draft
* @property bool $prerelease
* @property string $created_at
* @property string $published_at
* @property array $assets
* @property string $tarball_url
* @property string $zipball_url
* @property string $body
* @property int $mentions_count
*/
class Release
{
private bool $withUserLinks = true;

private bool $convertLinks = true;

public function __construct(
private array $data
) {
//
}

public function __get(string $name)
{
return $this->data[$name] ?? null;
}

public function __isset(string $name): bool
{
return isset($this->data[$name]);
}

public function getBodyForMarkdown(): string
{
$body = $this->body;

// Convert any URLs to Markdown links
if ($this->convertLinks) {
$body = preg_replace(
'/https?:\/\/[^\s]+\/pull\/(\d+)/',
'[#$1]($0)',
$body
);

$body = preg_replace(
'/(https?:\/\/(?![^\s]+\/pull\/\d+)[^\s]+)/',
'[$1]($1)',
$body
);
}

// Change any @ tags to markdown links to GitHub
if ($this->withUserLinks) {
$body = preg_replace(
'/@([a-zA-Z0-9_]+)/',
'[@$1](https://github.com/$1)',
$body
);
}

return preg_replace('/^#/m', '##', $body);
}

public function withoutUserLinks(bool $withoutUserLinks = true): static
{
$this->withUserLinks = ! $withoutUserLinks;

return $this;
}

public function withoutLinkConversion(bool $convertLinks = true): static
{
$this->convertLinks = ! $convertLinks;

return $this;
}
}
15 changes: 15 additions & 0 deletions resources/views/docs/desktop/1/getting-started/releasenotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Release Notes
order: 1100
---

@forelse (\App\Support\GitHub::electron()->releases()->take(10) as $release)
## {{ $release->name }}
**Released: {{ \Carbon\Carbon::parse($release->published_at)->format('F j, Y') }}**

{{ $release->getBodyForMarkdown() }}
---
@empty
## We couldn't show you the latest release notes at this time.
Not to worry, you can head over to GitHub to see the [latest release notes](https://github.com/NativePHP/electron/releases).
@endforelse