|
| 1 | +## Documentation |
| 2 | + |
| 3 | +You can see below the API reference of this module. |
| 4 | + |
| 5 | +### `json2md(data, prefix)` |
| 6 | +Converts a JSON input to markdown. |
| 7 | + |
| 8 | +**Supported elements** |
| 9 | + |
| 10 | +| Type | Element | Data | Example | |
| 11 | +|--------------|--------------------|--------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------| |
| 12 | +| `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` | |
| 13 | +| `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` | |
| 14 | +| `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` | |
| 15 | +| `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` | |
| 16 | +| `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` | |
| 17 | +| `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` | |
| 18 | +| `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` | |
| 19 | +| `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` | |
| 20 | +| `img` | Image | An object or an array of objects containing the `title` and `source` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png" } }` | |
| 21 | +| `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` | |
| 22 | +| `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` | |
| 23 | +| `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>" } }` | |
| 24 | +| `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"]] } }` | |
| 25 | + |
| 26 | +You can extend the `json2md.converters` object to support your custom types. |
| 27 | + |
| 28 | +```js |
| 29 | +json2md.converters.sayHello = function (input, json2md) { |
| 30 | + return "Hello " + input + "!"; |
| 31 | +}; |
| 32 | +``` |
| 33 | + |
| 34 | +Then you can use it: |
| 35 | + |
| 36 | +```js |
| 37 | +json2md({ sayHello: "World" }); |
| 38 | +// => "Hello World!" |
| 39 | +``` |
| 40 | + |
| 41 | +#### Params |
| 42 | +- **Array|Object|String** `data`: The input JSON data. |
| 43 | +- **String** `prefix`: A snippet to add before each line. |
| 44 | + |
| 45 | +#### Return |
| 46 | +- **String** The generated markdown result. |
| 47 | + |
0 commit comments