Skip to content

Commit

Permalink
Merge branch 'object-support' of github.com:steghoja/json2md
Browse files Browse the repository at this point in the history
  • Loading branch information
IonicaBizau committed Sep 16, 2022
2 parents 08bc549 + cde5136 commit d83aec8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ function json2md(data, prefix, _type) {
}
return content.join("\n")
} else {
let type = Object.keys(data)[0]
, func = converters[_type || type]
let mdText = "";
Object.keys(data).forEach((type, index, array) => {
let func = converters[_type || type];

if (typeof func === "function") {
return indento(func(_type ? data : data[type], json2md), 1, prefix) + "\n"
}
throw new Error("There is no such converter: " + type)
if (typeof func === "function") {
mdText += indento(func(_type ? data : data[type], json2md), 1, prefix) + "\n";
} else {
throw new Error("There is no such converter: " + type);
}
});
return mdText;
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"Dmitry Tsvettsikh <me@reklatsmasters.com>",
"Daniel Bastos <danielbastos@live.com>",
"Keith Chen <keith.chenzhun@yahoo.com>",
"Cédric Delpoux <cedric.delpoux@gmail.com>"
"Cédric Delpoux <cedric.delpoux@gmail.com>",
"Jan-Philipp Steghöfer <gh@splashstudio.com>"
],
"license": "MIT",
"bugs": {
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,19 @@ tester.describe("json2md", test => {
`);
cb();
})

test.it("should support several top-level object keys",
function(cb) {
json2md.converters.sayHello = function(input, json2md) {
return "Hello " + input + "!";
};
test.expect(json2md({
sayHello: "World",
h1: "Hello Friends!"
})).toBe("Hello World!\n# Hello Friends!\n")
cb();
}
);

test.it("should support tables with links", function(cb) {
test.expect(json2md({
Expand Down

0 comments on commit d83aec8

Please sign in to comment.