Skip to content

Commit

Permalink
Change docs formating for views
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Nov 10, 2023
1 parent 64a706c commit c8e2396
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 31 deletions.
10 changes: 6 additions & 4 deletions app/Docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Docs
*/
public const SUPPORT_VERSION = [
'8.x',
'5.4',
'4.2'
];

/**
Expand Down Expand Up @@ -93,13 +95,12 @@ public function view(string $view)
{
$content = Str::of($this->page)
->replace('{{version}}', $this->version)
->replace('{note}','⚠️')
->replace('{tip}','💡️')
->after('---')
->after('---')
->markdown();

$all = collect()->merge($this->variables)->merge([
'docs' => $this,
'content' => $content,
'edit' => $this->goToGitHub(),
]);
Expand All @@ -120,7 +121,8 @@ public function getMenu(): array
->after('---')
->after('---')
->replace('{{version}}', $this->version)
->markdown()->toString();
->markdown()
->toString();

return $this->docsToArray($html);
}
Expand Down Expand Up @@ -197,7 +199,7 @@ static public function every(string $version): \Illuminate\Support\Collection
*/
public function fetchBehind(): int
{
throw_unless(isset($this->variables['git']), new Exception("Document {$this->path} does not have a git hash"));
throw_unless(isset($this->variables['git']), new Exception("The document {$this->path} is missing a Git hash"));

$response = Http::withBasicAuth('token', config('services.github.token'))
->get("https://api.github.com/repos/laravel/docs/commits?sha={$this->version}&path={$this->file}");
Expand Down
10 changes: 2 additions & 8 deletions app/Http/Controllers/DocsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Docs;
use App\Models\Document;
use Illuminate\Http\Request;

class DocsController extends Controller
{
Expand All @@ -14,19 +13,14 @@ class DocsController extends Controller
* @param string $version
* @param string $page
*
* @return \Illuminate\View\View
* @return \Illuminate\View\View|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function show(string $version = Docs::DEFAULT_VERSION, string $page = 'installation')
{
$docs = new Docs($version, $page);

$text = $docs->view('particles.docs');

return view('pages.docs', [
'docs' => $docs,
'text' => $text,
]);
return $docs->view('pages.docs');
}

/**
Expand Down
83 changes: 83 additions & 0 deletions app/View/Components/Docs/Anchors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\View\Components\Docs;

use Illuminate\View\Component;
use Illuminate\Support\Str;
use Symfony\Component\DomCrawler\Crawler;

class Anchors extends Component
{
/**
* @var string
*/
protected $content;

/**
* Create a new component instance.
*
* @return void
*/
public function __construct(string $content)
{
$this->content = $content;
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
* @throws \DOMException
*/
public function render()
{
return view('components.docs.anchors', [
'anchors' => $this->findAnchors(),
]);
}


/**
* @param $contents
*
* @return array
* @throws \DOMException
*/
private function findAnchors()
{
$crawler = new Crawler();
$crawler->addContent(mb_convert_encoding($this->content, "UTF-8"));

$anchors = [];

$crawler
->filter('h2,h3')
->each(function ($elm) use (&$anchors) {

/** @var Crawler $elm */
/** @var \DOMElement $node */
$node = $elm->getNode(0);
$text = $node->textContent;
$id = Str::slug($text);


$anchors[] = [
'text' => $text,
'level' => $node->tagName,
'id' => $id,
];

while ($node->hasChildNodes()) {
$node->removeChild($node->firstChild);
}


$node->appendChild(new \DOMElement('a', e($text)));
$node->firstChild->setAttribute('href', '#' . $id);
$node->firstChild->setAttribute('name', $id);
});

return $anchors;
}

}
146 changes: 146 additions & 0 deletions app/View/Components/Docs/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace App\View\Components\Docs;

use Illuminate\View\Component;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;
use JoliTypo\Fixer;
use Symfony\Component\DomCrawler\Crawler;

class Content extends Component implements Htmlable
{
/**
* @var string
*/
protected $content;

/**
* Create a new component instance.
*
* @return void
*/
public function __construct(string $content)
{
$this->content = $content;
}

/**
* Get the view / contents that represent the component.
*
* @return \App\View\Components\DocsContent
* @throws \DOMException
*/
public function render()
{
return $this;
}

/**
* @param $contents
*
* @return string
* @throws \DOMException
*/
private function modifyContent()
{
$crawler = new Crawler();
$crawler->addHtmlContent( mb_convert_encoding($this->content, "UTF-8"));
$this->content = $crawler->filterXpath('//body')->first()->html();


$crawler->filter('blockquote')->each(function (Crawler $elm) {
$tag = $elm->outerHtml();

$html = $tag;

if(Str::of($tag)->contains('{note}')){
$html = Str::of($tag)
->replace('<blockquote>', '<blockquote class="docs-blockquote-note">')
->replace('{note}', '');
}

if(Str::of($tag)->contains('{tip}')){
$html = Str::of($tag)
->replace('<blockquote>', '<blockquote class="docs-blockquote-tip">')
->replace('{tip}', '');
}

$this->content = Str::of($this->content)->replace($tag, $html);
});

$crawler->filter('x-docs-banner')->each(function (Crawler $elm) {
$tag = $elm->outerHtml();

$this->content = Str::of($this->content)
->replace($tag, Blade::render($tag));
});


$crawler->filter('h1')->each(function (Crawler $elm) {
$tag = $elm->outerHtml();

$this->content = Str::of($this->content)->replace($tag, '');
});

$crawler
->filter('h2,h3,h4,h5,h6')
->each(function (Crawler $elm) use (&$anchors) {

/** @var \DOMElement $node */
$node = $elm->getNode(0);

$content = $node->textContent;
$id = Str::slug($content);
$tag = $node->nodeName;

$this->content = Str::of($this->content)
->replace($elm->outerHtml(), "<$tag><a href='#$id' id='$id'>$content</a></$tag>");
});

$crawler
->filter('img')
->each(function (Crawler $elm) use (&$anchors) {

$imgTag = $elm->outerHtml();
$alt = $elm->attr('alt');

$this->content = Str::of($this->content)
->replace($imgTag, "<picture alt='$alt'>$imgTag</picture>");
});

$fixer = new Fixer([
'Ellipsis',
'Dimension',
'Unit',
'Dash',
'SmartQuotes',
'NoSpaceBeforeComma',
'CurlyQuote',
'Trademark',
]);

$crawler
->filter('p,liб,blockquote')
->each(function (Crawler $elm) use ($fixer) {

$content = $elm->html();

$paragraph = $fixer->fix($content);

$this->content = Str::of($this->content)->replace($content, $paragraph);
});

return $this->content;
}

/**
* @return string
* @throws \DOMException
*/
public function toHtml(): string
{
return $this->modifyContent();
}
}
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": ">=8.2",
"php": ">=8.1",
"cagilo/cagilo": "^3.2",
"doctrine/dbal": "^3.7",
"guzzlehttp/guzzle": "^7.2",
"jolicode/jolitypo": "^1.4",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.2",
"laravel/socialite": "^5.9",
Expand All @@ -22,6 +23,7 @@
"fakerphp/faker": "^1.9.1",
"laravel/pint": "^1.0",
"laravel/sail": "^1.18",
"laravel/telescope": "^4.17",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^7.0",
"phpunit/phpunit": "^10.1",
Expand Down Expand Up @@ -56,7 +58,9 @@
},
"extra": {
"laravel": {
"dont-discover": []
"dont-discover": [
"laravel/telescope"
]
}
},
"config": {
Expand Down
Loading

0 comments on commit c8e2396

Please sign in to comment.