generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The stuff I needed in there for the EXACT documentation portal (#4)
- Loading branch information
Showing
19 changed files
with
377 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,7 @@ phpunit.xml | |
phpstan.neon | ||
testbench.yaml | ||
vendor | ||
node_modules | ||
node_module | ||
tests/md/.DS_Store | ||
tests/.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace ArtisanBuild\Docsidian\Actions; | ||
|
||
use ArtisanBuild\Docsidian\DocumentationSite; | ||
use Closure; | ||
|
||
class EnableMermaid | ||
{ | ||
public bool $mermaid = false; | ||
|
||
public function __invoke(DocumentationSite $site, Closure $next) | ||
{ | ||
$site->blade_files->each( | ||
fn ($file) => $file->lines = $file->lines->map(fn ($line) => $this->enableMermaid($line))); | ||
|
||
return $next($site); | ||
} | ||
|
||
protected function enableMermaid($line) | ||
{ | ||
if (str_contains($line->content, '</code')) { | ||
if ($this->mermaid) { | ||
$line->content = str_replace('</code>', '', $line->content); | ||
$this->mermaid = false; | ||
|
||
return $line; | ||
} | ||
} | ||
if (str_contains($line->content, 'class="language-mermaid"')) { | ||
$this->mermaid = true; | ||
} | ||
|
||
if ($this->mermaid) { | ||
$line->content = htmlspecialchars_decode($line->content); | ||
$line->content = str_replace('<pre><code class="language-mermaid">', '<pre class="mermaid">', $line->content); | ||
} | ||
|
||
return $line; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace ArtisanBuild\Docsidian\Actions; | ||
|
||
use ArtisanBuild\Docsidian\DocumentationLine; | ||
use ArtisanBuild\Docsidian\DocumentationSite; | ||
use ArtisanBuild\Docsidian\EmbeddedMedia; | ||
use Closure; | ||
|
||
class HandleWikiStyleImages | ||
{ | ||
public string $image_path = ''; | ||
|
||
public function __invoke(DocumentationSite $site, Closure $next) | ||
{ | ||
$this->image_path = $site->obsidian_configuration->attachment_path; | ||
|
||
$site->blade_files->each( | ||
fn ($file) => $file->lines = $file->lines->map(fn ($line) => $this->transformImages($line))); | ||
|
||
return $next($site); | ||
} | ||
|
||
public function transformImages(DocumentationLine $line) | ||
{ | ||
$line->content = $this->convertWikiImagesToHtml($line->content); | ||
|
||
return $line; | ||
|
||
} | ||
|
||
public function convertWikiImagesToHtml($markdown): string | ||
{ | ||
$pattern = '/!\[\[([^\]]+)\]\]/'; | ||
preg_match($pattern, $markdown, $matches); | ||
|
||
if (isset($matches[1])) { | ||
$image = app(EmbeddedMedia::class)->make($matches[1]); | ||
$markdown = str_replace($matches[0], '<img src="{{ asset(\''.$image->uri.'\') }}" alt="">', $markdown); | ||
} | ||
|
||
return $markdown; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace ArtisanBuild\Docsidian; | ||
|
||
use Illuminate\Support\Facades\File; | ||
|
||
class EmbeddedMedia | ||
{ | ||
public string $uri = ''; | ||
|
||
public string $path = ''; | ||
|
||
public string $source_path = ''; | ||
|
||
public bool $exists = false; | ||
|
||
public ObsidianConfiguration $obsidian; | ||
|
||
public function __construct(public array $config) | ||
{ | ||
$this->obsidian = new ObsidianConfiguration($config); | ||
} | ||
|
||
public function make(string $path) | ||
{ | ||
$this->path = implode('/', [ | ||
$this->config['media_path'], | ||
$path, | ||
]); | ||
|
||
$this->source_path = implode('/', [ | ||
$this->obsidian->attachment_path, | ||
$path, | ||
]); | ||
|
||
$this->uri = implode('/', [ | ||
str_replace(public_path(), '', rtrim($this->config['media_path'], '/')), | ||
$path, | ||
]); | ||
|
||
throw_if(! file_exists($this->source_path), "{$this->source_path} does not exist"); | ||
|
||
$this->exists = file_exists($this->path); | ||
|
||
File::ensureDirectoryExists(dirname($this->path)); | ||
|
||
File::copy($this->source_path, $this->path); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace ArtisanBuild\Docsidian\LineTransformers; | ||
|
||
use Closure; | ||
|
||
class EmbedRawImageUrls | ||
{ | ||
public function __invoke(string $line, Closure $next): string | ||
{ | ||
if (! filter_var(trim(current($chunks = explode(' ', trim(strip_tags($line))))), FILTER_VALIDATE_URL)) { | ||
return $next($line); | ||
} | ||
|
||
$headers = get_headers(current($chunks), true); | ||
|
||
if (! str_contains(data_get($headers, 'Content-Type'), 'image')) { | ||
|
||
return $next($line); | ||
} | ||
|
||
$line = count(array_filter($chunks)) === 1 | ||
? '<p><img class="object-contain md:object-scale-down" src="'.current($chunks).'" alt=""></p>' | ||
: '<p><figure><img class="object-contain md:object-scale-down" src="' | ||
.array_shift($chunks) | ||
.'" alt=""><figcaption>' | ||
.implode(' ', $chunks) | ||
.'</figcaption></figure></p>'; | ||
|
||
return $next($line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace ArtisanBuild\Docsidian\LineTransformers; | ||
|
||
use Closure; | ||
|
||
class TransformWikiImages | ||
{ | ||
public function __invoke(string $line, Closure $next): string | ||
{ | ||
return $next($line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace ArtisanBuild\Docsidian; | ||
|
||
class ObsidianConfiguration | ||
{ | ||
public string $css; | ||
|
||
public string $attachment_path; | ||
|
||
/* TODO: It would be nice to let users use their Obsidian theme's CSS to style their documentation site. To | ||
that end, I am populating $this->css with the full path to the CSS that is bundled with the theme. I am not | ||
sure how much work it will take to create a template that has the correct classes in the correct places | ||
and whether we might also have to grab or generate a base theme css file to make this work. It's very much | ||
a seed of an idea at this point, but it would be nice to make it happen at some point if we can figure it out. | ||
*/ | ||
public function __construct(array $configuration) | ||
{ | ||
// Calculate the full path to the Obsidian configuration folder | ||
$configuration_path = data_get($configuration, 'obsidian_config'); | ||
|
||
$config = [ | ||
'app' => json_decode(file_get_contents(implode('/', [$configuration_path, 'app.json'])), true), | ||
'appearance' => json_decode(file_get_contents(implode('/', [$configuration_path, 'appearance.json'])), true), | ||
]; | ||
|
||
$this->attachment_path = implode('/', array_filter([ | ||
data_get($configuration, 'md_path'), | ||
str_replace('./', '', data_get($config, 'app.attachmentFolderPath')), | ||
])); | ||
|
||
$this->css = implode('/', [ | ||
$configuration_path, | ||
'themes', | ||
data_get($config, 'appearance.cssTheme'), | ||
'theme.css', | ||
]); | ||
} | ||
} |
Oops, something went wrong.