Mark Messa
Mark Messa

Reputation: 470

Parsedown: sub/superscript

The current version of Parsedown 1.8.0-beta-5 doesn't have a builtin syntax for sub/superscript. Although CommonMark doesn't specify such syntax, several other lightweight markup languages (ex: Parsedown Extreme, Textile) use a syntax similar to the following:

in: 19^th^  
out: 19<sup>th</sup>

in: H~2~O  
out: H<sub>2</sub>O

Question
What steps should be taken in order to modify Parsedown.php file and include such syntax?

 


Note: This issue has already come up other times (Parsedown, add sub/superscript). However, there is still no step-by-step guide explaining what modifications should be done in Parsedown.php file in order to achieve that.

Upvotes: 1

Views: 216

Answers (1)

Mark Messa
Mark Messa

Reputation: 470

  1. Append Superscript and Tilde in $InlineTypes:

    protected $InlineTypes = array(
        '!' => array('Image'),
        '&' => array('SpecialCharacter'),
        '*' => array('Emphasis'),
        ':' => array('Url'),
        '<' => array('UrlTag', 'EmailTag', 'Markup'),
        '[' => array('Link'),
        '_' => array('Emphasis'),
        '`' => array('Code'),
        '~' => array('Tilde'),
        '^' => array('Superscript'),
        '\\' => array('EscapeSequence'),
    );
    
  2. Define methods inlineSuperscript. It should look pretty much like inlineStrikethrough:

    protected function inlineSuperscript($Excerpt)
    {
        if (preg_match('/^\^(.+?)\^/', $Excerpt['text'], $matches))
        {
            return array(
                'extent' => strlen($matches[0]),
                'element' => array(
                    'name' => 'sup',
                    'handler' => array(
                        'function' => 'lineElements',
                        'argument' => $matches[1],
                        'destination' => 'elements',
                    )
                ),
            );
        }
    }
    
  3. Define methods inlineTilde and delete method inlineStrikethrough. It should look pretty much like inlineEmphasis:

    protected function inlineTilde($Excerpt)
    {
        if ( ! isset($Excerpt['text'][1]))
        {
            return;
        }
    
        $marker = $Excerpt['text'][0];
    
        if ($Excerpt['text'][1] === $marker and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches))
        {
            $emphasis = 'del';
        }
        elseif (preg_match('/^~(?=\S)(.+?)(?<=\S)~/', $Excerpt['text'], $matches))
        {
            $emphasis = 'sub';
        }
        else
        {
            return;
        }
    
        return array(
            'extent' => strlen($matches[0]),
            'element' => array(
                'name' => $emphasis,
                'handler' => array(
                    'function' => 'lineElements',
                    'argument' => $matches[1],
                    'destination' => 'elements',
                )
            ),
        );
    }
    
  4. Add the new symbol to $inlineMarkerList:

    protected $inlineMarkerList = '!*_&[:<`~\\^';
    

Upvotes: 1

Related Questions