11import type { NodeHandler } from 'comark/render'
22import type { ComarkElement , ComarkNode } from 'comark'
3+ import { indent } from 'comark/utils'
4+
5+ // Block elements that need explicit indentation in list items.
6+ // Note: ol/ul are handled by their own handlers which manage indentation via listIndent context.
7+ const blockElements = new Set ( [ 'pre' , 'blockquote' , 'table' ] )
38
49export const li : NodeHandler = async ( node , state ) => {
510 const children = node . slice ( 2 ) as ComarkNode [ ]
@@ -14,15 +19,31 @@ export const li: NodeHandler = async (node, state) => {
1419 prefix += input [ 1 ] . checked || input [ 1 ] [ ':checked' ] ? '[x] ' : '[ ] '
1520 }
1621
17- let content = ''
22+ const prefixWidth = prefix . length
23+ let result = ''
1824 for ( const child of children ) {
19- content += await state . one ( child , state , node )
25+ const rendered = await state . one ( child , state , node )
26+ if ( result && Array . isArray ( child ) ) {
27+ if ( blockElements . has ( child [ 0 ] as string ) ) {
28+ // Block-level child: put on its own line and indent to align with list prefix
29+ const indented = indent ( rendered , { width : prefixWidth } )
30+ result = result . trimEnd ( ) + '\n' + indented . trimEnd ( ) + '\n'
31+ continue
32+ }
33+
34+ if ( child [ 0 ] === 'p' ) {
35+ const indented = indent ( rendered , { width : prefixWidth } )
36+ result = result . trimEnd ( ) + '\n\n' + indented . trimEnd ( ) + '\n'
37+ continue
38+ }
39+ }
40+ result += rendered
2041 }
21- content = content . trim ( )
42+ result = result . trim ( )
2243
2344 if ( typeof order === 'number' ) {
2445 state . applyContext ( { order : order + 1 } )
2546 }
2647
27- return `${ prefix } ${ content } \n`
48+ return `${ prefix } ${ result } \n`
2849}
0 commit comments