addFriendlyScheduleComments adds comments showing the original friendly format for schedule cron expressions This function is called after the YAML has been generated from the frontmatter
(yamlStr string, frontmatter map[string]any)
| 410 | // addFriendlyScheduleComments adds comments showing the original friendly format for schedule cron expressions |
| 411 | // This function is called after the YAML has been generated from the frontmatter |
| 412 | func (c *Compiler) addFriendlyScheduleComments(yamlStr string, frontmatter map[string]any) string { |
| 413 | // Retrieve the friendly formats for this compilation |
| 414 | if len(c.scheduleFriendlyFormats) == 0 { |
| 415 | return yamlStr |
| 416 | } |
| 417 | |
| 418 | // Process the YAML string to add comments |
| 419 | lines := strings.Split(yamlStr, "\n") |
| 420 | var result []string |
| 421 | scheduleItemIndex := -1 |
| 422 | inScheduleArray := false |
| 423 | |
| 424 | for _, line := range lines { |
| 425 | trimmedLine := strings.TrimSpace(line) |
| 426 | |
| 427 | // Check if we're entering the schedule array |
| 428 | if strings.HasPrefix(trimmedLine, "schedule:") { |
| 429 | inScheduleArray = true |
| 430 | scheduleItemIndex = -1 |
| 431 | result = append(result, line) |
| 432 | continue |
| 433 | } |
| 434 | |
| 435 | // Check if we're leaving the schedule section (new top-level key) |
| 436 | if inScheduleArray && strings.TrimSpace(line) != "" && !strings.HasPrefix(line, " ") && !strings.HasPrefix(line, "\t") { |
| 437 | inScheduleArray = false |
| 438 | } |
| 439 | |
| 440 | // If we're in the schedule array and find a cron line, add the friendly comment |
| 441 | if inScheduleArray && strings.Contains(trimmedLine, "cron:") { |
| 442 | scheduleItemIndex++ |
| 443 | result = append(result, line) |
| 444 | |
| 445 | // Add friendly format comment if available |
| 446 | if friendly, exists := c.scheduleFriendlyFormats[scheduleItemIndex]; exists { |
| 447 | // Get the indentation of the cron line |
| 448 | indentation := "" |
| 449 | if len(line) > len(trimmedLine) { |
| 450 | indentation = line[:len(line)-len(trimmedLine)] |
| 451 | } |
| 452 | // Add comment with friendly format on the next line |
| 453 | comment := indentation + " # Friendly format: " + friendly |
| 454 | result = append(result, comment) |
| 455 | } |
| 456 | continue |
| 457 | } |
| 458 | |
| 459 | result = append(result, line) |
| 460 | } |
| 461 | |
| 462 | return strings.Join(result, "\n") |
| 463 | } |
| 464 | |
| 465 | // addDailyCronWarning emits a warning when a daily cron pattern with fixed time is detected |
| 466 | func (c *Compiler) addDailyCronWarning(cronExpr string) { |
no outgoing calls