MCPcopy Create free account
hub / github.com/cli/cli / linkReferenceDestination

Function linkReferenceDestination

internal/attachments/references.go:650–692  ·  view source on GitHub ↗

linkReferenceDestination finds the destination in a link reference definition, as the "./f.png" of [ref]: ./f.png "a title", so that rewriting it leaves the label and the title as the author wrote them. It reads the destination out of the source and refuses if that disagrees with what goldmark pars

(src []byte, def *ast.LinkReferenceDefinition)

Source from the content-addressed store, hash-verified

648// with what goldmark parsed, which means the line is shaped in some way this
649// does not understand and is safer left alone.
650func linkReferenceDestination(src []byte, def *ast.LinkReferenceDefinition) (byteRange, bool) {
651 lo, hi, ok := blockRange(def, len(src))
652 if !ok {
653 return byteRange{}, false
654 }
655
656 // Past the label, which ends at the first unescaped "]:".
657 colon := -1
658 for off, c := range src[lo:hi] {
659 i := lo + off
660 if c == ']' && i+1 < hi && src[i+1] == ':' && !isEscaped(src, i) {
661 colon = i + 1
662 break
663 }
664 }
665 if colon < 0 {
666 return byteRange{}, false
667 }
668
669 start := skipSpace(src, colon+1, hi)
670 if start >= hi {
671 return byteRange{}, false
672 }
673 stop := start
674 if src[stop] == '<' {
675 end, ok := scanAngleDest(src, stop, hi)
676 if !ok {
677 return byteRange{}, false
678 }
679 stop = end
680 } else {
681 stop = scanBareDest(src, stop, hi)
682 }
683
684 // A definition continued onto the next line of a blockquote is the case
685 // that reaches here: the block range still carries the ">" prefix of the
686 // continuation, so the destination read from the source is that marker
687 // rather than the path, and rewriting it would eat the blockquote.
688 if comparableSource(string(src[start:stop])) != comparableDestination(string(def.Destination)) {
689 return byteRange{}, false
690 }
691 return byteRange{start: start, stop: stop}, true
692}
693
694// isEscaped reports whether the byte at i is preceded by an odd number of
695// backslashes, which is what makes it a literal rather than markup.

Callers 1

attachAssetsToMarkdownFunction · 0.85

Calls 7

blockRangeFunction · 0.85
isEscapedFunction · 0.85
skipSpaceFunction · 0.85
scanAngleDestFunction · 0.85
scanBareDestFunction · 0.85
comparableSourceFunction · 0.85
comparableDestinationFunction · 0.85

Tested by

no test coverage detected