-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1590 from hydephp/dynamic-markdown-links
[2.x] Dynamic Markdown links
- Loading branch information
Showing
7 changed files
with
682 additions
and
0 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
77 changes: 77 additions & 0 deletions
77
packages/framework/src/Markdown/Processing/DynamicMarkdownLinkProcessor.php
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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Markdown\Processing; | ||
|
||
use Hyde\Hyde; | ||
use Illuminate\Support\Str; | ||
use Hyde\Support\Filesystem\MediaFile; | ||
use Hyde\Markdown\Contracts\MarkdownPostProcessorContract; | ||
|
||
class DynamicMarkdownLinkProcessor implements MarkdownPostProcessorContract | ||
{ | ||
/** @var array<string, \Hyde\Support\Filesystem\MediaFile>|null */ | ||
protected static ?array $assetMapCache = null; | ||
|
||
public static function postprocess(string $html): string | ||
{ | ||
foreach (static::routeMap() as $sourcePath => $route) { | ||
$patterns = [ | ||
sprintf('<a href="%s"', $sourcePath), | ||
sprintf('<a href="/%s"', $sourcePath), | ||
]; | ||
|
||
$html = str_replace($patterns, sprintf('<a href="%s"', $route->getLink()), $html); | ||
} | ||
|
||
foreach (static::assetMap() as $sourcePath => $mediaFile) { | ||
$patterns = [ | ||
sprintf('<img src="%s"', $sourcePath), | ||
sprintf('<img src="/%s"', $sourcePath), | ||
]; | ||
|
||
$html = str_replace($patterns, sprintf('<img src="%s"', static::assetPath($mediaFile)), $html); | ||
} | ||
|
||
return $html; | ||
} | ||
|
||
/** @return array<string, \Hyde\Support\Models\Route> */ | ||
protected static function routeMap(): array | ||
{ | ||
$map = []; | ||
|
||
/** @var \Hyde\Support\Models\Route $route */ | ||
foreach (Hyde::routes() as $route) { | ||
$map[$route->getSourcePath()] = $route; | ||
} | ||
|
||
return $map; | ||
} | ||
|
||
/** @return array<string, \Hyde\Support\Filesystem\MediaFile> */ | ||
protected static function assetMap(): array | ||
{ | ||
if (static::$assetMapCache === null) { | ||
static::$assetMapCache = []; | ||
|
||
foreach (MediaFile::all() as $mediaFile) { | ||
static::$assetMapCache[$mediaFile->getPath()] = $mediaFile; | ||
} | ||
} | ||
|
||
return static::$assetMapCache; | ||
} | ||
|
||
protected static function assetPath(MediaFile $mediaFile): string | ||
{ | ||
return Hyde::asset(Str::after($mediaFile->getPath(), '_media/')); | ||
} | ||
|
||
/** @internal Testing helper to reset the asset map cache. */ | ||
public static function resetAssetMapCache(): void | ||
{ | ||
static::$assetMapCache = null; | ||
} | ||
} |
Oops, something went wrong.