-
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.
6 allow prefixing image sources (#7)
It's possible to prefix image sources. Thanks @juripetersen
- Loading branch information
1 parent
45dc00f
commit 242481f
Showing
5 changed files
with
154 additions
and
14 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
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,94 @@ | ||
<?php | ||
|
||
namespace VV\Markdown\Markdown; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
|
||
class PrefixImageSources | ||
{ | ||
public string $content; | ||
|
||
public function __construct(string $content) | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
/** | ||
* Find all image sources in the html content and replace all absolute paths with a configurable url that is prefixed to it. | ||
*/ | ||
public function handle() | ||
{ | ||
if ($this->getPrefixUrl()) { | ||
$this->findImageSources($this->findImages($this->content)); | ||
} | ||
|
||
return $this->content; | ||
} | ||
|
||
/** | ||
* Iterate over the html and find all images. | ||
*/ | ||
private function findImages(string $htmlContent): array | ||
{ | ||
preg_match_all('/<img[^>]+>/i', $htmlContent, $result); | ||
|
||
return Arr::flatten($result); | ||
} | ||
|
||
/** | ||
* Iterate over all sources and prefix them with the configurable url. | ||
*/ | ||
private function findImageSources(array $htmlImages): void | ||
{ | ||
foreach ($htmlImages as $image) { | ||
preg_match('/src=(?:"[^"]*")/i', $image, $source); | ||
|
||
$source = $source[0] ?? null; | ||
|
||
if ($source && !$this->isAbsolutePath($source)) { | ||
$this->content = str_replace($source, $this->insertPrefix($source), $this->content); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add prefix to image source. | ||
*/ | ||
private function insertPrefix(string $source): string | ||
{ | ||
$source = str_replace('src="', '', $source); | ||
|
||
if (Str::startsWith($source, '/')) { | ||
$source = Str::substr($source, 1); | ||
} | ||
|
||
$prefix = $this->getPrefixUrl(); | ||
|
||
return 'src="'.$prefix.$source; | ||
} | ||
|
||
/** | ||
* Load Prefix URL from config. | ||
*/ | ||
private function getPrefixUrl(): ?string | ||
{ | ||
$prefix = config('markdown.images.prefix', null); | ||
|
||
if ($prefix) { | ||
$prefix = Str::finish($prefix, '/'); | ||
} | ||
|
||
return $prefix; | ||
} | ||
|
||
/** | ||
* Check if source is a absolute path or a complete url. | ||
*/ | ||
private function isAbsolutePath(string $source): bool | ||
{ | ||
return Str::contains($source, 'https://') | ||
|| Str::contains($source, 'http://') | ||
|| Str::contains($source, 'ftp://'); | ||
} | ||
} |
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