Closed jenstornell closed 5 years ago
You're trying to remove link parsing, but keep images if I'm reading that right?
@aidantwoods
I have one method inlineImage
where I manipulate an image from my-image.jpg
to https://example.com/cache/assets/images/my-image-800.jpg
(prefix with a cache path and suffix with an image width).
For the anchor tag I intend to do something similar but simpler, just prefix my-link
to https://example.com/my-link
.
So it's a difference between the two cases. I hope that answered your question.
I even tried to add $image['element']['type'] = 'image';
in my inlineImage
, but that does not work because inlineLink
is always loaded first (by inlineImage) so it will not be aware of it anyway.
There isn't such a good way to solve this in the current version since Parsedown's image will defer to $this->inlineLink
, which is of course your overloaded version.
In 2.0 you'll be able to swap out components without this being an issue, since if any deferring exists it defers statically and won't be affected by other loaded components.
If you're wanting to do this I think you'll have to completely overload and replicate the functionality in the inlineImage
method, but replacing its call to $this->inlineLink
with parent::inlineLink
to defer Parsedown's version, then add your own functionality after. You should then be free to separately overload inlineLink
rendering.
@aidantwoods Yes, I just figured that out as a last resort, to completely override the function. For my need it should work fine.
I'll close it, as I got a decent workaround and because you will treat it differently in version 2.0.
Thanks for the help!
@aidantwoods
For completeness, here is the full code that I use now that is working.
class ParsedownExtension extends Parsedown {
protected function inlineImage($Excerpt)
{
if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
{
return;
}
$Excerpt['text']= substr($Excerpt['text'], 1);
// Custom
$Link = parent::inlineLink($Excerpt);
if ($Link === null)
{
return;
}
$Inline = array(
'extent' => $Link['extent'] + 1,
'element' => array(
'name' => 'img',
'attributes' => array(
'src' => $Link['element']['attributes']['href'],
'alt' => $Link['element']['handler']['argument'],
),
'autobreak' => true,
),
);
$Inline['element']['attributes'] += $Link['element']['attributes'];
unset($Inline['element']['attributes']['href']);
// Custom
$pathinfo = pathinfo($Inline['element']['attributes']['src']);
$Inline['element']['attributes']['src'] = $this->image_root . '/' . $pathinfo['filename'] . '-' . $this->image_suffix . '.' . $pathinfo['extension'];
return $Inline;
}
protected function inlineLink($excerpt) {
$link = parent::inlineLink($excerpt);
if(!isset($link)) return null;
if(substr($link['element']['attributes']['href'], 0, 4) != 'http') {
$link['element']['attributes']['href'] = $this->url_root . '/' . $link['element']['attributes']['href'];
}
return $link;
}
}
$Parsedown = new ParsedownExtension();
$Parsedown->url_root = 'http://example.com';
$Parsedown->image_root = 'http://example.com/assets/images/some-dir';
$Parsedown->image_suffix = 800;
echo $Parsedown->setBreaksEnabled(true)->text($text);
Today I tried to extend Parsedown. The image example worked just as expected but to prefix an anchor tag (which was my goal) did not go as well.
Content (shorten down):
The problem is that
inlineLink
will find both my image and my anchor tag. I can't by the result know if it's an image or an anchor tag.