Skip to content

Commit d4d5858

Browse files
committed
Fix #135, rendering code blocks enclosed in brackets
fixes #135 close #152
1 parent fccb6ac commit d4d5858

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Version 1.2.0 work in progress
55
------------------------------
66

77
- #89 Lists should be separated by a HR (@bieleckim)
8+
- #135 GithubMarkdown was not parsing inline code when there are square brackets around it.
89
- #151 Fixed table rendering for lines begining with | for GFM (@GenaBitu)
910
- Improved table rendering, allow single column tables.
1011

MarkdownExtra.php

+6
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ protected function renderLink($block)
223223
if (($ref = $this->lookupReference($block['refkey'])) !== false) {
224224
$block = array_merge($block, $ref);
225225
} else {
226+
if (strncmp($block['orig'], '[', 1) === 0) {
227+
return '[' . $this->renderAbsy($this->parseInline(substr($block['orig'], 1)));
228+
}
226229
return $block['orig'];
227230
}
228231
}
@@ -238,6 +241,9 @@ protected function renderImage($block)
238241
if (($ref = $this->lookupReference($block['refkey'])) !== false) {
239242
$block = array_merge($block, $ref);
240243
} else {
244+
if (strncmp($block['orig'], '![', 2) === 0) {
245+
return '![' . $this->renderAbsy($this->parseInline(substr($block['orig'], 2)));
246+
}
241247
return $block['orig'];
242248
}
243249
}

inline/LinkTrait.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function parseLink($markdown)
7979
// remove all starting [ markers to avoid next one to be parsed as link
8080
$result = '[';
8181
$i = 1;
82-
while (isset($markdown[$i]) && $markdown[$i] == '[') {
82+
while (isset($markdown[$i]) && $markdown[$i] === '[') {
8383
$result .= '[';
8484
$i++;
8585
}
@@ -111,7 +111,7 @@ protected function parseImage($markdown)
111111
// remove all starting [ markers to avoid next one to be parsed as link
112112
$result = '!';
113113
$i = 1;
114-
while (isset($markdown[$i]) && $markdown[$i] == '[') {
114+
while (isset($markdown[$i]) && $markdown[$i] === '[') {
115115
$result .= '[';
116116
$i++;
117117
}
@@ -221,6 +221,9 @@ protected function renderLink($block)
221221
if (($ref = $this->lookupReference($block['refkey'])) !== false) {
222222
$block = array_merge($block, $ref);
223223
} else {
224+
if (strncmp($block['orig'], '[', 1) === 0) {
225+
return '[' . $this->renderAbsy($this->parseInline(substr($block['orig'], 1)));
226+
}
224227
return $block['orig'];
225228
}
226229
}
@@ -235,6 +238,9 @@ protected function renderImage($block)
235238
if (($ref = $this->lookupReference($block['refkey'])) !== false) {
236239
$block = array_merge($block, $ref);
237240
} else {
241+
if (strncmp($block['orig'], '![', 2) === 0) {
242+
return '![' . $this->renderAbsy($this->parseInline(substr($block['orig'], 2)));
243+
}
238244
return $block['orig'];
239245
}
240246
}
@@ -275,4 +281,7 @@ protected function consumeReference($lines, $current)
275281
}
276282
return [false, --$current];
277283
}
284+
285+
abstract protected function parseInline($text);
286+
abstract protected function renderAbsy($blocks);
278287
}

tests/markdown-data/code.html

+5
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@
1010
<p>this is code too: <code> co
1111
ooo
1212
de </code></p>
13+
<p>A code block enclosed in brackets: [<code>somecode</code>].</p>
14+
<p>A code block enclosed in brackets: [ <code>somecode</code> ].</p>
15+
<p>A code block enclosed in a link: <a href="./url.md"><code>somecode</code></a>.</p>
16+
<p>A code block enclosed in image brackets: ![<code>somecode</code>].</p>
17+
<p>A code block enclosed in image brackets: ![ <code>somecode</code> ].</p>

tests/markdown-data/code.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ this is ``` inline `` code ```
1414

1515
this is code too: ` co
1616
ooo
17-
de `
17+
de `
18+
19+
A code block enclosed in brackets: [`somecode`].
20+
21+
A code block enclosed in brackets: [ `somecode` ].
22+
23+
A code block enclosed in a link: [`somecode`](./url.md).
24+
25+
A code block enclosed in image brackets: ![`somecode`].
26+
27+
A code block enclosed in image brackets: ![ `somecode` ].

0 commit comments

Comments
 (0)