Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Created September 2, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhrozenByte/4977cbb2aae750487d3532cf3715c40a to your computer and use it in GitHub Desktop.
Save PhrozenByte/4977cbb2aae750487d3532cf3715c40a to your computer and use it in GitHub Desktop.
A simple Pico plugin to wrap Markdown images in <figure> elements. Pico is a stupidly simple, blazing fast, flat file CMS. http://picocms.org/
<?php
/**
* Parsedown figure extension
*
* Parsedown extension to wrap single-image lines in <figure>, not <p>.
*
* @author Krzysztof Antoniak
* @link https://gist.github.com/kantoniak/b1a5c7889e5583824487dc78d93da7cd
* @license Public Domain
* @version fdb7c2c3ccf561e2e84b31096d6ba1257b4e9058 (2019-08-31T13:49:00+0200)
*/
class FigureExtParsedown extends Parsedown {
// Matches Markdown image definition
private $MarkdownImageRegex = "~^!\[.*?\]\(.*?\)$~";
public function __construct () {
// Add blockFigure to non-exclusive handlers for text starting with !
$this->BlockTypes['!'][] = 'Figure';
}
protected function blockFigure($Line) {
// If line does not match image def, don't handle it
if (1 !== preg_match($this->MarkdownImageRegex, $Line['text'])) {
return;
}
$InlineImage = $this->inlineImage($Line);
if (!isset($InlineImage)) {
return;
}
$FigureBlock = array(
'element' => array(
'name' => 'figure',
'handler' => 'elements',
'text' => array(
$InlineImage['element']
)
),
);
// Add figcaption if title set
if (!empty($InlineImage['element']['attributes']['title'])) {
$InlineFigcaption = array(
'element' => array(
'name' => 'figcaption',
'text' => $InlineImage['element']['attributes']['title']
),
);
$FigureBlock['element']['text'][] = $InlineFigcaption['element'];
}
return $FigureBlock;
}
}
<?php
/**
* Parsedown figure extension for Pico
*
* Uses Krzysztof Antoniak's FigureExtParsedown Parsedown plugin in Pico
* to wrap Markdown images in <figure> elements.
*
* @author Daniel Rudolf
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
* @version 0.0.1
*/
class FigureExtParsedownPicoPlugin extends AbstractPicoPlugin
{
const API_VERSION = 2;
public function __construct(Pico $pico)
{
parent::__construct($pico);
require_once(__DIR__ . '/FigureExtParsedown.php');
}
public function onParsedownRegistered(Parsedown &$parsedown)
{
$config = $this->getPico()->getConfig();
$parsedown = new FigureExtParsedown();
$parsedown->setBreaksEnabled((bool) $config['content_config']['breaks']);
$parsedown->setMarkupEscaped((bool) $config['content_config']['escape']);
$parsedown->setUrlsLinked((bool) $config['content_config']['auto_urls']);
}
}
@anasram
Copy link

anasram commented Feb 25, 2020

Hi Daniel,

I just tested it with Pico 2.1.1. It corrupts definition lists.

I think it conflicts with parsedown-extra.

Copy link

ghost commented Nov 11, 2020

Hi,
These break footnotes. Any ideas why?

@PhrozenByte
Copy link
Author

This Parsedown extension extends Parsedown, not ParsedownExtra. Try replacing line 13 in FigureExtParsedown.php by the following:

class FigureExtParsedown extends ParsedownExtra {

Copy link

ghost commented Nov 14, 2020

This Parsedown extension extends Parsedown, not ParsedownExtra. Try replacing line 13 in FigureExtParsedown.php by the following:

class FigureExtParsedown extends ParsedownExtra {

Hey, thanks for the quick reply. Unfortunately it did not fix it. I did inject the FigureExtParsedown code directly into the ParsedownExtra php and it's working. I think parsedown extra changed it's __construct() function, but I don't know enough about php to really know what's going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment