55
66import { containerFlow } from '../util/container-flow.js'
77import { checkBullet } from '../util/check-bullet.js'
8- import { checkOtherBullet } from '../util/check-other- bullet.js'
8+ import { checkBulletOther } from '../util/check-bullet-other .js'
99import { checkRule } from '../util/check-rule.js'
1010
1111/**
1212 * @type {Handle }
1313 * @param {List } node
1414 */
15- export function list ( node , _ , context ) {
15+ export function list ( node , parent , context ) {
1616 const exit = context . enter ( 'list' )
17- const currentBullet = context . currentBullet
17+ const bulletCurrent = context . bulletCurrent
1818 /** @type {string } */
1919 let bullet = checkBullet ( context )
20- const otherBullet = checkOtherBullet ( context )
20+ /** @type {string } */
21+ const bulletOther = checkBulletOther ( context )
22+ const bulletLastUsed = context . bulletLastUsed
2123
2224 if ( node . ordered ) {
2325 bullet = '.'
2426 } else {
2527 const firstListItem = node . children ? node . children [ 0 ] : undefined
2628 let useDifferentMarker = false
2729
28- // If there’s an empty first list item, directly in two list items,
30+ if (
31+ parent &&
32+ context . options . bulletOther &&
33+ bulletLastUsed &&
34+ bullet === bulletLastUsed
35+ ) {
36+ useDifferentMarker = true
37+ }
38+
39+ // If there’s an empty first list item directly in two list items,
2940 // we have to use a different bullet:
3041 //
3142 // ```markdown
@@ -34,16 +45,22 @@ export function list(node, _, context) {
3445 //
3546 // …because otherwise it would become one big thematic break.
3647 if (
48+ // Bullet could be used as a thematic break marker:
49+ ( bullet === '*' || bullet === '-' ) &&
50+ // Empty first list item:
3751 firstListItem &&
38- // Empty list item:
3952 ( ! firstListItem . children || ! firstListItem . children [ 0 ] ) &&
4053 // Directly in two other list items:
54+ context . stack [ context . stack . length - 1 ] === 'list' &&
4155 context . stack [ context . stack . length - 2 ] === 'listItem' &&
42- context . stack [ context . stack . length - 4 ] === 'listItem'
56+ context . stack [ context . stack . length - 3 ] === 'list' &&
57+ context . stack [ context . stack . length - 4 ] === 'listItem' &&
58+ // That are each the first child.
59+ context . indexStack [ context . indexStack . length - 1 ] === 0 &&
60+ context . indexStack [ context . indexStack . length - 2 ] === 0 &&
61+ context . indexStack [ context . indexStack . length - 3 ] === 0 &&
62+ context . indexStack [ context . indexStack . length - 4 ] === 0
4363 ) {
44- // Note: this is only needed for first children of first children,
45- // but the code checks for *children*, not *first*.
46- // So this might generate different bullets where not really needed.
4764 useDifferentMarker = true
4865 }
4966
@@ -60,6 +77,7 @@ export function list(node, _, context) {
6077
6178 while ( ++ index < node . children . length ) {
6279 const item = node . children [ index ]
80+
6381 if (
6482 item &&
6583 item . type === 'listItem' &&
@@ -74,13 +92,14 @@ export function list(node, _, context) {
7492 }
7593
7694 if ( useDifferentMarker ) {
77- bullet = otherBullet
95+ bullet = bulletOther
7896 }
7997 }
8098
81- context . currentBullet = bullet
99+ context . bulletCurrent = bullet
82100 const value = containerFlow ( node , context )
83- context . currentBullet = currentBullet
101+ context . bulletLastUsed = bullet
102+ context . bulletCurrent = bulletCurrent
84103 exit ( )
85104 return value
86105}
0 commit comments