diff --git a/types/json2md/index.d.ts b/types/json2md/index.d.ts new file mode 100644 index 0000000000..ce15a5909e --- /dev/null +++ b/types/json2md/index.d.ts @@ -0,0 +1,65 @@ +// Type definitions for json2md 1.5 +// Project: https://github.com/IonicaBizau/json2md#readme +// Definitions by: MartynasZilinskas +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +export = json2md; + +/** + * Converts a JSON input to markdown. + * + * @param data The input JSON data. + * @param prefix A snippet to add before each line. + * @return The generated markdown result. + */ +declare function json2md(data: json2md.DataObject | json2md.DataObject[] | string | string[], prefix?: string): string; +type json2md = typeof json2md; + +declare namespace json2md { + const converters: ConvertersMethods; + + namespace DefaultConverters { + interface Converters { + [converter: string]: any; + blockquote: string | string[]; + code: CodeInput; + h1: string | string[]; + h2: string | string[]; + h3: string | string[]; + h4: string | string[]; + h5: string | string[]; + h6: string | string[]; + img: ImgInput | ImgInput[]; + ol: string[]; + p: string | string[]; + table: TableInput; + ul: string[]; + } + + interface ImgInput { + title: string; + source: string; + } + + interface CodeInput { + language?: string; + content: string | string[]; + } + + interface TableInput { + headers: string[]; + rows: Array<{ [column: string]: string }> | string[][]; + } + } + + type DataObject = { + [TConverter in keyof DefaultConverters.Converters]?: DefaultConverters.Converters[TConverter] + }; + + type ConverterCallback = (input: TInput, json2md: json2md) => string; + + type ConvertersMethods = { + [TConverter in keyof DefaultConverters.Converters]: ConverterCallback + }; +} diff --git a/types/json2md/json2md-tests.ts b/types/json2md/json2md-tests.ts new file mode 100644 index 0000000000..1b10c2d6f1 --- /dev/null +++ b/types/json2md/json2md-tests.ts @@ -0,0 +1,210 @@ +import * as json2md from 'json2md'; + +declare function describe(desc: string, f: () => void): void; +declare function it(desc: string, f: () => void): void; + +const EXAMPLE_STRING: string = "example string"; +const EXAMPLE_STRING_ARRAY: string[] = ["A", "B"]; + +describe("Default elements", () => { + it("Headings", () => { + json2md([ + { + h1: EXAMPLE_STRING + }, + { + h2: EXAMPLE_STRING + }, + { + h3: EXAMPLE_STRING + }, + { + h4: EXAMPLE_STRING + }, + { + h5: EXAMPLE_STRING + }, + { + h6: EXAMPLE_STRING + } + ]); + }); + + it("Paragraphs", () => { + json2md([{ + p: EXAMPLE_STRING + }]); + + json2md([{ + p: EXAMPLE_STRING_ARRAY + }]); + }); + + it("Blockquote", () => { + json2md([{ + blockquote: EXAMPLE_STRING + }]); + + json2md([{ + blockquote: EXAMPLE_STRING_ARRAY + }]); + }); + + it("Image", () => { + json2md([{ + img: { + title: EXAMPLE_STRING, + source: EXAMPLE_STRING + } + }]); + + json2md([{ + img: [ + { + title: EXAMPLE_STRING, + source: EXAMPLE_STRING + } + ] + }]); + }); + + it("Unordered list", () => { + json2md([ + { + ul: EXAMPLE_STRING_ARRAY + } + ]); + }); + + it("Ordered list", () => { + json2md([ + { + ol: EXAMPLE_STRING_ARRAY + } + ]); + }); + + it("Code block element", () => { + json2md([ + { + code: { + language: "js", + content: EXAMPLE_STRING + } + } + ]); + }); + + it("Table", () => { + json2md([ + { + table: { + headers: EXAMPLE_STRING_ARRAY, + rows: [ + { + A: EXAMPLE_STRING, + B: EXAMPLE_STRING + } + ] + } + } + ]); + + json2md([ + { + table: { + headers: EXAMPLE_STRING_ARRAY, + rows: [ + EXAMPLE_STRING_ARRAY + ] + } + } + ]); + }); +}); + +it("Custom types", () => { + const customConverter: json2md.ConverterCallback = (input, json2md) => { + return `Hello ${input} world!`; + }; + + json2md.converters.customConverter = customConverter; + + json2md([ + { + customConverter: EXAMPLE_STRING + } + ]); +}); + +it("Big example", () => { + const md: string = json2md([ + { + h1: "JSON To Markdown" + }, + { + blockquote: "A JSON to Markdown converter." + }, + { + img: [ + { + title: "Some image", + source: "https://example.com/some-image.png" + }, + { + title: "Another image", + source: "https://example.com/some-image1.png" + }, + { + title: "Yet another image", + source: "https://example.com/some-image2.png" + } + ] + }, + { + h2: "Features" + }, + { + ul: [ + "Easy to use", + "You can programatically generate Markdown content", + "..." + ] + }, + { + h2: "How to contribute" + }, + { + ol: [ + "Fork the project", + "Create your branch", + "Raise a pull request" + ] + }, + { + h2: "Code blocks" + }, + { + p: "Below you can see a code block example." + }, + { + code: { + language: "js", + content: [ + "function sum (a, b) {", + " return a + b;", + "}", + "sum(1, 2);" + ] + } + } + ]); +}); + +it("Use converter", () => { + const blockquote: string = json2md.converters.blockquote(EXAMPLE_STRING, json2md); + const img: string = json2md.converters.img({ + title: EXAMPLE_STRING, + source: EXAMPLE_STRING + }, json2md); +}); diff --git a/types/json2md/tsconfig.json b/types/json2md/tsconfig.json new file mode 100644 index 0000000000..8fec5e6f49 --- /dev/null +++ b/types/json2md/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "json2md-tests.ts" + ] +} diff --git a/types/json2md/tslint.json b/types/json2md/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/json2md/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }