{"id":3625,"date":"2020-05-26T15:06:43","date_gmt":"2020-05-26T21:06:43","guid":{"rendered":"http:\/\/bililite.com\/blog\/?p=3625"},"modified":"2022-07-11T11:04:58","modified_gmt":"2022-07-11T17:04:58","slug":"extending-parsedown-inline-elements","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2020\/05\/26\/extending-parsedown-inline-elements\/","title":{"rendered":"Extending Parsedown: Inline elements"},"content":{"rendered":"<p>Extending <a href=\"http:\/\/bililite.com\/blog\/2020\/05\/22\/extending-parsedown\/\">Parsedown<\/a> involves adding elements to the <code class=\"language-php\" >$InlineTypes<\/code> and <code class=\"language-php\" >$BlockTypes<\/code> arrays, then adding methods to handle them.<\/p>\n<p><a href=https:\/\/github.com\/dwachss\/kavanotparsedown>See the actual code<\/a>.<\/p>\n<h2>Italics<\/h2>\n<p>I use<code class=\"language-html\" > &lt;i&gt;<\/code> a lot, to indicate transliterated words. So I use could use <code class=\"language-php\" >\"\/\"<\/code> to indicate that:<br \/>\n<code class=\"language-markdown\" >\/Shabbat\/ is a Hebrew word<\/code> becomes <code class=\"language-html\" >&lt;i&gt;Shabbat&lt;\/i&gt; is a Hebrew word<\/code>. To do that:<br \/>\ndo<\/p>\n<pre><code class=\"language-php\" >class myParsedown extends Parsedown{\r\n  function __construct(){\r\n    $this->InlineTypes['\/'] []= 'Italic';\r\n    \/\/ after adding all the new inline types, create the list of characters\r\n    $this->inlineMarkerList = implode ('', array_keys($this->InlineTypes));\r\n    \/\/ allow the character to be escaped by '\\'\r\n    $this->specialCharacters []= '\/';\r\n  }\r\n\r\n  protected function inlineItalic($excerpt){\r\n    if (preg_match('#^\/(.+?)\/#', $excerpt['text'], $matches)) {\r\n      return array(\r\n        'extent' => strlen($matches[0]), \r\n        'element' => array(\r\n          'name' => 'i',\r\n          'handler' => array(\r\n            'function' => 'lineElements',\r\n            'argument' => $matches[1],\r\n            'destination' => 'elements'\r\n          )\r\n        )\r\n      );\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>Now, my transliterated words are almost always Hebrew, so I can automatically add the <code class=\"language-html\" >lang=he<\/code> attribute:<\/p>\n<pre><code class=\"language-php\" >\r\n  protected function inlineItalic($excerpt){\r\n    if (preg_match('#^\/(.+?)\/#', $excerpt['text'], $matches)) {\r\n      return array(\r\n        'extent' => strlen($matches[0]), \r\n        'element' => array(\r\n          'name' => 'i',\r\n          'handler' => array(\r\n            'function' => 'lineElements',\r\n            'argument' => $matches[1],\r\n            'destination' => 'elements'\r\n          ),\r\n          'attributes' => array('lang' => 'he') \/\/ Add attributes\r\n        )\r\n      );\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>and now <code class=\"language-markdown\" >\/Shabbat\/ is a Hebrew word<\/code> becomes <code class=\"language-html\" >&lt;i lang=he&gt;Shabbat&lt;\/i&gt; is a Hebrew word<\/code>.<\/p>\n<h2>Cite<\/h2>\n<p>I also use the <code class=\"language-html\" >&lt;cite&gt;<\/code>. I'm running out of single characters to indicate elements, so I'm going to redefine <code class=\"language-php\" >\"-\"<\/code>. I don't need two different markers for <code class=\"language-html\" >&lt;em&gt;<\/code>.<\/p>\n<pre><code class=\"language-php\" >  function __construct(){\r\n    $this->InlineTypes['_'] = ['Cite']; \/\/ redefinition; I am replacing the old array (which was ['Emphasis'])\r\n    \/\/ ... rest of the constructor as above\r\n  }\r\n\r\n  protected function inlineCite($excerpt){\r\n    if (preg_match('#^_(.+?)_#', $excerpt['text'], $matches)) {\r\n      return array(\r\n        'extent' => strlen($matches[0]), \r\n        'element' => array(\r\n          'name' => 'cite',\r\n          'handler' => array(\r\n            'function' => 'lineElements',\r\n            'argument' => $matches[1],\r\n            'destination' => 'elements'\r\n          )\r\n        )\r\n      );\r\n    }\r\n<\/code><\/pre>\n<p>And now <code class=\"language-markdown\" >_A Tale of Two Cities_<\/code> becomes <code class=\"language-html\" >&lt;cite&gt;A Tale of Two Cities&lt;\/cite&gt;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extending Parsedown involves adding elements to the $InlineTypes and $BlockTypes arrays, then adding methods to handle them. See the actual code. Italics I use &lt;i&gt; a lot, to indicate transliterated words. So I use could use \"\/\" to indicate that: \/Shabbat\/ is a Hebrew word becomes &lt;i&gt;Shabbat&lt;\/i&gt; is a Hebrew word. To do that: do [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,9],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3625"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=3625"}],"version-history":[{"count":3,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3625\/revisions"}],"predecessor-version":[{"id":3628,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3625\/revisions\/3628"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=3625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=3625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=3625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}