Skip to content

Commit

Permalink
WIP but gross
Browse files Browse the repository at this point in the history
  • Loading branch information
edgrosvenor committed May 29, 2024
1 parent 8c36f06 commit b8e2bc5
Show file tree
Hide file tree
Showing 17 changed files with 385 additions and 29 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"illuminate/contracts": "^10.0||^11.0",
"laravel/folio": "^v1.0",
"laravel/prompts": "^0.1",
"laravel/scout": "^v10.9",
"tempest/highlight": "2.0"
},
"require-dev": {
Expand Down
20 changes: 7 additions & 13 deletions config/docsidian.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

return [
'installed' => true,
// You can define as many documentation sites as you want below. Just make sure that each has unique paths and uri.
'sites' => [
'default' => [
'default_visibility' => '{{ default_visibility }}',
'folio_middleware' => ['web'],
'folio_path' => resource_path('{{ folio_path }}'),
'folio_uri' => '{{ folio_uri }}',
'layout' => 'docsidian',
'md_path' => base_path('{{ md_path }}'),
'md_repo' => 'artisan-build/docsidian-docs', // Only applies to hosted docs
],
],

'default_visibility' => '{{ default_visibility }}',
'folio_middleware' => ['web'],
'folio_path' => resource_path('{{ folio_path }}'),
'folio_uri' => '{{ folio_uri }}',
'layout' => 'docsidian',
'md_path' => base_path('{{ md_path }}'),
'obsidian_config' => base_path('{{ md_path }}/.obsidian')
];
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace ArtisanBuild\Docsidian\Database\Factories;

use ArtisanBuild\Docsidian\Models\DocsidianPage;
use Illuminate\Database\Eloquent\Factories\Factory;

/*
class ModelFactory extends Factory

class DocsidianPageFactory extends Factory
{
protected $model = YourModel::class;
protected $model = DocsidianPage::class;

public function definition()
{
Expand All @@ -16,4 +17,3 @@ public function definition()
];
}
}
*/
19 changes: 19 additions & 0 deletions database/factories/DocsidianSiteFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ArtisanBuild\Docsidian\Database\Factories;

use ArtisanBuild\Docsidian\Models\DocsidianSite;
use Illuminate\Database\Eloquent\Factories\Factory;


class DocsidianSiteFactory extends Factory
{
protected $model = DocsidianSite::class;

public function definition()
{
return [

];
}
}
34 changes: 32 additions & 2 deletions database/migrations/create_docsidian_table.php.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use ArtisanBuild\Docsidian\Models\DocsidianSite;
use ArtisanBuild\Docsidian\SiteStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -8,11 +10,39 @@ return new class extends Migration
{
public function up()
{
Schema::create('docsidian_table', function (Blueprint $table) {
Schema::create('docsidian_sites', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description')->nullable();
$table->string('image')->nullable();
$table->unsignedInteger('weight')->nullable();
$table->string('status')->default(SiteStatus::Hidden->name);
$table->string('default_visibility')->default('public');
$table->json('folio_middleware');
$table->string('folio_path');
$table->string('folio_uri');
$table->string('layout');
$table->string('md_path');
$table->string('obsidian_config');

// add fields
/*
* 'folio_middleware' => ['web'],
'folio_path' => resource_path('views/docs'),
'folio_uri' => 'docs',
'layout' => 'docsidian',
'md_path' => base_path('documentation'),
'md_repo' => 'artisan-build/docsidian-docs',
*/
$table->timestamps();
});

Schema::create('docsidian_pages', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(DocsidianSite::class);
$table->string('uri');
$table->text('public')->nullable();
$table->text('protected')->nullable();
$table->text('private')->nullable();
$table->timestamps();
});
}
Expand Down
56 changes: 56 additions & 0 deletions src/Commands/DiscoverCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace ArtisanBuild\Docsidian\Commands;

use ArtisanBuild\Docsidian\Models\DocsidianSite;
use ArtisanBuild\Docsidian\SiteStatus;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class DiscoverCommand extends Command
{
public $signature = 'docsidian:discover {directory?}';
public $description = 'Create a record for every folder in the markdown folder';

public function handle(): int
{
$directories = File::directories($this->argument('directory') ?? config('docsidian.md_path'));

foreach ($directories as $directory) {
if (DocsidianSite::where('md_path', $directory)->exists()) {
continue;
}
$key = last(explode(DIRECTORY_SEPARATOR, $directory));
$site = DocsidianSite::create([
'name' => $this->generateName($key),
'description' => null,
'image' => null,
'weight' => (DocsidianSite::max('weight') ?? 0) + 100,
'status' => SiteStatus::Hidden,
'default_visibility' => config('docsidian.default_visibility'),
'folio_middleware' => config('docsidian.folio_middleware'),
'folio_path' => implode(DIRECTORY_SEPARATOR, [config('docsidian.folio_path'), $key]),
'folio_uri' => implode('/', [config('docsidian.folio_uri'), $key]),
'layout' => config('docsidian.layout'),
'md_path' => $directory,
'obsidian_config' => config('docsidian.obsidian_config'),

]);

File::ensureDirectoryExists($site->folio_path);

$this->info("Created record for {$key}");
}
return self::SUCCESS;
}

public function generateName(string $key): string
{
if (str_contains('.', $key)) {
return $key;
}

return Str::headline($key);
}
}
7 changes: 5 additions & 2 deletions src/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ArtisanBuild\Docsidian\Contracts\IndexesSiteForSearch;
use ArtisanBuild\Docsidian\DocumentationSite;
use ArtisanBuild\Docsidian\EmbeddedMedia;
use ArtisanBuild\Docsidian\Models\DocsidianSite;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Pipeline;
Expand All @@ -31,8 +32,10 @@ class GenerateCommand extends Command

public function handle(): int
{
foreach (config('docsidian.sites') as $key => $site) {
$this->info("Generating {$key}");
foreach (DocsidianSite::all() as $site) {
$this->info("Generating {$site->name}");

$site = $site->toArray();

File::deleteDirectory($site['folio_path']);
File::ensureDirectoryExists($site['folio_path']);
Expand Down
8 changes: 7 additions & 1 deletion src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ public function handle(): int

$configContents = File::get(__DIR__.'/../../config/docsidian.php.stub');

foreach (['md_path', 'folio_uri', 'folio_path'] as $key) {
foreach (['default_visibility', 'md_path', 'folio_uri', 'folio_path'] as $key) {
$configContents = str_replace("{{ $key }}", $config[$key], $configContents);
}

File::put(config_path('docsidian.php'), $configContents);

$this->callSilently("vendor:publish", [
'--tag' => "docsidian-migrations",
]);

$this->call('migrate');

return self::SUCCESS;
}
}
30 changes: 23 additions & 7 deletions src/DocsidianServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
use ArtisanBuild\Docsidian\Actions\DoNotIndexForSearch;
use ArtisanBuild\Docsidian\Actions\GetDefinedAbilities;
use ArtisanBuild\Docsidian\Actions\HighlightCodeWithTempest;
use ArtisanBuild\Docsidian\Commands\DiscoverCommand;
use ArtisanBuild\Docsidian\Commands\GenerateCommand;
use ArtisanBuild\Docsidian\Commands\InstallCommand;
use ArtisanBuild\Docsidian\Contracts\HighlightsCodeBlocks;
use ArtisanBuild\Docsidian\Contracts\IndexesSiteForSearch;
use ArtisanBuild\Docsidian\Models\DocsidianSite;
use Filament\Facades\Filament;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Schema;
use Laravel\Folio\Folio;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand All @@ -30,14 +35,22 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasViews()
->hasMigration('create_docsidian_table')
->hasCommand(InstallCommand::class)
->hasCommand(GenerateCommand::class);
->hasCommands([
InstallCommand::class,
GenerateCommand::class,
DiscoverCommand::class,
]);
}

public function registeringPackage()
{
$this->app->bind(IndexesSiteForSearch::class, DoNotIndexForSearch::class);
$this->app->bind(HighlightsCodeBlocks::class, HighlightCodeWithTempest::class);

if (class_exists(Filament::class)) {
$this->app->register(\ArtisanBuild\Docsidian\DocumentationPanelProvider::class);
}

}

public function packageBooted(): void
Expand All @@ -46,19 +59,22 @@ public function packageBooted(): void
return;
}

if (! Schema::hasTable('docsidian_sites')) {
return;
}

Blade::component(DocsidianLayoutComponent::class, 'docsidian');

foreach (config('docsidian.sites') as $site) {
Folio::path($site['folio_path'])
->uri($site['folio_uri'])
->middleware($site['folio_middleware']);
foreach (DocsidianSite::all() as $site) {
Folio::path($site->folio_path)
->uri($site->folio_uri)
->middleware($site->folio_middleware);
}

if (app(GetDefinedAbilities::class)()->isEmpty()) {
Gate::define('docsidian-public', fn (?Authenticatable $user) => true);
Gate::define('docsidian-protected', fn (?Authenticatable $user) => $user instanceof Authenticatable);
Gate::define('docsidian-private', fn (?Authenticatable $user) => false);
}

}
}
60 changes: 60 additions & 0 deletions src/DocumentationPanelProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace ArtisanBuild\Docsidian;

use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class DocumentationPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('docsidian')
->path('docsidian')
->login()
->colors([
'primary' => Color::Amber,
])
->discoverResources(in: __DIR__.'/Filament', for: 'ArtisanBuild\\Docsidian\\Filament')
->discoverPages(in: __DIR__.'Filament/Pages', for: 'ArtisanBuild\\Docsidian\\Filament\\Pages')
->pages([
Pages\Dashboard::class,
Pages\Dashboard::class,
])
->discoverWidgets(in: __DIR__.'Filament/Widgets', for: 'ArtisanBuild\\Docsidian\\Filament\\Widgets')
->widgets([
Widgets\AccountWidget::class,
Widgets\FilamentInfoWidget::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
]);

}
}
Loading

0 comments on commit b8e2bc5

Please sign in to comment.