Skip to content

Commit

Permalink
Revert "Refactor to extract methods for common logic"
Browse files Browse the repository at this point in the history
This reverts commit 851b17a. Cleaner is not always better.
  • Loading branch information
caendesilva committed Jul 25, 2024
1 parent 851b17a commit ede0cdf
Showing 1 changed file with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Hyde\Hyde;
use Illuminate\Support\Str;
use Hyde\Support\Models\Route;
use Hyde\Support\Filesystem\MediaFile;
use Hyde\Markdown\Contracts\MarkdownPostProcessorContract;

Expand All @@ -17,35 +16,27 @@ class DynamicMarkdownLinkProcessor implements MarkdownPostProcessorContract

public static function postprocess(string $html): string
{
$html = static::processMap(static::routeMap(), $html, 'a', 'href');
$html = static::processMap(static::assetMap(), $html, 'img', 'src');
foreach (static::routeMap() as $sourcePath => $route) {
$patterns = [
sprintf('<a href="%s"', $sourcePath),
sprintf('<a href="/%s"', $sourcePath),
];

return $html;
}
$html = str_replace($patterns, sprintf('<a href="%s"', $route->getLink()), $html);
}

/**
* @param array<string, \Hyde\Support\Models\Route|\Hyde\Support\Filesystem\MediaFile> $map
*/
protected static function processMap(array $map, string $html, string $tag, string $attribute): string
{
foreach ($map as $sourcePath => $item) {
foreach (static::assetMap() as $sourcePath => $mediaFile) {
$patterns = [
sprintf('<%s %s="%s"', $tag, $attribute, $sourcePath),
sprintf('<%s %s="/%s"', $tag, $attribute, $sourcePath),
sprintf('<img src="%s"', $sourcePath),
sprintf('<img src="/%s"', $sourcePath),
];

$replacement = sprintf('<%s %s="%s"', $tag, $attribute, static::getItemPath($item));
$html = str_replace($patterns, $replacement, $html);
$html = str_replace($patterns, sprintf('<img src="%s"', static::assetPath($mediaFile)), $html);
}

return $html;
}

protected static function getItemPath(MediaFile|Route $item): string
{
return $item instanceof MediaFile ? static::assetPath($item) : $item->getLink();
}

/** @return array<string, \Hyde\Support\Models\Route> */
protected static function routeMap(): array
{
Expand Down

0 comments on commit ede0cdf

Please sign in to comment.