-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
463 additions
and
31 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
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,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; | ||
} | ||
|
||
} |
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,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(); | ||
} | ||
} |
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
Oops, something went wrong.