extractBlock static method

String? extractBlock(
  1. String content,
  2. String blockName
)

Extracts the content between the first top-level <blockName> { and its matching closing brace, tracking nested braces so unrelated {} pairs inside the block (conditionals, nested blocks) don't truncate the match early. Returns null if the block isn't found.

Implementation

static String? extractBlock(String content, String blockName) {
  final startMatch = RegExp('$blockName\\s*\\{').firstMatch(content);

  if (startMatch == null) {
    return null;
  }

  var depth = 1;
  var i = startMatch.end;

  while (i < content.length && depth > 0) {
    if (content[i] == '{') {
      depth++;
    } else if (content[i] == '}') {
      depth--;
    }
    i++;
  }

  return content.substring(startMatch.end, depth == 0 ? i - 1 : i);
}