Skip to content

Commit

Permalink
Updated Markdown extension, better event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
markseuffert committed Apr 3, 2024
1 parent d002650 commit 5c9bbaa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
42 changes: 31 additions & 11 deletions system/extensions/markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Markdown extension, https://github.com/annaesvensson/yellow-markdown

class YellowMarkdown {
const VERSION = "0.8.27";
const VERSION = "0.8.28";
public $yellow; // access to API

// Handle initialisation
Expand Down Expand Up @@ -3911,9 +3911,10 @@ public function _doAutoLinks_shortcutSymbol_callback($matches) {

// Handle fenced code blocks
public function _doFencedCodeBlocks_callback($matches) {
$name = $this->getBlockName($matches[2], $matches[3]);
$text = $matches[4];
$name = is_string_empty($matches[2]) ? "" : trim("$matches[2] $matches[3]");
$output = $this->page->parseContentElement($name, $text, "", "code");
$attributes = $matches[3];
$output = $this->page->parseContentElement($name, $text, $attributes, "code");
if (is_null($output)) {
$attr = $this->doExtraAttributes("pre", ".$matches[2] $matches[3]");
$output = "<pre$attr><code>".htmlspecialchars($text, ENT_NOQUOTES)."</code></pre>";
Expand Down Expand Up @@ -4007,19 +4008,25 @@ public function doNoticeBlocks($text) {

// Handle notice blocks over multiple lines
public function _doNoticeBlocks_callback($matches) {
$lines = $matches[1];
$attr = "";
$text = preg_replace("/^[ ]*![ ]?/m", "", $lines);
if (preg_match("/^[ ]*".$this->id_class_attr_catch_re."[ ]*\n([\S\s]*)$/m", $text, $matches)) {
$attr = $this->doExtraAttributes("div", $dummy =& $matches[1]);
$text = $matches[2];
$name = $attributes = $attr = "";
$text = preg_replace("/^[ ]*![ ]?/m", "", $matches[1]);
if (preg_match("/^[ ]*".$this->id_class_attr_catch_re."[ ]*\n([\S\s]*)$/m", $text, $parts)) {
$name = $this->getBlockName("", $parts[1]);
$text = $parts[2];
$attributes = $parts[1];
$attr = $this->doExtraAttributes("div", $parts[1]);
} elseif ($this->noticeLevel==0) {
$level = strspn(str_replace(array("![", " "), "", $lines), "!");
$level = strspn(str_replace(array("![", " "), "", $matches[1]), "!");
$attr = " class=\"notice$level\"";
}
if (!is_string_empty($text)) {
++$this->noticeLevel;
$output = "<div$attr>\n".$this->runBlockGamut($text)."\n</div>";
$output = $this->page->parseContentElement($name, "[--notice--]", $attributes, "notice");
if (!is_null($output) && preg_match("/^(.+)(\[--notice--\])(.+)$/s", $output, $parts)) {
$output = $parts[1].$this->runBlockGamut($text).$parts[3];
} else {
$output = "<div$attr>\n".$this->runBlockGamut($text)."\n</div>";
}
--$this->noticeLevel;
} else {
$output = "<div$attr></div>";
Expand All @@ -4045,6 +4052,19 @@ public function doFootnotesLinks($text) {
}
return $text;
}

// Return suitable name for code block or notice block
public function getBlockName($language, $attributes) {
if (!is_string_empty($language)) {
$name = ltrim($language, ".");
} else {
$name = "";
foreach (explode(" ", $attributes) as $token) {
if (substru($token, 0, 1)==".") { $name = substru($token, 1); break; }
}
}
return $name;
}

// Return unique id attribute
public function getIdAttribute($text) {
Expand Down
8 changes: 4 additions & 4 deletions system/extensions/update-available.ini
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,14 @@ media/images/language-en.png: language-en.png, create, optional
media/images/language-sv.png: language-sv.png, create, optional

Extension: Highlight
Version: 0.8.17
Version: 0.8.18
Description: Highlight source code.
Developer: Anna Svensson
Tag: feature
DownloadUrl: https://github.com/annaesvensson/yellow-highlight/archive/refs/heads/main.zip
DocumentationUrl: https://github.com/annaesvensson/yellow-highlight
DocumentationLanguage: en, de, sv
Published: 2024-04-01 19:30:24
Published: 2024-04-03 10:13:46
Status: available
system/extensions/highlight.php: highlight.php, create, update
system/extensions/highlight.css: highlight.css, create, update
Expand Down Expand Up @@ -501,14 +501,14 @@ system/themes/karlskrona.css: karlskrona.css, create, update, careful
system/themes/karlskrona.png: karlskrona.png, create

Extension: Markdown
Version: 0.8.27
Version: 0.8.28
Description: Text formatting for humans.
Developer: Anna Svensson
Tag: feature
DownloadUrl: https://github.com/annaesvensson/yellow-markdown/archive/refs/heads/main.zip
DocumentationUrl: https://github.com/annaesvensson/yellow-markdown
DocumentationLanguage: en, de, sv
Published: 2024-04-01 18:41:04
Published: 2024-04-03 10:08:24
Status: available
system/extensions/markdown.php: markdown.php, create, update

Expand Down
4 changes: 2 additions & 2 deletions system/extensions/update-current.ini
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ media/downloads/yellow.pdf: yellow.pdf, create
./robots.txt: robots.txt, create

Extension: Markdown
Version: 0.8.27
Version: 0.8.28
Description: Text formatting for humans.
Developer: Anna Svensson
Tag: feature
DownloadUrl: https://github.com/annaesvensson/yellow-markdown/archive/refs/heads/main.zip
DocumentationUrl: https://github.com/annaesvensson/yellow-markdown
DocumentationLanguage: en, de, sv
Published: 2024-04-01 18:41:04
Published: 2024-04-03 10:08:24
Status: available
system/extensions/markdown.php: markdown.php, create, update

Expand Down

0 comments on commit 5c9bbaa

Please sign in to comment.