Skip to content

Commit caeb6c5

Browse files
committed
Merge branch 'master' of https://github.com/younho9/json2md into new-version
2 parents c68bc95 + 670bd16 commit caeb6c5

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

DOCUMENTATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Converts a JSON input to markdown.
2020
| `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png", alt: "My image alt" } }` |
2121
| `ul` | Unordered list | An array of strings or lists representing the items. | `{ ul: ["item 1", "item 2"] }` |
2222
| `ol` | Ordered list | An array of strings or lists representing the items. | `{ ol: ["item 1", "item 2"] }` |
23+
| `taskLists` | Task list | An array of strings or array of task objects or lists representing the items. | `{ taskLists: [{ title: "item 1" }, { title: "item 2", isDone: true }, "item 3"]` |
2324
| `hr` | Separator | None | `{ hr: "" }` |
2425
| `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. | `{ code: { "language": "html", "content": "<script src='dummy.js'></script>" } }` |
2526
| `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s). | `{ table: { headers: ["a", "b"], rows: [{ a: "col1", b: "col2" }] } }` or `{ table: { headers: ["a", "b"], rows: [["col1", "col2"]] } }` |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Converts a JSON input to markdown.
195195
| `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png", alt: "My image alt" } }` |
196196
| `ul` | Unordered list | An array of strings or lists representing the items. | `{ ul: ["item 1", "item 2"] }` |
197197
| `ol` | Ordered list | An array of strings or lists representing the items. | `{ ol: ["item 1", "item 2"] }` |
198+
| `taskLists` | Task list | An array of strings or array of task objects or lists representing the items. | `{ taskLists: [{ title: "item 1" }, { title: "item 2", isDone: true }, "item 3"]` |
198199
| `hr` | Separator | None | `{ hr: "" }` |
199200
| `code` | Code block element | An object containing the `language` (`String`) and `content` (`Array` or `String`) fields. | `{ code: { "language": "html", "content": "<script src='dummy.js'></script>" } }` |
200201
| `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s). | `{ table: { headers: ["a", "b"], rows: [{ a: "col1", b: "col2" }] } }` or `{ table: { headers: ["a", "b"], rows: [["col1", "col2"]] } }` |

lib/converters.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ converters.ul = (input, json2md) => {
7474
let marker = ""
7575

7676
let type = Object.keys(input[i])[0]
77-
if(type !== "ul" && type !== "ol"){
77+
if(type !== "ul" && type !== "ol" && type !== 'taskLists'){
7878
marker += "\n - "
7979
}
8080

@@ -89,7 +89,7 @@ converters.ol = (input, json2md) => {
8989
for (let i = 0; i < input.length; ++i) {
9090
let marker = ""
9191
let type = Object.keys(input[i])[0]
92-
if(type !== "ul" && type !== "ol"){
92+
if(type !== "ul" && type !== "ol" && type !== 'taskLists'){
9393
marker = "\n " + (i + 1 - jumpCount) + ". "
9494
} else {
9595
jumpCount++
@@ -100,6 +100,21 @@ converters.ol = (input, json2md) => {
100100
return c
101101
}
102102

103+
converters.taskLists = (input, json2md) => {
104+
let c = ""
105+
for (let i = 0; i < input.length; ++i) {
106+
let marker = ""
107+
108+
let type = Object.keys(input[i])[0]
109+
if(type !== "ul" && type !== "ol" && type !== 'taskLists'){
110+
marker += input[i].isDone ? "\n - [x] " : "\n - [ ] "
111+
}
112+
113+
c += marker + parseTextFormat(indent(json2md(input[i].title || input[i]), 4, true))
114+
}
115+
return c
116+
}
117+
103118
converters.code = (input, json2md) => {
104119
let c = "```" + (input.language || "") + "\n"
105120
if (Array.isArray(input.content)) {

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ tester.describe("json2md", test => {
9494
cb();
9595
});
9696

97+
// task lists
98+
test.it("should support task lists", function(cb) {
99+
test.expect(json2md({
100+
taskLists: [
101+
{ title: "item 1" }, { title: "item 2", isDone: true }, "item 3"
102+
]
103+
})).toBe("\n - [ ] item 1\n - [x] item 2\n - [ ] item 3\n");
104+
cb();
105+
});
106+
97107
// Code blocks
98108
test.it("should support code blocks", function(cb) {
99109
test.expect(json2md({

0 commit comments

Comments
 (0)