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

Function linkReferenceDestination

internal/attachments/references.go:687–729  ·  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

685// with what goldmark parsed, which means the line is shaped in some way this
686// does not understand and is safer left alone.
687func linkReferenceDestination(src []byte, def *ast.LinkReferenceDefinition) (byteRange, bool) {
688 lo, hi, ok := blockRange(def, len(src))
689 if !ok {
690 return byteRange{}, false
691 }
692
693 // Past the label, which ends at the first unescaped "]:".
694 colon := -1
695 for off, c := range src[lo:hi] {
696 i := lo + off
697 if c == ']' && i+1 < hi && src[i+1] == ':' && !isEscaped(src, i) {
698 colon = i + 1
699 break
700 }
701 }
702 if colon < 0 {
703 return byteRange{}, false
704 }
705
706 start := skipSpace(src, colon+1, hi)
707 if start >= hi {
708 return byteRange{}, false
709 }
710 stop := start
711 if src[stop] == '<' {
712 end, ok := scanAngleDest(src, stop, hi)
713 if !ok {
714 return byteRange{}, false
715 }
716 stop = end
717 } else {
718 stop = scanBareDest(src, stop, hi)
719 }
720
721 // A definition continued onto the next line of a blockquote is the case
722 // that reaches here: the block range still carries the ">" prefix of the
723 // continuation, so the destination read from the source is that marker
724 // rather than the path, and rewriting it would eat the blockquote.
725 if comparableSource(string(src[start:stop])) != comparableDestination(string(def.Destination)) {
726 return byteRange{}, false
727 }
728 return byteRange{start: start, stop: stop}, true
729}
730
731// isEscaped reports whether the byte at i is preceded by an odd number of
732// 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