SetFieldInOnTrigger sets a field value in the 'on' trigger object in the frontmatter. This handles nested fields like "stop-after" which are located under the "on" key. It preserves the original formatting of the frontmatter including comments and blank lines.
(content, fieldName, fieldValue string)
| 314 | // This handles nested fields like "stop-after" which are located under the "on" key. |
| 315 | // It preserves the original formatting of the frontmatter including comments and blank lines. |
| 316 | func SetFieldInOnTrigger(content, fieldName, fieldValue string) (string, error) { |
| 317 | frontmatterEditorLog.Printf("Setting field in 'on' trigger: %s = %s", fieldName, fieldValue) |
| 318 | |
| 319 | // Parse frontmatter using parser package |
| 320 | result, err := parser.ExtractFrontmatterFromContent(content) |
| 321 | if err != nil { |
| 322 | frontmatterEditorLog.Printf("Failed to parse frontmatter: %v", err) |
| 323 | return "", fmt.Errorf("failed to parse frontmatter: %w", err) |
| 324 | } |
| 325 | |
| 326 | // Check if frontmatter exists |
| 327 | if result.Frontmatter == nil { |
| 328 | // No frontmatter, cannot set nested field without 'on' block |
| 329 | return "", errors.New("no frontmatter found, cannot set field in 'on' trigger") |
| 330 | } |
| 331 | |
| 332 | // Check if 'on' field exists |
| 333 | onValue, exists := result.Frontmatter["on"] |
| 334 | if !exists { |
| 335 | // No 'on' field exists, need to create it |
| 336 | // Add the 'on:' block with the field at the beginning of frontmatter |
| 337 | if len(result.FrontmatterLines) > 0 { |
| 338 | frontmatterEditorLog.Printf("Creating 'on' block with field %s", fieldName) |
| 339 | |
| 340 | // Create new frontmatter lines with 'on:' block at the start |
| 341 | frontmatterLines := make([]string, 0, len(result.FrontmatterLines)+2) |
| 342 | frontmatterLines = append(frontmatterLines, "on:") |
| 343 | frontmatterLines = append(frontmatterLines, fmt.Sprintf(" %s: %s", fieldName, fieldValue)) |
| 344 | frontmatterLines = append(frontmatterLines, result.FrontmatterLines...) |
| 345 | |
| 346 | // Reconstruct the file |
| 347 | var lines []string |
| 348 | lines = append(lines, "---") |
| 349 | lines = append(lines, frontmatterLines...) |
| 350 | lines = append(lines, "---") |
| 351 | if result.Markdown != "" { |
| 352 | lines = append(lines, "") |
| 353 | lines = append(lines, result.Markdown) |
| 354 | } |
| 355 | |
| 356 | frontmatterEditorLog.Printf("Successfully created 'on' block with field %s", fieldName) |
| 357 | return strings.Join(lines, "\n"), nil |
| 358 | } |
| 359 | |
| 360 | // No frontmatter lines, cannot create 'on' block |
| 361 | return "", errors.New("no frontmatter found, cannot set field in 'on' trigger") |
| 362 | } |
| 363 | |
| 364 | // Check if 'on' is an object (map) |
| 365 | _, isMap := onValue.(map[string]any) |
| 366 | if !isMap { |
| 367 | // 'on' is not a map (might be a string), cannot set field |
| 368 | return "", errors.New("'on' field is not an object, cannot set nested field") |
| 369 | } |
| 370 | |
| 371 | // Work with raw frontmatter lines to preserve formatting |
| 372 | if len(result.FrontmatterLines) > 0 { |
| 373 | frontmatterEditorLog.Printf("Using raw frontmatter lines to set field (%d lines)", len(result.FrontmatterLines)) |