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

Function parseIncludePath

pkg/cli/remove_command.go:428–494  ·  view source on GitHub ↗

parseIncludePath extracts the file path from @include/@import/{{#import}} directive lines without allocating a regex submatch slice or a directive struct. Returns an empty string if the line is not a recognised directive. Section references (e.g. file.md#Section) are stripped from the returned path.

(line string)

Source from the content-addressed store, hash-verified

426// Returns an empty string if the line is not a recognised directive.
427// Section references (e.g. file.md#Section) are stripped from the returned path.
428func parseIncludePath(line string) string {
429 trimmed := strings.TrimSpace(line)
430 if trimmed == "" {
431 return ""
432 }
433
434 // Fast path: the vast majority of lines are not directives.
435 // Checking the first byte avoids three full HasPrefix comparisons.
436 if trimmed[0] != '@' && trimmed[0] != '{' {
437 return ""
438 }
439
440 var rest string
441
442 switch {
443 case strings.HasPrefix(trimmed, "@include"):
444 rest = trimmed[len("@include"):]
445 case strings.HasPrefix(trimmed, "@import"):
446 rest = trimmed[len("@import"):]
447 case strings.HasPrefix(trimmed, "{{#import"):
448 rest = trimmed[len("{{#import"):]
449 // Skip optional marker '?'
450 if len(rest) > 0 && rest[0] == '?' {
451 rest = rest[1:]
452 }
453 // Skip optional whitespace, then an optional single colon, then optional whitespace
454 // (mirrors the regex \s*:?\s* in IncludeDirectivePattern)
455 rest = strings.TrimSpace(rest)
456 if len(rest) > 0 && rest[0] == ':' {
457 rest = strings.TrimSpace(rest[1:])
458 }
459 // Extract path up to closing "}}" and require only whitespace after it.
460 before, after, ok := strings.Cut(rest, "}}")
461 if !ok || strings.TrimSpace(after) != "" {
462 return ""
463 }
464 path := strings.TrimSpace(before)
465 if path == "" {
466 return ""
467 }
468 // Strip section reference (file.md#Section → file.md)
469 if filePath, _, ok := strings.Cut(path, "#"); ok {
470 return filePath
471 }
472 return path
473 default:
474 return ""
475 }
476
477 // Handle @include and @import: skip optional marker '?'
478 if len(rest) > 0 && rest[0] == '?' {
479 rest = rest[1:]
480 }
481 // Require at least one whitespace character after the directive keyword
482 if rest == "" || (rest[0] != ' ' && rest[0] != '\t') {
483 return ""
484 }
485 path := strings.TrimSpace(rest)

Callers 1

findIncludesInContentFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected