diff --git a/README.md b/README.md index 3446bc3..4642a97 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ var converter = new ReverseMarkdown.Converter(config); * 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 * Can deal with nested lists -* 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. +* 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. ## Acknowledgements This library's initial implementation ideas were from the Ruby based Html to Markdown converter [ xijo/reverse_markdown](https://github.com/xijo/reverse_markdown). diff --git a/README.source.md b/README.source.md index d0eb171..15453f4 100644 --- a/README.source.md +++ b/README.source.md @@ -91,7 +91,7 @@ var converter = new ReverseMarkdown.Converter(config); * 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 * Supports nested lists -* 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. +* 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. ## Acknowledgements This library's initial implementation ideas were from the Ruby based Html to Markdown converter [ xijo/reverse_markdown](https://github.com/xijo/reverse_markdown). diff --git a/src/ReverseMarkdown.Test/ConverterTests.WhenThereIsInputListWithGithubFlavoredDisabled_ThenConvertToTypicalMarkdownList.verified.md b/src/ReverseMarkdown.Test/ConverterTests.WhenThereIsInputListWithGithubFlavoredDisabled_ThenConvertToTypicalMarkdownList.verified.md new file mode 100644 index 0000000..9b65eb4 --- /dev/null +++ b/src/ReverseMarkdown.Test/ConverterTests.WhenThereIsInputListWithGithubFlavoredDisabled_ThenConvertToTypicalMarkdownList.verified.md @@ -0,0 +1,2 @@ +- Unchecked +- Checked \ No newline at end of file diff --git a/src/ReverseMarkdown.Test/ConverterTests.WhenThereIsInputListWithGithubFlavoredEnabled_ThenConvertToMarkdownCheckList.verified.md b/src/ReverseMarkdown.Test/ConverterTests.WhenThereIsInputListWithGithubFlavoredEnabled_ThenConvertToMarkdownCheckList.verified.md new file mode 100644 index 0000000..80cb384 --- /dev/null +++ b/src/ReverseMarkdown.Test/ConverterTests.WhenThereIsInputListWithGithubFlavoredEnabled_ThenConvertToMarkdownCheckList.verified.md @@ -0,0 +1,2 @@ +- [ ] Unchecked +- [x] Checked \ No newline at end of file diff --git a/src/ReverseMarkdown.Test/ConverterTests.cs b/src/ReverseMarkdown.Test/ConverterTests.cs index b2c2923..fc4c971 100644 --- a/src/ReverseMarkdown.Test/ConverterTests.cs +++ b/src/ReverseMarkdown.Test/ConverterTests.cs @@ -541,6 +541,32 @@ public Task WhenThereIsUnorderedListAndBulletIsAsterisk_ThenConvertToMarkdownLis return CheckConversion(html, new Config {ListBulletChar = '*'}); } + [Fact] + public Task WhenThereIsInputListWithGithubFlavoredEnabled_ThenConvertToMarkdownCheckList() + { + var html = ""; + + var config = new Config + { + GithubFlavored = true, + }; + + return CheckConversion(html, config); + } + + [Fact] + public Task WhenThereIsInputListWithGithubFlavoredDisabled_ThenConvertToTypicalMarkdownList() + { + var html = ""; + + var config = new Config + { + GithubFlavored = false, + }; + + return CheckConversion(html, config); + } + [Fact] public Task WhenThereIsOrderedList_ThenConvertToMarkdownList() { diff --git a/src/ReverseMarkdown/Converters/Li.cs b/src/ReverseMarkdown/Converters/Li.cs index 0b05e21..7359d19 100644 --- a/src/ReverseMarkdown/Converters/Li.cs +++ b/src/ReverseMarkdown/Converters/Li.cs @@ -1,7 +1,7 @@ -using System; +using HtmlAgilityPack; +using System; using System.Linq; - -using HtmlAgilityPack; +using System.Text; namespace ReverseMarkdown.Converters { @@ -25,7 +25,7 @@ public override string Convert(HtmlNode node) } } - var content = TreatChildren(node); + var content = ContentFor(node); var indentation = IndentationFor(node, true); var prefix = PrefixFor(node); @@ -45,5 +45,27 @@ private string PrefixFor(HtmlNode node) return $"{Converter.Config.ListBulletChar} "; } } + + private string ContentFor(HtmlNode node) + { + if (!Converter.Config.GithubFlavored) + return TreatChildren(node); + + var content = new StringBuilder(); + + if (node.FirstChild is HtmlNode childNode + && childNode.Name == "input" + && childNode.GetAttributeValue("type", "").Equals("checkbox", StringComparison.OrdinalIgnoreCase)) + { + content.Append(childNode.Attributes.Contains("checked") + ? $"[x]" + : $"[ ]"); + + node.RemoveChild(childNode); + } + + content.Append(TreatChildren(node)); + return content.ToString(); + } } }