@milkdown/preset-gfm 7.5.0 → 7.5.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/lib/__internal__/with-meta.d.ts.map +1 -1
  2. package/lib/composed/commands.d.ts +7 -7
  3. package/lib/composed/commands.d.ts.map +1 -1
  4. package/lib/composed/inputrules.d.ts.map +1 -1
  5. package/lib/index.d.ts.map +1 -1
  6. package/lib/index.es.js +407 -323
  7. package/lib/index.es.js.map +1 -1
  8. package/lib/mark/strike-through.d.ts.map +1 -1
  9. package/lib/node/footnote/definition.d.ts.map +1 -1
  10. package/lib/node/footnote/reference.d.ts.map +1 -1
  11. package/lib/node/table/command.d.ts +11 -11
  12. package/lib/node/table/command.d.ts.map +1 -1
  13. package/lib/node/table/input.d.ts.map +1 -1
  14. package/lib/node/table/schema.d.ts.map +1 -1
  15. package/lib/node/table/utils.d.ts.map +1 -1
  16. package/lib/node/task-list-item.d.ts.map +1 -1
  17. package/lib/plugin/keep-table-align-plugin.d.ts.map +1 -1
  18. package/lib/plugin/remark-gfm-plugin.d.ts.map +1 -1
  19. package/lib/plugin/table-editing-plugin.d.ts.map +1 -1
  20. package/package.json +8 -8
  21. package/src/__internal__/with-meta.ts +4 -1
  22. package/src/composed/commands.ts +17 -1
  23. package/src/composed/inputrules.ts +1 -3
  24. package/src/index.ts +16 -2
  25. package/src/mark/strike-through.ts +21 -9
  26. package/src/node/footnote/definition.ts +56 -54
  27. package/src/node/footnote/reference.ts +47 -45
  28. package/src/node/table/command.ts +169 -93
  29. package/src/node/table/input.ts +32 -15
  30. package/src/node/table/schema.ts +12 -13
  31. package/src/node/table/utils.ts +120 -84
  32. package/src/node/task-list-item.ts +100 -88
  33. package/src/plugin/keep-table-align-plugin.ts +7 -14
  34. package/src/plugin/remark-gfm-plugin.ts +2 -1
  35. package/src/plugin/table-editing-plugin.ts +3 -1
@@ -6,7 +6,13 @@ import type { TableRect } from '@milkdown/prose/tables'
6
6
  import { CellSelection, TableMap } from '@milkdown/prose/tables'
7
7
 
8
8
  import type { Ctx } from '@milkdown/ctx'
9
- import { tableCellSchema, tableHeaderRowSchema, tableHeaderSchema, tableRowSchema, tableSchema } from './schema'
9
+ import {
10
+ tableCellSchema,
11
+ tableHeaderRowSchema,
12
+ tableHeaderSchema,
13
+ tableRowSchema,
14
+ tableSchema,
15
+ } from './schema'
10
16
 
11
17
  /// @internal
12
18
  export interface CellPos {
@@ -27,33 +33,42 @@ export function createTable(ctx: Ctx, rowsCount = 3, colsCount = 3): Node {
27
33
 
28
34
  const rows = Array(rowsCount)
29
35
  .fill(0)
30
- .map((_, i) => i === 0
31
- ? tableHeaderRowSchema.type(ctx).create(null, headerCells)
32
- : tableRowSchema.type(ctx).create(null, cells))
36
+ .map((_, i) =>
37
+ i === 0
38
+ ? tableHeaderRowSchema.type(ctx).create(null, headerCells)
39
+ : tableRowSchema.type(ctx).create(null, cells)
40
+ )
33
41
 
34
42
  return tableSchema.type(ctx).create(null, rows)
35
43
  }
36
44
 
37
45
  /// Find the table node with position information for target pos.
38
46
  export function findTable($pos: ResolvedPos) {
39
- return findParentNodeClosestToPos(node => node.type.spec.tableRole === 'table')($pos)
47
+ return findParentNodeClosestToPos(
48
+ (node) => node.type.spec.tableRole === 'table'
49
+ )($pos)
40
50
  }
41
51
 
42
52
  /// Get cells in a column of a table.
43
- export function getCellsInCol(columnIndex: number, selection: Selection): CellPos[] | undefined {
53
+ export function getCellsInCol(
54
+ columnIndex: number,
55
+ selection: Selection
56
+ ): CellPos[] | undefined {
44
57
  const table = findTable(selection.$from)
45
- if (!table)
46
- return undefined
58
+ if (!table) return undefined
47
59
  const map = TableMap.get(table.node)
48
- if (columnIndex < 0 || columnIndex >= map.width)
49
- return undefined
60
+ if (columnIndex < 0 || columnIndex >= map.width) return undefined
50
61
 
51
62
  return map
52
- .cellsInRect({ left: columnIndex, right: columnIndex + 1, top: 0, bottom: map.height })
63
+ .cellsInRect({
64
+ left: columnIndex,
65
+ right: columnIndex + 1,
66
+ top: 0,
67
+ bottom: map.height,
68
+ })
53
69
  .map((pos) => {
54
70
  const node = table.node.nodeAt(pos)
55
- if (!node)
56
- return undefined
71
+ if (!node) return undefined
57
72
  const start = pos + table.start
58
73
  return {
59
74
  pos: start,
@@ -65,20 +80,25 @@ export function getCellsInCol(columnIndex: number, selection: Selection): CellPo
65
80
  }
66
81
 
67
82
  /// Get cells in a row of a table.
68
- export function getCellsInRow(rowIndex: number, selection: Selection): CellPos[] | undefined {
83
+ export function getCellsInRow(
84
+ rowIndex: number,
85
+ selection: Selection
86
+ ): CellPos[] | undefined {
69
87
  const table = findTable(selection.$from)
70
- if (!table)
71
- return undefined
88
+ if (!table) return undefined
72
89
  const map = TableMap.get(table.node)
73
- if (rowIndex < 0 || rowIndex >= map.height)
74
- return undefined
90
+ if (rowIndex < 0 || rowIndex >= map.height) return undefined
75
91
 
76
92
  return map
77
- .cellsInRect({ left: 0, right: map.width, top: rowIndex, bottom: rowIndex + 1 })
93
+ .cellsInRect({
94
+ left: 0,
95
+ right: map.width,
96
+ top: rowIndex,
97
+ bottom: rowIndex + 1,
98
+ })
78
99
  .map((pos) => {
79
100
  const node = table.node.nodeAt(pos)
80
- if (!node)
81
- return undefined
101
+ if (!node) return undefined
82
102
  const start = pos + table.start
83
103
  return {
84
104
  pos: start,
@@ -92,8 +112,7 @@ export function getCellsInRow(rowIndex: number, selection: Selection): CellPos[]
92
112
  /// Get all cells in a table.
93
113
  export function getAllCellsInTable(selection: Selection) {
94
114
  const table = findTable(selection.$from)
95
- if (!table)
96
- return
115
+ if (!table) return
97
116
 
98
117
  const map = TableMap.get(table.node)
99
118
  const cells = map.cellsInRect({
@@ -124,7 +143,12 @@ export function selectTable(tr: Transaction) {
124
143
  }
125
144
 
126
145
  /// @internal
127
- export function addRowWithAlignment(ctx: Ctx, tr: Transaction, { map, tableStart, table }: TableRect, row: number) {
146
+ export function addRowWithAlignment(
147
+ ctx: Ctx,
148
+ tr: Transaction,
149
+ { map, tableStart, table }: TableRect,
150
+ row: number
151
+ ) {
128
152
  const rowPos = Array(row)
129
153
  .fill(0)
130
154
  .reduce((acc, _, i) => {
@@ -135,7 +159,9 @@ export function addRowWithAlignment(ctx: Ctx, tr: Transaction, { map, tableStart
135
159
  .fill(0)
136
160
  .map((_, col) => {
137
161
  const headerCol = table.nodeAt(map.map[col] as number)
138
- return tableCellSchema.type(ctx).createAndFill({ alignment: headerCol?.attrs.alignment }) as Node
162
+ return tableCellSchema
163
+ .type(ctx)
164
+ .createAndFill({ alignment: headerCol?.attrs.alignment }) as Node
139
165
  })
140
166
 
141
167
  tr.insert(rowPos, tableRowSchema.type(ctx).create(null, cells))
@@ -147,7 +173,9 @@ export function selectLine(type: 'row' | 'col') {
147
173
  return (index: number, pos?: number) => (tr: Transaction) => {
148
174
  pos = pos ?? tr.selection.from
149
175
  const $pos = tr.doc.resolve(pos)
150
- const $node = findParentNodeClosestToPos(node => node.type.name === 'table')($pos)
176
+ const $node = findParentNodeClosestToPos(
177
+ (node) => node.type.name === 'table'
178
+ )($pos)
151
179
  const table = $node
152
180
  ? {
153
181
  node: $node.node,
@@ -164,15 +192,25 @@ export function selectLine(type: 'row' | 'col') {
164
192
  const lastCell = map.positionAt(
165
193
  isRowSelection ? index : map.height - 1,
166
194
  isRowSelection ? map.width - 1 : index,
167
- table.node,
195
+ table.node
168
196
  )
169
197
  const $lastCell = tr.doc.resolve(table.from + lastCell)
170
198
 
171
- const createCellSelection = isRowSelection ? CellSelection.rowSelection : CellSelection.colSelection
199
+ const createCellSelection = isRowSelection
200
+ ? CellSelection.rowSelection
201
+ : CellSelection.colSelection
172
202
 
173
- const firstCell = map.positionAt(isRowSelection ? index : 0, isRowSelection ? 0 : index, table.node)
203
+ const firstCell = map.positionAt(
204
+ isRowSelection ? index : 0,
205
+ isRowSelection ? 0 : index,
206
+ table.node
207
+ )
174
208
  const $firstCell = tr.doc.resolve(table.from + firstCell)
175
- return cloneTr(tr.setSelection(createCellSelection($lastCell, $firstCell) as unknown as Selection))
209
+ return cloneTr(
210
+ tr.setSelection(
211
+ createCellSelection($lastCell, $firstCell) as unknown as Selection
212
+ )
213
+ )
176
214
  }
177
215
  }
178
216
  return tr
@@ -189,11 +227,14 @@ export const selectCol = selectLine('col')
189
227
 
190
228
  function transpose<T>(array: T[][]) {
191
229
  return array[0]!.map((_, i) => {
192
- return array.map(column => column[i])
230
+ return array.map((column) => column[i])
193
231
  }) as T[][]
194
232
  }
195
233
 
196
- function convertArrayOfRowsToTableNode(tableNode: Node, arrayOfNodes: (Node | null)[][]) {
234
+ function convertArrayOfRowsToTableNode(
235
+ tableNode: Node,
236
+ arrayOfNodes: (Node | null)[][]
237
+ ) {
197
238
  const rowsPM = []
198
239
  const map = TableMap.get(tableNode)
199
240
  for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
@@ -201,8 +242,7 @@ function convertArrayOfRowsToTableNode(tableNode: Node, arrayOfNodes: (Node | nu
201
242
  const rowCells = []
202
243
 
203
244
  for (let colIndex = 0; colIndex < map.width; colIndex++) {
204
- if (!arrayOfNodes[rowIndex]![colIndex])
205
- continue
245
+ if (!arrayOfNodes[rowIndex]![colIndex]) continue
206
246
 
207
247
  const cellPos = map.map[rowIndex * map.width + colIndex]!
208
248
 
@@ -211,7 +251,7 @@ function convertArrayOfRowsToTableNode(tableNode: Node, arrayOfNodes: (Node | nu
211
251
  const newCell = oldCell.type.createChecked(
212
252
  Object.assign({}, cell.attrs),
213
253
  cell.content,
214
- cell.marks,
254
+ cell.marks
215
255
  )
216
256
  rowCells.push(newCell)
217
257
  }
@@ -222,7 +262,7 @@ function convertArrayOfRowsToTableNode(tableNode: Node, arrayOfNodes: (Node | nu
222
262
  const newTable = tableNode.type.createChecked(
223
263
  tableNode.attrs,
224
264
  rowsPM,
225
- tableNode.marks,
265
+ tableNode.marks
226
266
  )
227
267
 
228
268
  return newTable
@@ -254,7 +294,12 @@ function convertTableNodeToArrayOfRows(tableNode: Node) {
254
294
  return rows
255
295
  }
256
296
 
257
- function moveRowInArrayOfRows(rows: (Node | null)[][], indexesOrigin: number[], indexesTarget: number[], directionOverride: -1 | 1 | 0) {
297
+ function moveRowInArrayOfRows(
298
+ rows: (Node | null)[][],
299
+ indexesOrigin: number[],
300
+ indexesTarget: number[],
301
+ directionOverride: -1 | 1 | 0
302
+ ) {
258
303
  const direction = indexesOrigin[0]! > indexesTarget[0]! ? -1 : 1
259
304
 
260
305
  const rowsExtracted = rows.splice(indexesOrigin[0]!, indexesOrigin.length)
@@ -263,13 +308,11 @@ function moveRowInArrayOfRows(rows: (Node | null)[][], indexesOrigin: number[],
263
308
 
264
309
  if (directionOverride === -1 && direction === 1) {
265
310
  target = indexesTarget[0]! - 1
266
- }
267
- else if (directionOverride === 1 && direction === -1) {
311
+ } else if (directionOverride === 1 && direction === -1) {
268
312
  target = indexesTarget[indexesTarget.length - 1]! - positionOffset + 1
269
- }
270
- else {
271
- target
272
- = direction === -1
313
+ } else {
314
+ target =
315
+ direction === -1
273
316
  ? indexesTarget[0]!
274
317
  : indexesTarget[indexesTarget.length - 1]! - positionOffset
275
318
  }
@@ -278,7 +321,12 @@ function moveRowInArrayOfRows(rows: (Node | null)[][], indexesOrigin: number[],
278
321
  return rows
279
322
  }
280
323
 
281
- function moveTableColumn(table: ContentNodeWithPos, indexesOrigin: number[], indexesTarget: number[], direction: -1 | 1 | 0) {
324
+ function moveTableColumn(
325
+ table: ContentNodeWithPos,
326
+ indexesOrigin: number[],
327
+ indexesTarget: number[],
328
+ direction: -1 | 1 | 0
329
+ ) {
282
330
  let rows = transpose(convertTableNodeToArrayOfRows(table.node))
283
331
 
284
332
  rows = moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget, direction)
@@ -287,7 +335,12 @@ function moveTableColumn(table: ContentNodeWithPos, indexesOrigin: number[], ind
287
335
  return convertArrayOfRowsToTableNode(table.node, rows)
288
336
  }
289
337
 
290
- function moveTableRow(table: ContentNodeWithPos, indexesOrigin: number[], indexesTarget: number[], direction: -1 | 1 | 0) {
338
+ function moveTableRow(
339
+ table: ContentNodeWithPos,
340
+ indexesOrigin: number[],
341
+ indexesTarget: number[],
342
+ direction: -1 | 1 | 0
343
+ ) {
291
344
  let rows = convertTableNodeToArrayOfRows(table.node)
292
345
 
293
346
  rows = moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget, direction)
@@ -305,11 +358,9 @@ function getSelectionRangeInColumn(columnIndex: number, tr: Transaction) {
305
358
  if (cells) {
306
359
  cells.forEach((cell) => {
307
360
  const maybeEndIndex = cell.node.attrs.colspan + i - 1
308
- if (maybeEndIndex >= startIndex)
309
- startIndex = i
361
+ if (maybeEndIndex >= startIndex) startIndex = i
310
362
 
311
- if (maybeEndIndex > endIndex)
312
- endIndex = maybeEndIndex
363
+ if (maybeEndIndex > endIndex) endIndex = maybeEndIndex
313
364
  })
314
365
  }
315
366
  }
@@ -329,8 +380,7 @@ function getSelectionRangeInColumn(columnIndex: number, tr: Transaction) {
329
380
  const indexes = []
330
381
  for (let i = startIndex; i <= endIndex; i++) {
331
382
  const maybeCells = getCellsInCol(i, tr.selection)
332
- if (maybeCells && maybeCells.length)
333
- indexes.push(i)
383
+ if (maybeCells && maybeCells.length) indexes.push(i)
334
384
  }
335
385
  startIndex = indexes[0]!
336
386
  endIndex = indexes[indexes.length - 1]!
@@ -338,7 +388,7 @@ function getSelectionRangeInColumn(columnIndex: number, tr: Transaction) {
338
388
  const firstSelectedColumnCells = getCellsInCol(startIndex, tr.selection)!
339
389
  const firstRowCells = getCellsInRow(0, tr.selection)!
340
390
  const $anchor = tr.doc.resolve(
341
- firstSelectedColumnCells[firstSelectedColumnCells.length - 1]!.pos,
391
+ firstSelectedColumnCells[firstSelectedColumnCells.length - 1]!.pos
342
392
  )
343
393
 
344
394
  let headCell: CellPos | undefined
@@ -351,8 +401,7 @@ function getSelectionRangeInColumn(columnIndex: number, tr: Transaction) {
351
401
  break
352
402
  }
353
403
  }
354
- if (headCell)
355
- break
404
+ if (headCell) break
356
405
  }
357
406
  }
358
407
 
@@ -368,11 +417,9 @@ function getSelectionRangeInRow(rowIndex: number, tr: Transaction) {
368
417
  const cells = getCellsInRow(i, tr.selection)
369
418
  cells!.forEach((cell) => {
370
419
  const maybeEndIndex = cell.node.attrs.rowspan + i - 1
371
- if (maybeEndIndex >= startIndex)
372
- startIndex = i
420
+ if (maybeEndIndex >= startIndex) startIndex = i
373
421
 
374
- if (maybeEndIndex > endIndex)
375
- endIndex = maybeEndIndex
422
+ if (maybeEndIndex > endIndex) endIndex = maybeEndIndex
376
423
  })
377
424
  }
378
425
  // looking for selection end row (endIndex)
@@ -389,15 +436,16 @@ function getSelectionRangeInRow(rowIndex: number, tr: Transaction) {
389
436
  const indexes = []
390
437
  for (let i = startIndex; i <= endIndex; i++) {
391
438
  const maybeCells = getCellsInRow(i, tr.selection)
392
- if (maybeCells && maybeCells.length)
393
- indexes.push(i)
439
+ if (maybeCells && maybeCells.length) indexes.push(i)
394
440
  }
395
441
  startIndex = indexes[0]!
396
442
  endIndex = indexes[indexes.length - 1]!
397
443
 
398
444
  const firstSelectedRowCells = getCellsInRow(startIndex, tr.selection)!
399
445
  const firstColumnCells = getCellsInCol(0, tr.selection)!
400
- const $anchor = tr.doc.resolve(firstSelectedRowCells[firstSelectedRowCells.length - 1]!.pos)
446
+ const $anchor = tr.doc.resolve(
447
+ firstSelectedRowCells[firstSelectedRowCells.length - 1]!.pos
448
+ )
401
449
 
402
450
  let headCell: CellPos | undefined
403
451
  for (let i = endIndex; i >= startIndex; i--) {
@@ -409,8 +457,7 @@ function getSelectionRangeInRow(rowIndex: number, tr: Transaction) {
409
457
  break
410
458
  }
411
459
  }
412
- if (headCell)
413
- break
460
+ if (headCell) break
414
461
  }
415
462
  }
416
463
 
@@ -433,30 +480,27 @@ export function moveCol(moveColParams: MoveColParams) {
433
480
  const { tr, origin, target, select = true, pos } = moveColParams
434
481
  const $pos = pos != null ? tr.doc.resolve(pos) : tr.selection.$from
435
482
  const table = findTable($pos)
436
- if (!table)
437
- return tr
483
+ if (!table) return tr
438
484
 
439
485
  const { indexes: indexesOriginColumn } = getSelectionRangeInColumn(origin, tr)
440
486
  const { indexes: indexesTargetColumn } = getSelectionRangeInColumn(target, tr)
441
487
 
442
- if (indexesOriginColumn.includes(target))
443
- return tr
488
+ if (indexesOriginColumn.includes(target)) return tr
444
489
 
445
490
  const newTable = moveTableColumn(
446
491
  table,
447
492
  indexesOriginColumn,
448
493
  indexesTargetColumn,
449
- 0,
494
+ 0
450
495
  )
451
496
 
452
497
  const _tr = cloneTr(tr).replaceWith(
453
498
  table.pos,
454
499
  table.pos + table.node.nodeSize,
455
- newTable,
500
+ newTable
456
501
  )
457
502
 
458
- if (!select)
459
- return _tr
503
+ if (!select) return _tr
460
504
 
461
505
  const map = TableMap.get(newTable)
462
506
  const start = table.start
@@ -487,30 +531,22 @@ export function moveRow(moveRowParams: MoveRowParams) {
487
531
  const { tr, origin, target, select = true, pos } = moveRowParams
488
532
  const $pos = pos != null ? tr.doc.resolve(pos) : tr.selection.$from
489
533
  const table = findTable($pos)
490
- if (!table)
491
- return tr
534
+ if (!table) return tr
492
535
 
493
536
  const { indexes: indexesOriginRow } = getSelectionRangeInRow(origin, tr)
494
537
  const { indexes: indexesTargetRow } = getSelectionRangeInRow(target, tr)
495
538
 
496
- if (indexesOriginRow.includes(target))
497
- return tr
539
+ if (indexesOriginRow.includes(target)) return tr
498
540
 
499
- const newTable = moveTableRow(
500
- table,
501
- indexesOriginRow,
502
- indexesTargetRow,
503
- 0,
504
- )
541
+ const newTable = moveTableRow(table, indexesOriginRow, indexesTargetRow, 0)
505
542
 
506
543
  const _tr = cloneTr(tr).replaceWith(
507
544
  table.pos,
508
545
  table.pos + table.node.nodeSize,
509
- newTable,
546
+ newTable
510
547
  )
511
548
 
512
- if (!select)
513
- return _tr
549
+ if (!select) return _tr
514
550
 
515
551
  const map = TableMap.get(newTable)
516
552
  const start = table.start
@@ -5,89 +5,97 @@ import { $inputRule } from '@milkdown/utils'
5
5
  import { withMeta } from '../__internal__'
6
6
 
7
7
  /// This schema extends the [list item](/preset-commonmark#list-item) schema and add task list support for it.
8
- export const extendListItemSchemaForTask = listItemSchema.extendSchema((prev) => {
9
- return (ctx) => {
10
- const baseSchema = prev(ctx)
11
- return {
12
- ...baseSchema,
13
- attrs: {
14
- ...baseSchema.attrs,
15
- checked: {
16
- default: null,
17
- },
18
- },
19
- parseDOM: [
20
- {
21
- tag: 'li[data-item-type="task"]',
22
- getAttrs: (dom) => {
23
- if (!(dom instanceof HTMLElement))
24
- throw expectDomTypeError(dom)
25
-
26
- return {
27
- label: dom.dataset.label,
28
- listType: dom.dataset.listType,
29
- spread: dom.dataset.spread,
30
- checked: dom.dataset.checked ? dom.dataset.checked === 'true' : null,
31
- }
8
+ export const extendListItemSchemaForTask = listItemSchema.extendSchema(
9
+ (prev) => {
10
+ return (ctx) => {
11
+ const baseSchema = prev(ctx)
12
+ return {
13
+ ...baseSchema,
14
+ attrs: {
15
+ ...baseSchema.attrs,
16
+ checked: {
17
+ default: null,
32
18
  },
33
19
  },
34
- ...baseSchema?.parseDOM || [],
35
- ],
36
- toDOM: (node) => {
37
- if (baseSchema.toDOM && node.attrs.checked == null)
38
- return baseSchema.toDOM(node)
39
-
40
- return [
41
- 'li',
20
+ parseDOM: [
42
21
  {
43
- 'data-item-type': 'task',
44
- 'data-label': node.attrs.label,
45
- 'data-list-type': node.attrs.listType,
46
- 'data-spread': node.attrs.spread,
47
- 'data-checked': node.attrs.checked,
22
+ tag: 'li[data-item-type="task"]',
23
+ getAttrs: (dom) => {
24
+ if (!(dom instanceof HTMLElement)) throw expectDomTypeError(dom)
25
+
26
+ return {
27
+ label: dom.dataset.label,
28
+ listType: dom.dataset.listType,
29
+ spread: dom.dataset.spread,
30
+ checked: dom.dataset.checked
31
+ ? dom.dataset.checked === 'true'
32
+ : null,
33
+ }
34
+ },
48
35
  },
49
- 0,
50
- ]
51
- },
52
- parseMarkdown: {
53
- match: ({ type }) => type === 'listItem',
54
- runner: (state, node, type) => {
55
- if (node.checked == null) {
56
- baseSchema.parseMarkdown.runner(state, node, type)
57
- return
58
- }
36
+ ...(baseSchema?.parseDOM || []),
37
+ ],
38
+ toDOM: (node) => {
39
+ if (baseSchema.toDOM && node.attrs.checked == null)
40
+ return baseSchema.toDOM(node)
41
+
42
+ return [
43
+ 'li',
44
+ {
45
+ 'data-item-type': 'task',
46
+ 'data-label': node.attrs.label,
47
+ 'data-list-type': node.attrs.listType,
48
+ 'data-spread': node.attrs.spread,
49
+ 'data-checked': node.attrs.checked,
50
+ },
51
+ 0,
52
+ ]
53
+ },
54
+ parseMarkdown: {
55
+ match: ({ type }) => type === 'listItem',
56
+ runner: (state, node, type) => {
57
+ if (node.checked == null) {
58
+ baseSchema.parseMarkdown.runner(state, node, type)
59
+ return
60
+ }
59
61
 
60
- const label = node.label != null ? `${node.label}.` : '•'
61
- const checked = node.checked != null ? Boolean(node.checked) : null
62
- const listType = node.label != null ? 'ordered' : 'bullet'
63
- const spread = node.spread != null ? `${node.spread}` : 'true'
62
+ const label = node.label != null ? `${node.label}.` : '•'
63
+ const checked = node.checked != null ? Boolean(node.checked) : null
64
+ const listType = node.label != null ? 'ordered' : 'bullet'
65
+ const spread = node.spread != null ? `${node.spread}` : 'true'
64
66
 
65
- state.openNode(type, { label, listType, spread, checked })
66
- state.next(node.children)
67
- state.closeNode()
67
+ state.openNode(type, { label, listType, spread, checked })
68
+ state.next(node.children)
69
+ state.closeNode()
70
+ },
68
71
  },
69
- },
70
- toMarkdown: {
71
- match: node => node.type.name === 'list_item',
72
- runner: (state, node) => {
73
- if (node.attrs.checked == null) {
74
- baseSchema.toMarkdown.runner(state, node)
75
- return
76
- }
72
+ toMarkdown: {
73
+ match: (node) => node.type.name === 'list_item',
74
+ runner: (state, node) => {
75
+ if (node.attrs.checked == null) {
76
+ baseSchema.toMarkdown.runner(state, node)
77
+ return
78
+ }
77
79
 
78
- const label = node.attrs.label
79
- const listType = node.attrs.listType
80
- const spread = node.attrs.spread === 'true'
81
- const checked = node.attrs.checked
80
+ const label = node.attrs.label
81
+ const listType = node.attrs.listType
82
+ const spread = node.attrs.spread === 'true'
83
+ const checked = node.attrs.checked
82
84
 
83
- state.openNode('listItem', undefined, { label, listType, spread, checked })
84
- state.next(node.content)
85
- state.closeNode()
85
+ state.openNode('listItem', undefined, {
86
+ label,
87
+ listType,
88
+ spread,
89
+ checked,
90
+ })
91
+ state.next(node.content)
92
+ state.closeNode()
93
+ },
86
94
  },
87
- },
95
+ }
88
96
  }
89
97
  }
90
- })
98
+ )
91
99
 
92
100
  withMeta(extendListItemSchemaForTask, {
93
101
  displayName: 'NodeSchema<listItem>',
@@ -97,28 +105,32 @@ withMeta(extendListItemSchemaForTask, {
97
105
  /// Input rule for wrapping a block in task list node.
98
106
  /// Users can type `[ ] ` or `[x] ` to wrap the block in task list node with checked status.
99
107
  export const wrapInTaskListInputRule = $inputRule(() => {
100
- return new InputRule(/^\[(?<checked>\s|x)\]\s$/, (state, match, start, end) => {
101
- const pos = state.doc.resolve(start)
102
- let depth = 0
103
- let node = pos.node(depth)
104
- while (node && node.type.name !== 'list_item') {
105
- depth--
106
- node = pos.node(depth)
107
- }
108
+ return new InputRule(
109
+ /^\[(?<checked>\s|x)\]\s$/,
110
+ (state, match, start, end) => {
111
+ const pos = state.doc.resolve(start)
112
+ let depth = 0
113
+ let node = pos.node(depth)
114
+ while (node && node.type.name !== 'list_item') {
115
+ depth--
116
+ node = pos.node(depth)
117
+ }
108
118
 
109
- if (!node || node.attrs.checked != null)
110
- return null
119
+ if (!node || node.attrs.checked != null) return null
111
120
 
112
- const checked = Boolean(match.groups?.checked === 'x')
121
+ const checked = Boolean(match.groups?.checked === 'x')
113
122
 
114
- const finPos = pos.before(depth)
115
- const tr = state.tr
123
+ const finPos = pos.before(depth)
124
+ const tr = state.tr
116
125
 
117
- tr.deleteRange(start, end)
118
- .setNodeMarkup(finPos, undefined, { ...node.attrs, checked })
126
+ tr.deleteRange(start, end).setNodeMarkup(finPos, undefined, {
127
+ ...node.attrs,
128
+ checked,
129
+ })
119
130
 
120
- return tr
121
- })
131
+ return tr
132
+ }
133
+ )
122
134
  })
123
135
 
124
136
  withMeta(wrapInTaskListInputRule, {