Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion README.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- <input type="checkbox" disabled> Unchecked
- <input type="checkbox" checked> Checked
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ ] Unchecked
- [x] Checked
26 changes: 26 additions & 0 deletions src/ReverseMarkdown.Test/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,32 @@ public Task WhenThereIsUnorderedListAndBulletIsAsterisk_ThenConvertToMarkdownLis
return CheckConversion(html, new Config {ListBulletChar = '*'});
}

[Fact]
public Task WhenThereIsInputListWithGithubFlavoredEnabled_ThenConvertToMarkdownCheckList()
{
var html = "<ul><li><input type=\"checkbox\" disabled> Unchecked</li><li><input type=\"checkbox\" checked> Checked</li></ul>";

var config = new Config
{
GithubFlavored = true,
};

return CheckConversion(html, config);
}

[Fact]
public Task WhenThereIsInputListWithGithubFlavoredDisabled_ThenConvertToTypicalMarkdownList()
{
var html = "<ul><li><input type=\"checkbox\" disabled> Unchecked</li><li><input type=\"checkbox\" checked> Checked</li></ul>";

var config = new Config
{
GithubFlavored = false,
};

return CheckConversion(html, config);
}

[Fact]
public Task WhenThereIsOrderedList_ThenConvertToMarkdownList()
{
Expand Down
30 changes: 26 additions & 4 deletions src/ReverseMarkdown/Converters/Li.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using HtmlAgilityPack;
using System;
using System.Linq;

using HtmlAgilityPack;
using System.Text;

namespace ReverseMarkdown.Converters
{
Expand All @@ -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);

Expand All @@ -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();
}
}
}