Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify to support code #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
packages
*kanso*
.idea/
9 changes: 8 additions & 1 deletion html2markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ function html2markdown(html, opts) {
"ul": "* ",
"ol": "1. ",
"dl": "- ",
"blockquote": "> "
"blockquote": "> ",
"code": '`'
};

if (!parser && typeof markdownDOMParser !== 'undefined') {
Expand Down Expand Up @@ -231,6 +232,12 @@ function html2markdown(html, opts) {
nodeList.push(markdownTags[tag]);
break;
case "code":
if (preStack.length > 0) {
break;
}

nodeList.push(markdownTags[tag]);
break;
case "span":
if (preStack.length > 0) {
break;
Expand Down
13 changes: 13 additions & 0 deletions spec/html2markdown_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ for(var key in parsers) {
expect(md).toEqual("before this is span element after");
});

it("should be able to convert code element", function() {
var html= "this is <code>code</code> element.";
var md = markdown(html);

expect(md).toEqual("this is `code` element.");

md = markdown("before<code>this is code element</code>after");
expect(md).toEqual("before`this is code element`after");

md = markdown("before <code>this is code element</code> after");
expect(md).toEqual("before `this is code element` after");
});

it("should be able to convert '<blockquote>This is blockquoted</blockquote>' to '> This is blockquoted'", function() {
var md = markdown("<blockquote>This is blockquoted</blockquote>");
expect(md).toMatch("> This is blockquoted");
Expand Down