Skip to content

Commit f3c0c3a

Browse files
committed
Refactor code-style
* Add more docs to JSDoc
1 parent d2c14eb commit f3c0c3a

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

lib/index.js

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
/**
2-
* @typedef {Extract<import('mdast').Root|import('mdast').Content, import('unist').Parent>} Parent
2+
* @typedef {import('mdast').Content} Content
33
* @typedef {import('mdast').ListItem} ListItem
44
* @typedef {import('mdast').Paragraph} Paragraph
5-
* @typedef {import('mdast').BlockContent} BlockContent
5+
* @typedef {import('mdast').Parent} Parent
6+
* @typedef {import('mdast').Root} Root
67
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
78
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
89
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
910
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
1011
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
1112
*/
1213

14+
/**
15+
* @typedef {Extract<Root | Content, Parent>} Parents
16+
*/
17+
1318
import {listItem} from 'mdast-util-to-markdown/lib/handle/list-item.js'
1419
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
1520

16-
/** @type {FromMarkdownExtension} */
21+
// To do: next major: rename `context` -> `state`, `safeOptions` -> `info`, use
22+
// `track` from `state`.
23+
// To do: next major: replace exports with functions.
24+
// To do: next major: use `defaulthandlers.listItem`.
25+
26+
/**
27+
* Extension for `mdast-util-from-markdown` to enable GFM task list items.
28+
*
29+
* @type {FromMarkdownExtension}
30+
*/
1731
export const gfmTaskListItemFromMarkdown = {
1832
exit: {
1933
taskListCheckValueChecked: exitCheck,
@@ -22,7 +36,11 @@ export const gfmTaskListItemFromMarkdown = {
2236
}
2337
}
2438

25-
/** @type {ToMarkdownExtension} */
39+
/**
40+
* Extension for `mdast-util-to-markdown` to enable GFM task list items.
41+
*
42+
* @type {ToMarkdownExtension}
43+
*/
2644
export const gfmTaskListItemToMarkdown = {
2745
unsafe: [{atBreak: true, character: '-', after: '[:|-]'}],
2846
handlers: {listItem: listItemWithTaskListItem}
@@ -43,43 +61,45 @@ function exitCheck(token) {
4361
* @type {FromMarkdownHandle}
4462
*/
4563
function exitParagraphWithTaskListItem(token) {
46-
const parent = /** @type {Parent} */ (this.stack[this.stack.length - 2])
47-
const node = /** @type {Paragraph} */ (this.stack[this.stack.length - 1])
48-
const siblings = parent.children
49-
const head = node.children[0]
50-
let index = -1
51-
/** @type {Paragraph|undefined} */
52-
let firstParaghraph
64+
const parent = /** @type {Parents} */ (this.stack[this.stack.length - 2])
5365

5466
if (
5567
parent &&
5668
parent.type === 'listItem' &&
57-
typeof parent.checked === 'boolean' &&
58-
head &&
59-
head.type === 'text'
69+
typeof parent.checked === 'boolean'
6070
) {
61-
while (++index < siblings.length) {
62-
const sibling = siblings[index]
63-
if (sibling.type === 'paragraph') {
64-
firstParaghraph = sibling
65-
break
71+
const node = /** @type {Paragraph} */ (this.stack[this.stack.length - 1])
72+
const head = node.children[0]
73+
74+
if (head && head.type === 'text') {
75+
const siblings = parent.children
76+
let index = -1
77+
/** @type {Paragraph | undefined} */
78+
let firstParaghraph
79+
80+
while (++index < siblings.length) {
81+
const sibling = siblings[index]
82+
if (sibling.type === 'paragraph') {
83+
firstParaghraph = sibling
84+
break
85+
}
6686
}
67-
}
6887

69-
if (firstParaghraph === node) {
70-
// Must start with a space or a tab.
71-
head.value = head.value.slice(1)
72-
73-
if (head.value.length === 0) {
74-
node.children.shift()
75-
} else if (
76-
node.position &&
77-
head.position &&
78-
typeof head.position.start.offset === 'number'
79-
) {
80-
head.position.start.column++
81-
head.position.start.offset++
82-
node.position.start = Object.assign({}, head.position.start)
88+
if (firstParaghraph === node) {
89+
// Must start with a space or a tab.
90+
head.value = head.value.slice(1)
91+
92+
if (head.value.length === 0) {
93+
node.children.shift()
94+
} else if (
95+
node.position &&
96+
head.position &&
97+
typeof head.position.start.offset === 'number'
98+
) {
99+
head.position.start.column++
100+
head.position.start.offset++
101+
node.position.start = Object.assign({}, head.position.start)
102+
}
83103
}
84104
}
85105
}

0 commit comments

Comments
 (0)