fastParseTitleFromReader scans lines from r for the first H1 header, skipping an optional frontmatter block, without reading the entire file into memory. This is more efficient than fastParseTitle for file-based callers because it stops reading as soon as the title is found. Frontmatter is recognis
(r io.Reader)
| 353 | // Returns the H1 title text, or ("", nil) when no H1 header is present. |
| 354 | // Returns an error if frontmatter is opened but never closed. |
| 355 | func fastParseTitleFromReader(r io.Reader) (string, error) { |
| 356 | scanner := bufio.NewScanner(r) |
| 357 | // Reuse the small initial scanner buffer across calls while still allowing |
| 358 | // growth up to 1 MB for large frontmatter values or long base64-encoded lines. |
| 359 | pooled := workflowTitleScannerBufferPool.Get() |
| 360 | scannerBufferPtr, ok := pooled.(*[]byte) |
| 361 | if !ok || scannerBufferPtr == nil { |
| 362 | fallback := make([]byte, workflowTitleScannerBufferSize) |
| 363 | scannerBufferPtr = &fallback |
| 364 | } |
| 365 | scannerBuffer := *scannerBufferPtr |
| 366 | if cap(scannerBuffer) != workflowTitleScannerBufferSize { |
| 367 | scannerBuffer = make([]byte, workflowTitleScannerBufferSize) |
| 368 | } else { |
| 369 | scannerBuffer = scannerBuffer[:workflowTitleScannerBufferSize] |
| 370 | } |
| 371 | defer func() { |
| 372 | *scannerBufferPtr = scannerBuffer |
| 373 | workflowTitleScannerBufferPool.Put(scannerBufferPtr) |
| 374 | }() |
| 375 | scanner.Buffer(scannerBuffer, 1024*1024) |
| 376 | firstLine := true |
| 377 | inFrontmatter := false |
| 378 | for scanner.Scan() { |
| 379 | trimmed := strings.TrimSpace(scanner.Text()) |
| 380 | if firstLine { |
| 381 | firstLine = false |
| 382 | if trimmed == "---" { |
| 383 | inFrontmatter = true |
| 384 | continue |
| 385 | } |
| 386 | } else if inFrontmatter { |
| 387 | if trimmed == "---" { |
| 388 | inFrontmatter = false |
| 389 | } |
| 390 | continue |
| 391 | } |
| 392 | if strings.HasPrefix(trimmed, "# ") { |
| 393 | return strings.TrimSpace(trimmed[2:]), nil |
| 394 | } |
| 395 | } |
| 396 | if err := scanner.Err(); err != nil { |
| 397 | return "", err |
| 398 | } |
| 399 | |
| 400 | // Unclosed frontmatter is an error (consistent with ExtractFrontmatterFromContent). |
| 401 | if inFrontmatter { |
| 402 | return "", errors.New("frontmatter not properly closed") |
| 403 | } |
| 404 | |
| 405 | return "", nil |
| 406 | } |
| 407 | |
| 408 | // extractWorkflowNameFromFile extracts the workflow name from a file's H1 header |
| 409 | func extractWorkflowNameFromFile(filePath string) (title string, err error) { |
no test coverage detected