MCPcopy Create free account
hub / github.com/github/gh-aw / removeFieldFromBlock

Function removeFieldFromBlock

pkg/cli/yaml_frontmatter_utils.go:171–245  ·  view source on GitHub ↗

removeFieldFromBlock removes a field and its nested content from a YAML block. If removing the field leaves the parent block truly empty (no children, not even comments), the parent block line is also removed to avoid a dangling "parentBlock:" key (which YAML parses as null). Returns the modified li

(lines []string, fieldName string, parentBlock string)

Source from the content-addressed store, hash-verified

169// "parentBlock:" key (which YAML parses as null).
170// Returns the modified lines and whether any changes were made.
171func removeFieldFromBlock(lines []string, fieldName string, parentBlock string) ([]string, bool) {
172 var result []string
173 var modified bool
174 var inParentBlock bool
175 var parentIndent string
176 var inFieldBlock bool
177 var fieldIndent string
178
179 for i, line := range lines {
180 trimmedLine := strings.TrimSpace(line)
181
182 // Track if we're in the parent block
183 if strings.HasPrefix(trimmedLine, parentBlock+":") {
184 inParentBlock = true
185 parentIndent = getIndentation(line)
186 result = append(result, line)
187 continue
188 }
189
190 // Check if we've left the parent block
191 if inParentBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
192 if hasExitedBlock(line, parentIndent) {
193 inParentBlock = false
194 }
195 }
196
197 // Remove field line if in parent block
198 if inParentBlock && strings.HasPrefix(trimmedLine, fieldName+":") {
199 modified = true
200 inFieldBlock = true
201 fieldIndent = getIndentation(line)
202 yamlUtilsLog.Printf("Removed %s.%s on line %d", parentBlock, fieldName, i+1)
203 continue
204 }
205
206 // Skip nested properties under the field (lines with greater indentation)
207 if inFieldBlock {
208 // Empty lines within the field block should be removed
209 if trimmedLine == "" {
210 continue
211 }
212
213 currentIndent := getIndentation(line)
214
215 // Comments need to check indentation
216 if strings.HasPrefix(trimmedLine, "#") {
217 if len(currentIndent) > len(fieldIndent) {
218 // Comment is nested under field, remove it
219 yamlUtilsLog.Printf("Removed nested %s comment on line %d: %s", fieldName, i+1, trimmedLine)
220 continue
221 }
222 // Comment is at same or less indentation, exit field block and keep it
223 inFieldBlock = false
224 result = append(result, line)
225 continue
226 }
227
228 // If this line has more indentation than field, it's a nested property

Calls 4

getIndentationFunction · 0.85
hasExitedBlockFunction · 0.85
PrintfMethod · 0.45