diff --git a/types/json2md/index.d.ts b/types/json2md/index.d.ts new file mode 100644 index 0000000000..ec40a753b5 --- /dev/null +++ b/types/json2md/index.d.ts @@ -0,0 +1,66 @@ +// 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; + +declare function json2md(data: json2md.ConverterObject | json2md.ConverterObject[] | string | string[], prefix?: string): string; +type json2md = typeof json2md; + +declare namespace json2md { + interface ConverterObject { + [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[]; + } + + type ConverterCallback = (input: TInput, json2md: json2md) => string; + + const converters: ConvertersMethods; + + interface ConvertersMethods { + [converter: string]: ConverterCallback; + + blockquote(input: string | string[], json2md: json2md): string; + code(input: CodeInput, json2md: json2md): string; + h1(input: string, json2md: json2md): string; + h2(input: string, json2md: json2md): string; + h3(input: string, json2md: json2md): string; + h4(input: string, json2md: json2md): string; + h5(input: string, json2md: json2md): string; + h6(input: string, json2md: json2md): string; + img(input: ImgInput | ImgInput[] | string, json2md: json2md): string; + ol(input: string, json2md: json2md): string; + p(input: string, json2md: json2md): string; + table(input: TableInput, json2md: json2md): string; + ul(input: string, json2md: json2md): string; + } + + interface ImgInput { + title: string; + source: string; + } + + interface CodeInput { + language?: string; + content: string | string[]; + } + + interface TableInput { + headers: string[]; + rows: Array<{ [column: string]: string }> | string[][]; + } +} diff --git a/types/json2md/json2md-tests.ts b/types/json2md/json2md-tests.ts new file mode 100644 index 0000000000..bdb071517b --- /dev/null +++ b/types/json2md/json2md-tests.ts @@ -0,0 +1,202 @@ +import * as json2md from 'json2md'; + +declare function describe(desc: string, f: () => void): void; +declare function it(desc: string, f: () => void): void; + +const exampleString: string = "example string"; +const exampleStringArray: string[] = ["A", "B"]; + +describe("Default elements", () => { + it("Headings", () => { + json2md([ + { + h1: exampleString + }, + { + h2: exampleString + }, + { + h3: exampleString + }, + { + h4: exampleString + }, + { + h5: exampleString + }, + { + h6: exampleString + } + ]); + }); + + it("Paragraphs", () => { + json2md([{ + p: exampleString + }]); + + json2md([{ + p: exampleStringArray + }]); + }); + + it("Blockquote", () => { + json2md([{ + blockquote: exampleString + }]); + + json2md([{ + blockquote: exampleStringArray + }]); + }); + + it("Image", () => { + json2md([{ + img: { + title: exampleString, + source: exampleString + } + }]); + + json2md([{ + img: [ + { + title: exampleString, + source: exampleString + } + ] + }]); + }); + + it("Unordered list", () => { + json2md([ + { + ul: exampleStringArray + } + ]); + }); + + it("Ordered list", () => { + json2md([ + { + ol: exampleStringArray + } + ]); + }); + + it("Code block element", () => { + json2md([ + { + code: { + language: "js", + content: exampleString + } + } + ]); + }); + + it("Table", () => { + json2md([ + { + table: { + headers: exampleStringArray, + rows: [ + { + A: exampleString, + B: exampleString + } + ] + } + } + ]); + + json2md([ + { + table: { + headers: exampleStringArray, + rows: [ + exampleStringArray + ] + } + } + ]); + }); +}); + +it("Custom types", () => { + const customConverter: json2md.ConverterCallback = (input, json2md) => { + return `Hello ${input} world!`; + }; + + json2md.converters.customConverter = customConverter; + + json2md([ + { + customConverter: exampleString + } + ]); +}); + +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);" + ] + } + } + ]); +}); 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" }