Comparing version 1.8.0 to 1.9.0
@@ -5,2 +5,4 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var converters = module.exports = {}; | ||
@@ -115,3 +117,13 @@ | ||
converters.table = function (input, json2md) { | ||
var _PREFERRED_LENGTH_PER; | ||
var ALIGNMENT = { | ||
CENTER: 'center', | ||
RIGHT: 'right', | ||
LEFT: 'left', | ||
NONE: 'none' | ||
}; | ||
var PREFERRED_LENGTH_PER_ALIGNMENT = (_PREFERRED_LENGTH_PER = {}, _defineProperty(_PREFERRED_LENGTH_PER, ALIGNMENT.CENTER, 3), _defineProperty(_PREFERRED_LENGTH_PER, ALIGNMENT.RIGHT, 2), _defineProperty(_PREFERRED_LENGTH_PER, ALIGNMENT.LEFT, 2), _defineProperty(_PREFERRED_LENGTH_PER, ALIGNMENT.NONE, 1), _PREFERRED_LENGTH_PER); | ||
if ((typeof input === "undefined" ? "undefined" : _typeof(input)) !== "object" || !input.hasOwnProperty("headers") || !input.hasOwnProperty("rows")) { | ||
@@ -121,25 +133,54 @@ return ""; | ||
var header = " | " + input.headers.join(" | ") + " | ", | ||
spaces = " | " + input.headers.map(function (_, index) { | ||
if (input.aligns && input.aligns[index]) { | ||
switch (input.aligns[index]) { | ||
case "center": | ||
return ":---:"; | ||
case "right": | ||
return "---:"; | ||
case "left": | ||
return ":---"; | ||
default: | ||
return "---"; | ||
} | ||
var alignment = input.headers.map(function (_, index) { | ||
return input.aligns && input.aligns[index] ? input.aligns[index] : ALIGNMENT.NONE; | ||
}); | ||
// try to match the space the column name and the dashes (and colons) take up. Minimum depends on alignment | ||
var preferred_lengths = input.headers.map(function (header, index) { | ||
return Math.max(PREFERRED_LENGTH_PER_ALIGNMENT[alignment[index]], header.length - 2); | ||
}); | ||
// add spaces around column name if necessary (side(s) depends on alignment) | ||
var column_names = input.headers.map(function (header, index) { | ||
var diff = preferred_lengths[index] + 2 - header.length; | ||
switch (alignment[index]) { | ||
case ALIGNMENT.RIGHT: | ||
return " ".repeat(diff) + header; | ||
case ALIGNMENT.LEFT: | ||
return header + " ".repeat(diff); | ||
case ALIGNMENT.CENTER: | ||
case ALIGNMENT.NONE: | ||
default: | ||
return " ".repeat(Math.floor(diff / 2)) + header + " ".repeat(Math.ceil(diff / 2)); | ||
} | ||
return "---"; | ||
}).join(" | ") + " | ", | ||
data = " | " + input.rows.map(function (r) { | ||
return Array.isArray(r) ? r.map(function (el) { | ||
return parseTextFormat(json2md(el)); | ||
}).join(" | ") : input.headers.map(function (h) { | ||
return parseTextFormat(json2md(r[h])); | ||
}); | ||
var header = "| " + column_names.join(" | ") + " |"; | ||
var spaces = "| " + input.headers.map(function (_, index) { | ||
var inner = "-".repeat(preferred_lengths[index]); | ||
switch (alignment[index]) { | ||
case ALIGNMENT.CENTER: | ||
return ":" + inner + ":"; | ||
case ALIGNMENT.RIGHT: | ||
return "-" + inner + ":"; | ||
case ALIGNMENT.LEFT: | ||
return ":" + inner + "-"; | ||
case ALIGNMENT.NONE: | ||
default: | ||
return "-" + inner + "-"; | ||
} | ||
}).join(" | ") + " |"; | ||
var data = "| " + input.rows.map(function (row) { | ||
return (Array.isArray(row) ? row : input.headers.map(function (col_id) { | ||
return row[col_id]; | ||
})).map(function (cell) { | ||
return json2md(cell); | ||
}).map(function (cell) { | ||
return parseTextFormat(cell); | ||
}).map(function (cell) { | ||
return cell.replace(/(?<!(\\))\|/, "\\|"); | ||
}).join(" | "); | ||
}).join("\n") + " | "; | ||
}).join("\n") + " |"; | ||
@@ -146,0 +187,0 @@ return [header, spaces, data].join("\n"); |
@@ -12,18 +12,18 @@ "use strict"; | ||
* | ||
* | Type | Element | Data | Example | | ||
* |--------------|--------------------|--------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------| | ||
* | `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` | | ||
* | `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` | | ||
* | `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` | | ||
* | `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` | | ||
* | `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` | | ||
* | `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` | | ||
* | `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` | | ||
* | `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` | | ||
* | `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png", alt: "My image alt" } }` | | ||
* | `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` | | ||
* | `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` | | ||
* | `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>" } }` | | ||
* | `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"]] } }` | | ||
* | `link` | Link | An object containing the `title` and the `source` fields. | `{ title: 'hello', source: 'https://ionicabizau.net' } | ||
* | Type | Element | Data | Example | | ||
* |--------------|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
* | `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` | | ||
* | `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` | | ||
* | `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` | | ||
* | `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` | | ||
* | `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` | | ||
* | `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` | | ||
* | `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` | | ||
* | `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` | | ||
* | `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png", alt: "My image alt" } }` | | ||
* | `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` | | ||
* | `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` | | ||
* | `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>" } }` | | ||
* | `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s) and optionally `aligns` (`Array` of `String`s). | `{ table: { headers: ["a", "b"], rows: [{ a: "col1", b: "col2" }] } }` or `{ table: { headers: ["a", "b"], rows: [["col1", "col2"]] } }` or `{ table: { headers: ["a", "b"], aligns: ["left", "center"], rows: [["col1", "col2"]] } }` | | ||
* | `link` | Link | An object containing the `title` and the `source` fields. | `{ title: 'hello', source: 'https://ionicabizau.net' } | | ||
* | ||
@@ -30,0 +30,0 @@ * |
{ | ||
"name": "json2md", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "A JSON to Markdown converter.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -185,18 +185,18 @@ <!-- Please do not edit this file. Edit the `blah` field in the `package.json` instead. If in doubt, open an issue. --> | ||
| Type | Element | Data | Example | | ||
|--------------|--------------------|--------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` | | ||
| `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` | | ||
| `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` | | ||
| `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` | | ||
| `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` | | ||
| `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` | | ||
| `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` | | ||
| `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` | | ||
| `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png", alt: "My image alt" } }` | | ||
| `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` | | ||
| `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` | | ||
| `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>" } }` | | ||
| `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"]] } }` | | ||
| `link` | Link | An object containing the `title` and the `source` fields. | `{ title: 'hello', source: 'https://ionicabizau.net' } | ||
| Type | Element | Data | Example | | ||
|--------------|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| `h1` | Heading 1 | The heading text as string. | `{ h1: "heading 1" }` | | ||
| `h2` | Heading 2 | The heading text as string. | `{ h2: "heading 2" }` | | ||
| `h3` | Heading 3 | The heading text as string. | `{ h3: "heading 3" }` | | ||
| `h4` | Heading 4 | The heading text as string. | `{ h4: "heading 4" }` | | ||
| `h5` | Heading 5 | The heading text as string. | `{ h5: "heading 5" }` | | ||
| `h6` | Heading 6 | The heading text as string. | `{ h6: "heading 6" }` | | ||
| `p` | Paragraphs | The paragraph text as string or array (multiple paragraphs). | `{ p: "Hello World"}` or multiple paragraphs: `{ p: ["Hello", "World"] }` | | ||
| `blockquote` | Blockquote | The blockquote as string or array (multiple blockquotes) | `{ blockquote: "Hello World"}` or multiple blockquotes: `{ blockquote: ["Hello", "World"] }` | | ||
| `img` | Image | An object or an array of objects containing the `title`, `source` and `alt` fields. | `{ img: { title: "My image title", source: "http://example.com/image.png", alt: "My image alt" } }` | | ||
| `ul` | Unordered list | An array of strings representing the items. | `{ ul: ["item 1", "item 2"] }` | | ||
| `ol` | Ordered list | An array of strings representing the items. | `{ ol: ["item 1", "item 2"] }` | | ||
| `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>" } }` | | ||
| `table` | Table | An object containing the `headers` (`Array` of `String`s) and `rows` (`Array` of `Array`s or `Object`s) and optionally `aligns` (`Array` of `String`s). | `{ table: { headers: ["a", "b"], rows: [{ a: "col1", b: "col2" }] } }` or `{ table: { headers: ["a", "b"], rows: [["col1", "col2"]] } }` or `{ table: { headers: ["a", "b"], aligns: ["left", "center"], rows: [["col1", "col2"]] } }` | | ||
| `link` | Link | An object containing the `title` and the `source` fields. | `{ title: 'hello', source: 'https://ionicabizau.net' } | | ||
@@ -323,3 +323,2 @@ You can extend the `json2md.converters` object to support your custom types. | ||
- `@bonitasoft/dependency-list-to-markdown` | ||
- `dokuinjs` | ||
- `lggn` | ||
@@ -330,4 +329,6 @@ - `gatsby-source-google-docs-sheets` | ||
- `node-red-contrib-json2md` | ||
- `gatsby-source-google-docs` | ||
- `@feizheng/react-markdown-props`I am using this library to generate documentation for my projects, being integrated with [blah](https://github.com/IonicaBizau/node-blah). | ||
- `@feizheng/react-markdown-props` | ||
- `@s-ui/changelog` | ||
- `dokuinjs` | ||
- `gatsby-source-google-docs`I am using this library to generate documentation for my projects, being integrated with [blah](https://github.com/IonicaBizau/node-blah). | ||
@@ -334,0 +335,0 @@ |
37707
350
362