Skip to content

Instantly share code, notes, and snippets.

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 magadanskiuchen/74aa03205f2167c89ded7bffcf412ee2 to your computer and use it in GitHub Desktop.
Save magadanskiuchen/74aa03205f2167c89ded7bffcf412ee2 to your computer and use it in GitHub Desktop.
Partial update to WP-MarkDown's Markdownify class to add support for GitHub flavor-like codeblocks
diff --git a/markdownify/markdownify.php b/markdownify/markdownify.php
index e9b43bd..24ddac3 100644
--- a/markdownify/markdownify.php
+++ b/markdownify/markdownify.php
@@ -816,15 +816,39 @@ class Markdownify {
*/
public function handleTag_pre() {
if ($this->keepHTML && $this->parser->isStartTag) {
- # check if a simple <code> follows
- if (!preg_match('#^\s*<code\s*>#Us', $this->parser->html)) {
+ # check if a simple <code> or <code class=""> follows
+ $this->parser->prelang = '';
+ $code_match = preg_match('#^\s*<code\s*(class="' . preg_quote(MARKDOWN_CODE_CLASS_PREFIX, '#') . '([^"]+)\s*)?">#Us', $this->parser->html, $language);
+
+ if ($code_match) {
+ $this->parser->prelang = !empty($language[2]) ? $language[2] : '';
+
+ $this->out('```' . $this->parser->prelang . "\n");
+
+ $this->buffer();
+ } else {
# this is no standard markdown code block
$this->handleTagToText();
return;
}
}
- $this->indent(' ');
+
+ if ($this->parser->prelang == '') {
+ $this->indent(' ');
+ }
+
if (!$this->parser->isStartTag) {
+ if ($this->parser->prelang != '') {
+ $buffer = $this->unbuffer();
+ $buffer = preg_replace('#</?code[^>]*>#', '', $buffer);
+
+ $this->out($buffer);
+ $this->setLineBreaks(1);
+ $this->out('```');
+
+ $this->parser->prelang = '';
+ }
+
$this->setLineBreaks(2);
} else {
$this->parser->html = ltrim($this->parser->html);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment