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

Add support for complex top-level objects #88

Merged
merged 1 commit into from
Sep 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
);

});

Expand Down