|
1 |
| -/** |
2 |
| - * @typedef {Extract<import('mdast').Root|import('mdast').Content, import('unist').Parent>} Parent |
3 |
| - * @typedef {import('mdast').ListItem} ListItem |
4 |
| - * @typedef {import('mdast').Paragraph} Paragraph |
5 |
| - * @typedef {import('mdast').BlockContent} BlockContent |
6 |
| - * @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext |
7 |
| - * @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension |
8 |
| - * @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle |
9 |
| - * @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension |
10 |
| - * @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle |
11 |
| - */ |
12 |
| - |
13 |
| -import {listItem} from 'mdast-util-to-markdown/lib/handle/list-item.js' |
14 |
| -import {track} from 'mdast-util-to-markdown/lib/util/track.js' |
15 |
| - |
16 |
| -/** @type {FromMarkdownExtension} */ |
17 |
| -export const gfmTaskListItemFromMarkdown = { |
18 |
| - exit: { |
19 |
| - taskListCheckValueChecked: exitCheck, |
20 |
| - taskListCheckValueUnchecked: exitCheck, |
21 |
| - paragraph: exitParagraphWithTaskListItem |
22 |
| - } |
23 |
| -} |
24 |
| - |
25 |
| -/** @type {ToMarkdownExtension} */ |
26 |
| -export const gfmTaskListItemToMarkdown = { |
27 |
| - unsafe: [{atBreak: true, character: '-', after: '[:|-]'}], |
28 |
| - handlers: {listItem: listItemWithTaskListItem} |
29 |
| -} |
30 |
| - |
31 |
| -/** |
32 |
| - * @this {CompileContext} |
33 |
| - * @type {FromMarkdownHandle} |
34 |
| - */ |
35 |
| -function exitCheck(token) { |
36 |
| - const node = /** @type {ListItem} */ (this.stack[this.stack.length - 2]) |
37 |
| - // We’re always in a paragraph, in a list item. |
38 |
| - node.checked = token.type === 'taskListCheckValueChecked' |
39 |
| -} |
40 |
| - |
41 |
| -/** |
42 |
| - * @this {CompileContext} |
43 |
| - * @type {FromMarkdownHandle} |
44 |
| - */ |
45 |
| -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 |
53 |
| - |
54 |
| - if ( |
55 |
| - parent && |
56 |
| - parent.type === 'listItem' && |
57 |
| - typeof parent.checked === 'boolean' && |
58 |
| - head && |
59 |
| - head.type === 'text' |
60 |
| - ) { |
61 |
| - while (++index < siblings.length) { |
62 |
| - const sibling = siblings[index] |
63 |
| - if (sibling.type === 'paragraph') { |
64 |
| - firstParaghraph = sibling |
65 |
| - break |
66 |
| - } |
67 |
| - } |
68 |
| - |
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) |
83 |
| - } |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - this.exit(token) |
88 |
| -} |
89 |
| - |
90 |
| -/** |
91 |
| - * @type {ToMarkdownHandle} |
92 |
| - * @param {ListItem} node |
93 |
| - */ |
94 |
| -function listItemWithTaskListItem(node, parent, context, safeOptions) { |
95 |
| - const head = node.children[0] |
96 |
| - const checkable = |
97 |
| - typeof node.checked === 'boolean' && head && head.type === 'paragraph' |
98 |
| - const checkbox = '[' + (node.checked ? 'x' : ' ') + '] ' |
99 |
| - const tracker = track(safeOptions) |
100 |
| - |
101 |
| - if (checkable) { |
102 |
| - tracker.move(checkbox) |
103 |
| - } |
104 |
| - |
105 |
| - let value = listItem(node, parent, context, { |
106 |
| - ...safeOptions, |
107 |
| - ...tracker.current() |
108 |
| - }) |
109 |
| - |
110 |
| - if (checkable) { |
111 |
| - value = value.replace(/^(?:[*+-]|\d+\.)([\r\n]| {1,3})/, check) |
112 |
| - } |
113 |
| - |
114 |
| - return value |
115 |
| - |
116 |
| - /** |
117 |
| - * @param {string} $0 |
118 |
| - * @returns {string} |
119 |
| - */ |
120 |
| - function check($0) { |
121 |
| - return $0 + checkbox |
122 |
| - } |
123 |
| -} |
| 1 | +export { |
| 2 | + gfmTaskListItemFromMarkdown, |
| 3 | + gfmTaskListItemToMarkdown |
| 4 | +} from './lib/index.js' |
0 commit comments