Skip to content

Commit 65abdfb

Browse files
CopilotDongyuZhao
andcommitted
Fix indented code block logic to respect list item contexts
Co-authored-by: DongyuZhao <8455725+DongyuZhao@users.noreply.github.com>
1 parent 942ebe7 commit 65abdfb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Sources/CodeParserCollection/Markdown/Nodes/MarkdownIndentedCodeBlockBuilder.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ public class MarkdownIndentedCodeBlockBuilder: CodeNodeBuilder {
7171
if context.current.element == .paragraph {
7272
return false
7373
}
74+
75+
// Check if we're in a list item context - indented content should be treated as list continuation
76+
// rather than code block if the indentation matches list item requirements
77+
if let listItem = findContainingListItem(context.current) {
78+
// If the indentation is exactly what's needed for list item continuation,
79+
// don't create a code block - let list continuation handle it
80+
if indentationSpaces < listItem.contentIndent + 4 {
81+
return false
82+
}
83+
}
7484

7585
// If we reached end of tokens, this is just indented whitespace - not a code block
7686
guard index < context.tokens.count else {
@@ -131,4 +141,15 @@ public class MarkdownIndentedCodeBlockBuilder: CodeNodeBuilder {
131141

132142
return true
133143
}
144+
145+
private func findContainingListItem(_ node: CodeNode<MarkdownNodeElement>) -> ListItemNode? {
146+
var current: CodeNode<MarkdownNodeElement>? = node
147+
while let n = current {
148+
if let listItem = n as? ListItemNode {
149+
return listItem
150+
}
151+
current = n.parent
152+
}
153+
return nil
154+
}
134155
}

Sources/CodeParserCollection/Markdown/Nodes/MarkdownUnifiedListBuilder.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ public class MarkdownUnifiedListBuilder: CodeNodeBuilder {
220220
// Handle paragraph continuation vs creation based on blank line context
221221
handleParagraphContinuationInListItem(targetListItem, state: state, hasBlankLineBefore: state.lastWasBlankLine)
222222

223+
// Return true to indicate we've set the correct context
224+
// Let the leafOnLine phase builders handle the actual content in this context
223225
return true
224226
}
225227
}

0 commit comments

Comments
 (0)