Socket
Socket
Sign inDemoInstall

mdast-util-gfm-task-list-item

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdast-util-gfm-task-list-item - npm Package Compare versions

Comparing version 0.1.6 to 1.0.0

index.d.ts

107

index.js

@@ -1,2 +0,105 @@

exports.fromMarkdown = require('./from-markdown')
exports.toMarkdown = require('./to-markdown')
/**
* @typedef {import('mdast').ListItem} ListItem
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('mdast').BlockContent} BlockContent
* @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
*/
import {listItem} from 'mdast-util-to-markdown/lib/handle/list-item.js'
/** @type {FromMarkdownExtension} */
export const gfmTaskListItemFromMarkdown = {
exit: {
taskListCheckValueChecked: exitCheck,
taskListCheckValueUnchecked: exitCheck,
paragraph: exitParagraphWithTaskListItem
}
}
/** @type {ToMarkdownExtension} */
export const gfmTaskListItemToMarkdown = {
unsafe: [{atBreak: true, character: '-', after: '[:|-]'}],
handlers: {listItem: listItemWithTaskListItem}
}
/** @type {FromMarkdownHandle} */
function exitCheck(token) {
// We’re always in a paragraph, in a list item.
this.stack[this.stack.length - 2].checked =
token.type === 'taskListCheckValueChecked'
}
/** @type {FromMarkdownHandle} */
function exitParagraphWithTaskListItem(token) {
const parent = this.stack[this.stack.length - 2]
/** @type {Paragraph} */
// @ts-expect-error: must be true.
const node = this.stack[this.stack.length - 1]
/** @type {BlockContent[]} */
// @ts-expect-error: check whether `parent` is a `listItem` later.
const siblings = parent.children
const head = node.children[0]
let index = -1
/** @type {Paragraph|undefined} */
let firstParaghraph
if (
parent &&
parent.type === 'listItem' &&
typeof parent.checked === 'boolean' &&
head &&
head.type === 'text'
) {
while (++index < siblings.length) {
const sibling = siblings[index]
if (sibling.type === 'paragraph') {
firstParaghraph = sibling
break
}
}
if (firstParaghraph === node) {
// Must start with a space or a tab.
head.value = head.value.slice(1)
if (head.value.length === 0) {
node.children.shift()
} else {
// @ts-expect-error: must be true.
head.position.start.column++
// @ts-expect-error: must be true.
head.position.start.offset++
// @ts-expect-error: must be true.
node.position.start = Object.assign({}, head.position.start)
}
}
}
this.exit(token)
}
/**
* @type {ToMarkdownHandle}
* @param {ListItem} node
*/
function listItemWithTaskListItem(node, parent, context) {
const head = node.children[0]
let value = listItem(node, parent, context)
if (typeof node.checked === 'boolean' && head && head.type === 'paragraph') {
value = value.replace(/^(?:[*+-]|\d+\.)([\r\n]| {1,3})/, check)
}
return value
/**
* @param {string} $0
* @returns {string}
*/
function check($0) {
return $0 + '[' + (node.checked ? 'x' : ' ') + '] '
}
}

55

package.json
{
"name": "mdast-util-gfm-task-list-item",
"version": "0.1.6",
"version": "1.0.0",
"description": "mdast extension to parse and serialize GFM task list items",

@@ -32,33 +32,36 @@ "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": {
"mdast-util-to-markdown": "~0.6.0"
"@types/mdast": "^3.0.3",
"mdast-util-to-markdown": "^1.0.0"
},
"devDependencies": {
"mdast-util-from-markdown": "^0.8.0",
"micromark-extension-gfm-task-list-item": "^0.3.0",
"nyc": "^15.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"mdast-util-from-markdown": "^1.0.0",
"micromark-extension-gfm-task-list-item": "^1.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0-alpha.1",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"unist-util-remove-position": "^3.0.0",
"xo": "^0.36.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": {

@@ -73,7 +76,3 @@ "tabWidth": 2,

"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/prefer-includes": "off"
}
"prettier": true
},

@@ -84,3 +83,9 @@ "remarkConfig": {

]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}

@@ -17,7 +17,14 @@ # mdast-util-gfm-task-list-item

You probably shouldn’t use this package directly, but instead use
[`remark-gfm`][remark-gfm] with **[remark][]**.
## When to use this
Use this if you’re dealing with the AST manually.
It’s might be better to use [`remark-gfm`][remark-gfm] with **[remark][]**,
which includes this but provides a nicer interface and makes it easier to
combine with hundreds of plugins.
## 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][]:

@@ -41,16 +48,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-task-list-item')
var taskListItem = require('mdast-util-gfm-task-list-item')
import fs from 'node:fs'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {gfmTaskListItem} from 'micromark-extension-gfm-task-list-item'
import {gfmTaskListItemFromMarkdown, gfmTaskListItemToMarkdown} from 'mdast-util-gfm-task-list-item'
var doc = fs.readFileSync('example.md')
const doc = fs.readFileSync('example.md')
var tree = fromMarkdown(doc, {
extensions: [syntax],
mdastExtensions: [taskListItem.fromMarkdown]
const tree = fromMarkdown(doc, {
extensions: [gfmTaskListItem],
mdastExtensions: [gfmTaskListItemFromMarkdown]
})

@@ -60,3 +67,3 @@

var out = toMarkdown(tree, {extensions: [taskListItem.toMarkdown]})
const out = toMarkdown(tree, {extensions: [gfmTaskListItemToMarkdown]})

@@ -135,9 +142,9 @@ console.log(out)

### `taskListItem.fromMarkdown`
This package exports the following identifier: `gfmTaskListItemFromMarkdown`,
`gfmTaskListItemToMarkdown`.
There is no default export.
### `taskListItem.toMarkdown`
### `gfmTaskListItemFromMarkdown`
> Note: the separate extensions are also available at
> `mdast-util-gfm-task-list-item/from-markdown` and
> `mdast-util-gfm-task-list-item/to-markdown`.
### `gfmTaskListItemToMarkdown`

@@ -144,0 +151,0 @@ Support task list items.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc