parse method
Element?
parse(
- BlockParser parser
)
Implementation
@override
md.Element? parse(md.BlockParser parser) {
// Get the current line being parsed.
final md.Line firstLine = parser.current;
// --- Debugging Prints ---
// Print the content of the current line the parser is looking at,
// including showing potential hidden characters.
print('WidgetBlockSyntax: Checking line for start pattern:');
print(' Content (raw): "${firstLine.content}"');
print(' Content (debug): "${firstLine.content.replaceAll(' ', '_').replaceAll('\n', '\\n')}"'); // Replace space with _, newline with \n for visibility
// --- End Debugging Prints ---
// Attempt to match the start pattern against the content of the current line.
final RegExpMatch? startMatch = pattern.firstMatch(firstLine.content);
// If no match for the start pattern, this block syntax doesn't apply.
if (startMatch == null) {
// --- Debugging Prints ---
print('WidgetBlockSyntax: Start pattern did NOT match.');
// --- End Debugging Prints ---
return null;
}
// --- Debugging Prints ---
print('WidgetBlockSyntax: Start pattern matched!');
print(' Start match (group 0): "${startMatch.group(0)}"');
print(' Label group (1): "${startMatch.group(1)}"');
// --- End Debugging Prints ---
// Extract the label (group 1).
final String label = startMatch.group(1)!;
// Consume the first line (the start fence).
parser.advance();
// Collect lines until the end fence is found.
final List<String> contentLines = [];
int linesConsumed = 1; // Start with 1 line consumed (the start fence)
while (!parser.isDone) {
final md.Line currentLine = parser.current;
// --- Debugging Prints ---
print('WidgetBlockSyntax: Checking line for end pattern:');
print(' Content (raw): "${currentLine.content}"');
print(' Content (debug): "${currentLine.content.replaceAll(' ', '_').replaceAll('\n', '\\n')}"'); // Replace space with _, newline with \n for visibility
// --- End Debugging Prints ---
// Check if the current line matches the end fence pattern.
final RegExpMatch? endMatch = RegExp(_endPattern).firstMatch(currentLine.content);
if (endMatch != null) {
// Found the end fence.
// Consume the end fence line.
parser.advance();
linesConsumed++;
// --- Debugging Prints ---
print('WidgetBlockSyntax: End pattern matched. Consumed end fence.');
// --- End Debugging Prints ---
break; // Exit the loop
}
// Add the current line's content to the collected lines.
contentLines.add(currentLine.content);
// Consume the current line.
parser.advance();
linesConsumed++;
// --- Debugging Prints ---
print('WidgetBlockSyntax: Added line to content: "${currentLine.content}"');
// --- End Debugging Prints ---
}
// Join the collected lines to form the JSON string content.
// Trim potential leading/trailing whitespace from the joined content.
final String jsonString = contentLines.join('\n').trim();
// Create a custom Markdown element.
// The tag 'widgetBlock' is arbitrary but should be unique and used
// to map to our custom builder (WidgetElementBuilder).
// We store the extracted label and JSON string in the element's attributes.
// Using the main Element constructor with tag and empty children list.
// final element = md.Element('widgetBlock', []);
// final element = md.Element('blockquote', []);
// final element = md.Element.empty('widget');
final element = md.Element('widget', []);
// final element = md.Element.empty('widgetBlock');
element.attributes.addAll({
'label': label,
'data': jsonString,
});
// --- Debugging Prints ---
print('WidgetBlockSyntax: Element created with tag "${element.tag}", attributes "${element.attributes}".');
print('WidgetBlockSyntax: Total lines consumed: $linesConsumed');
// Removed print for element.type and element.parent as they are not available in this version.
// --- End Debugging Prints ---
return element; // Return the created element
}