Skip to content

Commit b39a755

Browse files
authored
Add support for "Github Flavored Markdown Task Lists" (#405)
* Added support for check lists (github flavored markdowns only) * Updated readme * Clean up
1 parent fc79d5e commit b39a755

6 files changed

+58
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ var converter = new ReverseMarkdown.Converter(config);
9797

9898
* Supports all the established html tags like h1, h2, h3, h4, h5, h6, p, em, strong, i, b, blockquote, code, img, a, hr, li, ol, ul, table, tr, th, td, br
9999
* Can deal with nested lists
100-
* Github Flavoured Markdown conversion supported for br, pre and table. Use `var config = new ReverseMarkdown.Config(githubFlavoured:true);`. By default table will always be converted to Github flavored markdown immaterial of this flag.
100+
* Github Flavoured Markdown conversion supported for br, pre, tasklists and table. Use `var config = new ReverseMarkdown.Config(githubFlavoured:true);`. By default table will always be converted to Github flavored markdown immaterial of this flag.
101101

102102
## Acknowledgements
103103
This library's initial implementation ideas were from the Ruby based Html to Markdown converter [ xijo/reverse_markdown](https://github.com/xijo/reverse_markdown).

README.source.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ var converter = new ReverseMarkdown.Converter(config);
9191

9292
* Supports all the established html tags like h1, h2, h3, h4, h5, h6, p, em, strong, i, b, blockquote, code, img, a, hr, li, ol, ul, table, tr, th, td, br
9393
* Supports nested lists
94-
* Github Flavoured Markdown conversion supported for br, pre and table. Use `var config = new ReverseMarkdown.Config(githubFlavoured:true);`. By default the table will always be converted to Github flavored markdown immaterial of this flag.
94+
* Github Flavoured Markdown conversion supported for br, pre, tasklists and table. Use `var config = new ReverseMarkdown.Config(githubFlavoured:true);`. By default the table will always be converted to Github flavored markdown immaterial of this flag.
9595

9696
## Acknowledgements
9797
This library's initial implementation ideas were from the Ruby based Html to Markdown converter [ xijo/reverse_markdown](https://github.com/xijo/reverse_markdown).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- <input type="checkbox" disabled> Unchecked
2+
- <input type="checkbox" checked> Checked
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- [ ] Unchecked
2+
- [x] Checked

src/ReverseMarkdown.Test/ConverterTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,32 @@ public Task WhenThereIsUnorderedListAndBulletIsAsterisk_ThenConvertToMarkdownLis
541541
return CheckConversion(html, new Config {ListBulletChar = '*'});
542542
}
543543

544+
[Fact]
545+
public Task WhenThereIsInputListWithGithubFlavoredEnabled_ThenConvertToMarkdownCheckList()
546+
{
547+
var html = "<ul><li><input type=\"checkbox\" disabled> Unchecked</li><li><input type=\"checkbox\" checked> Checked</li></ul>";
548+
549+
var config = new Config
550+
{
551+
GithubFlavored = true,
552+
};
553+
554+
return CheckConversion(html, config);
555+
}
556+
557+
[Fact]
558+
public Task WhenThereIsInputListWithGithubFlavoredDisabled_ThenConvertToTypicalMarkdownList()
559+
{
560+
var html = "<ul><li><input type=\"checkbox\" disabled> Unchecked</li><li><input type=\"checkbox\" checked> Checked</li></ul>";
561+
562+
var config = new Config
563+
{
564+
GithubFlavored = false,
565+
};
566+
567+
return CheckConversion(html, config);
568+
}
569+
544570
[Fact]
545571
public Task WhenThereIsOrderedList_ThenConvertToMarkdownList()
546572
{

src/ReverseMarkdown/Converters/Li.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
1+
using HtmlAgilityPack;
2+
using System;
23
using System.Linq;
3-
4-
using HtmlAgilityPack;
4+
using System.Text;
55

66
namespace ReverseMarkdown.Converters
77
{
@@ -25,7 +25,7 @@ public override string Convert(HtmlNode node)
2525
}
2626
}
2727

28-
var content = TreatChildren(node);
28+
var content = ContentFor(node);
2929
var indentation = IndentationFor(node, true);
3030
var prefix = PrefixFor(node);
3131

@@ -45,5 +45,27 @@ private string PrefixFor(HtmlNode node)
4545
return $"{Converter.Config.ListBulletChar} ";
4646
}
4747
}
48+
49+
private string ContentFor(HtmlNode node)
50+
{
51+
if (!Converter.Config.GithubFlavored)
52+
return TreatChildren(node);
53+
54+
var content = new StringBuilder();
55+
56+
if (node.FirstChild is HtmlNode childNode
57+
&& childNode.Name == "input"
58+
&& childNode.GetAttributeValue("type", "").Equals("checkbox", StringComparison.OrdinalIgnoreCase))
59+
{
60+
content.Append(childNode.Attributes.Contains("checked")
61+
? $"[x]"
62+
: $"[ ]");
63+
64+
node.RemoveChild(childNode);
65+
}
66+
67+
content.Append(TreatChildren(node));
68+
return content.ToString();
69+
}
4870
}
4971
}

0 commit comments

Comments
 (0)