Skip to content

Commit

Permalink
Added support for checklists and removed slashes on img and hr tags
Browse files Browse the repository at this point in the history
  • Loading branch information
lux committed Aug 6, 2024
1 parent 23496f5 commit f15efcb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ following elements (and can be extended via `Slimdown::add_rule()`):
* Inline code
* Blockquotes
* Ordered/unordered lists
* Checklists
* Images

Originally hosted as a [gist here](https://gist.github.com/jbroadway/2836900).
Expand Down Expand Up @@ -113,5 +114,9 @@ More text with `inline($code)` sample.
> A block quote
> across two lines.

- [ ] One
- [x] Two
- [ ] Three

More text...");
```
8 changes: 5 additions & 3 deletions Slimdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ class Slimdown {
'/(\~\~)(?=(?:(?:[^`]*`[^`\r\n]*`)*[^`]*$))(?![^\/<]*>.*<\/.+>)(.*?)\1/' => '<del>\2</del>', // del
'/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote
'/`(.*?)`/' => '<code>\1</code>', // inline code
'/\n\*(.*)/' => self::class .'::ul_list', // ul lists
'/(\n[\*|\-] )\[x\]/' => '\1<input type=\'checkbox\' disabled checked>', // checkbox checked
'/(\n[\*|\-] )\[\ \]/' => '\1<input type=\'checkbox\' disabled>', // checkbox unchecked
'/\n[\*|\-] (.*)/' => self::class .'::ul_list', // ul lists
'/\n[0-9]+\.(.*)/' => self::class .'::ol_list', // ol lists
'/\n(&gt;|\>)(.*)/' => self::class .'::blockquote', // blockquotes
'/\n-{5,}/' => "\n<hr />", // horizontal rule
'/\n-{5,}/' => "\n<hr>", // horizontal rule
'/\n([^\n]+)\n/' => self::class .'::para', // add paragraphs
'/<\/ul>\s?<ul>/' => '', // fix extra ul
'/<\/ol>\s?<ol>/' => '', // fix extra ol
Expand Down Expand Up @@ -113,7 +115,7 @@ private static function img ($regs) {
list ($tmp, $text, $link) = $regs;
// Substitute _ and * in links so they don't break the URLs
$link = str_replace (['_', '*'], ['{^^^}', '{~~~}'], $link);
return sprintf ('<img src=\'%s\' alt=\'%s\' />', $link, $text);
return sprintf ('<img src=\'%s\' alt=\'%s\'>', $link, $text);
}

private static function fix_link ($regs) {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/expected/012-hr.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p>Paragraph 1.</p>

<hr />
<hr>

<p>Paragraph 2.</p>
4 changes: 2 additions & 2 deletions tests/fixtures/expected/014-img.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<p><img src='http://example.com/foo_underscore_test.jpg' alt='alt text' /></p>
<p><img src='http://example.com/foo_underscore_test.jpg' alt='alt text'></p>

<p><img src='http://example.com/foo_underscore_test.jpg' alt='' /></p>
<p><img src='http://example.com/foo_underscore_test.jpg' alt=''></p>
7 changes: 7 additions & 0 deletions tests/fixtures/expected/016-checkists.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul>
<li><input type='checkbox' disabled> One</li>

<li><input type='checkbox' disabled checked> Two</li>

<li><input type='checkbox' disabled> Three</li>
</ul>
3 changes: 3 additions & 0 deletions tests/fixtures/input/016-checklists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [ ] One
- [x] Two
- [ ] Three

0 comments on commit f15efcb

Please sign in to comment.