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

Function processImportsTextBased

pkg/parser/frontmatter_hash.go:414–476  ·  view source on GitHub ↗

processImportsTextBased processes imports from frontmatter using text-based parsing Returns: importedFiles (list of import paths), importedFrontmatterTexts (list of frontmatter texts)

(frontmatterText, baseDir string, visited map[string]struct {
}, fileReader FileReader)

Source from the content-addressed store, hash-verified

412// processImportsTextBased processes imports from frontmatter using text-based parsing
413// Returns: importedFiles (list of import paths), importedFrontmatterTexts (list of frontmatter texts)
414func processImportsTextBased(frontmatterText, baseDir string, visited map[string]struct {
415}, fileReader FileReader) ([]string, []string, error) {
416 var importedFiles []string
417 var importedFrontmatterTexts []string
418
419 // Extract imports from frontmatter text
420 imports := extractImportsFromText(frontmatterText)
421
422 if len(imports) == 0 {
423 return importedFiles, importedFrontmatterTexts, nil
424 }
425
426 frontmatterHashLog.Printf("Processing %d import(s) text-based from baseDir=%s", len(imports), baseDir)
427
428 // Sort imports for deterministic processing
429 sort.Strings(imports)
430
431 for _, importPath := range imports {
432 // Resolve import path relative to base directory
433 fullPath := filepath.Join(baseDir, importPath)
434
435 // Skip if already visited (cycle detection)
436 if setutil.Contains(visited, fullPath) {
437 frontmatterHashLog.Printf("Skipping already-visited import (cycle detection): %s", fullPath)
438 continue
439 }
440 visited[fullPath] = struct {
441 }{}
442
443 // Read imported file using the provided file reader
444 content, err := fileReader(fullPath)
445 if err != nil {
446 // Skip missing imports silently (matches JavaScript behavior)
447 continue
448 }
449
450 // Extract frontmatter text from imported file
451 importFrontmatterText, _, err := extractFrontmatterAndBodyText(string(content))
452 if err != nil {
453 // Skip files with invalid frontmatter
454 continue
455 }
456
457 // Add to imported files and texts
458 importedFiles = append(importedFiles, importPath)
459 importedFrontmatterTexts = append(importedFrontmatterTexts, importFrontmatterText)
460
461 // Recursively process imports in the imported file
462 importBaseDir := filepath.Dir(fullPath)
463 nestedFiles, nestedTexts, err := processImportsTextBased(importFrontmatterText, importBaseDir, visited, fileReader)
464 if err != nil {
465 // Continue processing other imports even if one fails
466 continue
467 }
468
469 // Add nested imports
470 importedFiles = append(importedFiles, nestedFiles...)
471 importedFrontmatterTexts = append(importedFrontmatterTexts, nestedTexts...)

Calls 4

ContainsFunction · 0.92
extractImportsFromTextFunction · 0.70
PrintfMethod · 0.45

Tested by

no test coverage detected