-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for custom inline images with markdown #10, no more twi…
…g needed!
- Loading branch information
Showing
8 changed files
with
134 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace Grav\Common\Markdown; | ||
|
||
class Markdown extends \Parsedown | ||
{ | ||
use MarkdownGravLinkTrait; | ||
|
||
function __construct($page) | ||
{ | ||
$this->page = $page; | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
namespace Grav\Common\Markdown; | ||
|
||
class MarkdownExtra extends \ParsedownExtra | ||
{ | ||
use MarkdownGravLinkTrait; | ||
|
||
function __construct($page) | ||
{ | ||
parent::__construct(); | ||
$this->page = $page; | ||
} | ||
} |
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 Grav\Common\Markdown; | ||
|
||
use Grav\Common\Debugger; | ||
|
||
/** | ||
* A trait to add some custom processing to the identifyLink() method in Parsedown and ParsedownExtra | ||
*/ | ||
trait MarkdownGravLinkTrait | ||
{ | ||
|
||
protected function identifyLink($Excerpt) | ||
{ | ||
// Run the parent method to get the actual results | ||
$Excerpt = parent::identifyLink($Excerpt); | ||
$actions = array(); | ||
$command = ''; | ||
|
||
// if this is an image | ||
if (isset($Excerpt['element']['attributes']['src'])) { | ||
|
||
$alt = isset($Excerpt['element']['attributes']['alt']) ? $Excerpt['element']['attributes']['alt'] : ''; | ||
$title = isset($Excerpt['element']['attributes']['title']) ? $Excerpt['element']['attributes']['title'] : ''; | ||
|
||
//get the url and parse it | ||
$url = parse_url(htmlspecialchars_decode($Excerpt['element']['attributes']['src'])); | ||
|
||
// if there is a query, then parse it and build action calls | ||
if (isset($url['query'])) { | ||
parse_str($url['query'], $actions); | ||
|
||
foreach ($actions as $action => $params) { | ||
// ignore any url or html actions | ||
if (!in_array($action, ['html','url'])) | ||
$command .= '->' . $action . '(' . $params . ')'; | ||
} | ||
} | ||
|
||
// if there is no host set but there is a path, the file is local | ||
if (!isset($url['host']) && isset($url['path'])) { | ||
// get the media objects for this page | ||
$media = $this->page->media(); | ||
|
||
// if there is a media file that matches the path referenced.. | ||
if (isset($media->images()[$url['path']])) { | ||
// get the medium object | ||
$medium = $media->images()[$url['path']]; | ||
|
||
// unless one of the actions is lightbox method get the url | ||
if (!isset($actions['lightbox'])) { | ||
$command .= '->url()'; | ||
} else { | ||
$command .= '->lightboxRaw()'; | ||
} | ||
|
||
// evaluate the commands to run against the media object | ||
eval ('$src = $medium'.$command.';'); | ||
|
||
// set the src element with the new generated url | ||
if (!isset($actions['lightbox']) && !is_array($src)) { | ||
$Excerpt['element']['attributes']['src'] = $src; | ||
} else { | ||
|
||
// Create the custom lightbox element | ||
$Element = array( | ||
'name' => 'a', | ||
'attributes' => array('rel' => $src['a_rel'], 'href' => $src['a_url']), | ||
'handler' => 'element', | ||
'text' => array( | ||
'name' => 'img', | ||
'attributes' => array('src' => $src['img_url'], 'alt' => $alt, 'title' => $title) | ||
), | ||
); | ||
|
||
// Set the lightbox element on the Excerpt | ||
$Excerpt['element'] = $Element; | ||
} | ||
} | ||
} | ||
} | ||
return $Excerpt; | ||
} | ||
} |
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
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