mdast-util-gfm-table
Advanced tools
Comparing version 0.1.6 to 1.0.0
236
index.js
@@ -1,2 +0,234 @@ | ||
exports.fromMarkdown = require('./from-markdown') | ||
exports.toMarkdown = require('./to-markdown') | ||
/** | ||
* @typedef {import('mdast').Table} Table | ||
* @typedef {import('mdast').TableRow} TableRow | ||
* @typedef {import('mdast').TableCell} TableCell | ||
* @typedef {import('mdast').InlineCode} InlineCode | ||
* @typedef {import('markdown-table').MarkdownTableOptions} MarkdownTableOptions | ||
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension | ||
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle | ||
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension | ||
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle | ||
* @typedef {import('mdast-util-to-markdown').Context} ToMarkdownContext | ||
* | ||
* @typedef Options | ||
* @property {boolean} [tableCellPadding=true] | ||
* @property {boolean} [tablePipeAlign=true] | ||
* @property {MarkdownTableOptions['stringLength']} [stringLength] | ||
*/ | ||
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js' | ||
import {inlineCode} from 'mdast-util-to-markdown/lib/handle/inline-code.js' | ||
import {markdownTable} from 'markdown-table' | ||
/** @type {FromMarkdownExtension} */ | ||
export const gfmTableFromMarkdown = { | ||
enter: { | ||
table: enterTable, | ||
tableData: enterCell, | ||
tableHeader: enterCell, | ||
tableRow: enterRow | ||
}, | ||
exit: { | ||
codeText: exitCodeText, | ||
table: exitTable, | ||
tableData: exit, | ||
tableHeader: exit, | ||
tableRow: exit | ||
} | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function enterTable(token) { | ||
// @ts-expect-error: `align` is custom. | ||
this.enter({type: 'table', align: token._align, children: []}, token) | ||
this.setData('inTable', true) | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function exitTable(token) { | ||
this.exit(token) | ||
this.setData('inTable') | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function enterRow(token) { | ||
this.enter({type: 'tableRow', children: []}, token) | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function exit(token) { | ||
this.exit(token) | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function enterCell(token) { | ||
this.enter({type: 'tableCell', children: []}, token) | ||
} | ||
// Overwrite the default code text data handler to unescape escaped pipes when | ||
// they are in tables. | ||
/** @type {FromMarkdownHandle} */ | ||
function exitCodeText(token) { | ||
let value = this.resume() | ||
if (this.getData('inTable')) { | ||
value = value.replace(/\\([\\|])/g, replace) | ||
} | ||
this.stack[this.stack.length - 1].value = value | ||
this.exit(token) | ||
} | ||
/** | ||
* @param {string} $0 | ||
* @param {string} $1 | ||
* @returns {string} | ||
*/ | ||
function replace($0, $1) { | ||
// Pipes work, backslashes don’t (but can’t escape pipes). | ||
return $1 === '|' ? $1 : $0 | ||
} | ||
/** | ||
* @param {Options} [options] | ||
* @returns {ToMarkdownExtension} | ||
*/ | ||
export function gfmTableToMarkdown(options) { | ||
const settings = options || {} | ||
const padding = settings.tableCellPadding | ||
const alignDelimiters = settings.tablePipeAlign | ||
const stringLength = settings.stringLength | ||
const around = padding ? ' ' : '|' | ||
return { | ||
unsafe: [ | ||
{character: '\r', inConstruct: 'tableCell'}, | ||
{character: '\n', inConstruct: 'tableCell'}, | ||
// A pipe, when followed by a tab or space (padding), or a dash or colon | ||
// (unpadded delimiter row), could result in a table. | ||
{atBreak: true, character: '|', after: '[\t :-]'}, | ||
// A pipe in a cell must be encoded. | ||
{character: '|', inConstruct: 'tableCell'}, | ||
// A colon must be followed by a dash, in which case it could start a | ||
// delimiter row. | ||
{atBreak: true, character: ':', after: '-'}, | ||
// A delimiter row can also start with a dash, when followed by more | ||
// dashes, a colon, or a pipe. | ||
// This is a stricter version than the built in check for lists, thematic | ||
// breaks, and setex heading underlines though: | ||
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/51a2038/lib/unsafe.js#L57> | ||
{atBreak: true, character: '-', after: '[:|-]'} | ||
], | ||
handlers: { | ||
table: handleTable, | ||
tableRow: handleTableRow, | ||
tableCell: handleTableCell, | ||
inlineCode: inlineCodeWithTable | ||
} | ||
} | ||
/** | ||
* @type {ToMarkdownHandle} | ||
* @param {Table} node | ||
*/ | ||
function handleTable(node, _, context) { | ||
// @ts-expect-error: fixed in `markdown-table@3.0.1`. | ||
return serializeData(handleTableAsData(node, context), node.align) | ||
} | ||
/** | ||
* This function isn’t really used normally, because we handle rows at the | ||
* table level. | ||
* But, if someone passes in a table row, this ensures we make somewhat sense. | ||
* | ||
* @type {ToMarkdownHandle} | ||
* @param {TableRow} node | ||
*/ | ||
function handleTableRow(node, _, context) { | ||
const row = handleTableRowAsData(node, context) | ||
// `markdown-table` will always add an align row | ||
const value = serializeData([row]) | ||
return value.slice(0, value.indexOf('\n')) | ||
} | ||
/** | ||
* @type {ToMarkdownHandle} | ||
* @param {TableCell} node | ||
*/ | ||
function handleTableCell(node, _, context) { | ||
const exit = context.enter('tableCell') | ||
const value = containerPhrasing(node, context, { | ||
before: around, | ||
after: around | ||
}) | ||
exit() | ||
return value | ||
} | ||
/** | ||
* @param {Array.<Array.<string>>} matrix | ||
* @param {Array.<string>} [align] | ||
*/ | ||
function serializeData(matrix, align) { | ||
return markdownTable(matrix, { | ||
align, | ||
alignDelimiters, | ||
padding, | ||
stringLength | ||
}) | ||
} | ||
/** | ||
* @param {Table} node | ||
* @param {ToMarkdownContext} context | ||
*/ | ||
function handleTableAsData(node, context) { | ||
const children = node.children | ||
let index = -1 | ||
/** @type {Array.<Array.<string>>} */ | ||
const result = [] | ||
const subexit = context.enter('table') | ||
while (++index < children.length) { | ||
result[index] = handleTableRowAsData(children[index], context) | ||
} | ||
subexit() | ||
return result | ||
} | ||
/** | ||
* @param {TableRow} node | ||
* @param {ToMarkdownContext} context | ||
*/ | ||
function handleTableRowAsData(node, context) { | ||
const children = node.children | ||
let index = -1 | ||
/** @type {Array.<string>} */ | ||
const result = [] | ||
const subexit = context.enter('tableRow') | ||
while (++index < children.length) { | ||
result[index] = handleTableCell(children[index], node, context) | ||
} | ||
subexit() | ||
return result | ||
} | ||
/** | ||
* @type {ToMarkdownHandle} | ||
* @param {InlineCode} node | ||
*/ | ||
function inlineCodeWithTable(node, parent, context) { | ||
let value = inlineCode(node, parent, context) | ||
if (context.stack.includes('tableCell')) { | ||
value = value.replace(/\|/g, '\\$&') | ||
} | ||
return value | ||
} | ||
} |
{ | ||
"name": "mdast-util-gfm-table", | ||
"version": "0.1.6", | ||
"version": "1.0.0", | ||
"description": "mdast extension to parse and serialize GFM tables", | ||
@@ -31,35 +31,37 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"from-markdown.js", | ||
"index.js", | ||
"to-markdown.js" | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"markdown-table": "^2.0.0", | ||
"mdast-util-to-markdown": "~0.6.0" | ||
"markdown-table": "^3.0.0", | ||
"mdast-util-to-markdown": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"mdast-util-from-markdown": "^0.8.0", | ||
"micromark-extension-gfm-table": "^0.4.0", | ||
"nyc": "^15.0.0", | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"mdast-util-from-markdown": "^1.0.0", | ||
"micromark-extension-gfm-table": "^1.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"string-width": "^4.0.0", | ||
"rimraf": "^3.0.0", | ||
"string-width": "^5.0.0", | ||
"tape": "^5.0.0", | ||
"unist-util-remove-position": "^3.0.0", | ||
"xo": "^0.37.0" | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-util-remove-position": "^4.0.0", | ||
"xo": "^0.39.0" | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run format && npm run test-coverage" | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -74,7 +76,3 @@ "tabWidth": 2, | ||
"xo": { | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"unicorn/prefer-includes": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -85,3 +83,9 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
@@ -17,7 +17,12 @@ # mdast-util-gfm-table | ||
You probably shouldn’t use this package directly, but instead use | ||
[`remark-gfm`][remark-gfm] with **[remark][]**. | ||
## When to use this | ||
Use [`mdast-util-gfm`][mdast-util-gfm] if you want all of GFM. | ||
Use this otherwise. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
[npm][]: | ||
@@ -40,16 +45,16 @@ | ||
And our script, `example.js`, looks as follows: | ||
And our module, `example.js`, looks as follows: | ||
```js | ||
var fs = require('fs') | ||
var fromMarkdown = require('mdast-util-from-markdown') | ||
var toMarkdown = require('mdast-util-to-markdown') | ||
var syntax = require('micromark-extension-gfm-table') | ||
var table = require('mdast-util-gfm-table') | ||
import fs from 'node:fs' | ||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
import {toMarkdown} from 'mdast-util-to-markdown' | ||
import {gfmTable} from 'micromark-extension-gfm-table' | ||
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'mdast-util-gfm-table' | ||
var doc = fs.readFileSync('example.md') | ||
const doc = fs.readFileSync('example.md') | ||
var tree = fromMarkdown(doc, { | ||
extensions: [syntax], | ||
mdastExtensions: [table.fromMarkdown] | ||
const tree = fromMarkdown(doc, { | ||
extensions: [gfmTable], | ||
mdastExtensions: [gfmTableFromMarkdown] | ||
}) | ||
@@ -59,3 +64,3 @@ | ||
var out = toMarkdown(tree, {extensions: [table.toMarkdown()]}) | ||
const out = toMarkdown(tree, {extensions: [gfmTableToMarkdown()]}) | ||
@@ -117,11 +122,11 @@ console.log(out) | ||
### `table.fromMarkdown` | ||
This package exports the following identifier: `gfmTableFromMarkdown`, | ||
`gfmTableToMarkdown`. | ||
There is no default export. | ||
### `table.toMarkdown(options?)` | ||
### `gfmTableFromMarkdown` | ||
> Note: the separate extensions are also available at | ||
> `mdast-util-gfm-table/from-markdown` and | ||
> `mdast-util-gfm-table/to-markdown`. | ||
### `gfmTableToMarkdown(options?)` | ||
Support tables. | ||
Support GFM tables. | ||
The exports of `fromMarkdown` is an extension for | ||
@@ -236,1 +241,3 @@ [`mdast-util-from-markdown`][from-markdown]. | ||
[string-length]: https://github.com/wooorm/markdown-table#optionsstringlength | ||
[mdast-util-gfm]: https://github.com/syntax-tree/mdast-util-gfm |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17758
230
0
239
Yes
14
5
1
+ Added@types/mdast@3.0.15(transitive)
+ Addedcharacter-entities@2.0.2(transitive)
+ Addeddecode-named-character-reference@1.0.2(transitive)
+ Addedlongest-streak@3.1.0(transitive)
+ Addedmarkdown-table@3.0.3(transitive)
+ Addedmdast-util-phrasing@3.0.1(transitive)
+ Addedmdast-util-to-markdown@1.5.0(transitive)
+ Addedmdast-util-to-string@3.2.0(transitive)
+ Addedmicromark-util-character@1.2.0(transitive)
+ Addedmicromark-util-decode-numeric-character-reference@1.1.0(transitive)
+ Addedmicromark-util-decode-string@1.1.0(transitive)
+ Addedmicromark-util-symbol@1.1.0(transitive)
+ Addedmicromark-util-types@1.1.0(transitive)
+ Addedunist-util-is@5.2.1(transitive)
+ Addedunist-util-visit@4.1.2(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
+ Addedzwitch@2.0.4(transitive)
- Removedcharacter-entities@1.2.4(transitive)
- Removedcharacter-entities-legacy@1.1.4(transitive)
- Removedcharacter-reference-invalid@1.1.4(transitive)
- Removedis-alphabetical@1.0.4(transitive)
- Removedis-alphanumerical@1.0.4(transitive)
- Removedis-decimal@1.0.4(transitive)
- Removedis-hexadecimal@1.0.4(transitive)
- Removedlongest-streak@2.0.4(transitive)
- Removedmarkdown-table@2.0.0(transitive)
- Removedmdast-util-to-markdown@0.6.5(transitive)
- Removedmdast-util-to-string@2.0.0(transitive)
- Removedparse-entities@2.0.0(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedzwitch@1.0.5(transitive)
Updatedmarkdown-table@^3.0.0