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

Function scanMarkdownRefs

internal/attachments/references.go:231–288  ·  view source on GitHub ↗

scanMarkdownRefs finds every place the markdown names one of the attached files.

(md string, attachmentArgs []attachmentArg)

Source from the content-addressed store, hash-verified

229// scanMarkdownRefs finds every place the markdown names one of the attached
230// files.
231func scanMarkdownRefs(md string, attachmentArgs []attachmentArg) ([]attachmentRef, error) {
232 byPath, err := attachmentArgsByPath(attachmentArgs)
233 if err != nil {
234 return nil, err
235 }
236
237 src := []byte(md)
238 doc := goldmark.New().Parser().Parse(text.NewReader(src))
239 defs := linkReferenceDefinitions(doc, len(src))
240
241 var refs []attachmentRef
242
243 err = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
244 if !entering {
245 return ast.WalkContinue, nil
246 }
247
248 dest, isEmbed, ok := linkDestination(n)
249 if !ok {
250 return ast.WalkContinue, nil
251 }
252 idx, ok := attachmentArgForDestination(dest, byPath)
253 if !ok {
254 return ast.WalkContinue, nil
255 }
256
257 // The nearest block ancestor is what carries position information.
258 var block ast.Node
259 for p := n.Parent(); p != nil; p = p.Parent() {
260 if p.Type() == ast.TypeBlock {
261 block = p
262 break
263 }
264 }
265 _, hi, ok := blockRange(block, len(src))
266 if !ok {
267 return ast.WalkContinue, nil
268 }
269
270 if ranges, ok := inlineRanges(src, n, hi); ok {
271 refs = append(refs, attachmentRef{attachmentArg: idx, isEmbed: isEmbed, ranges: ranges, block: block})
272 return ast.WalkContinue, nil
273 }
274
275 // No destination beside the reference means a reference-style link. Every
276 // definition carrying this destination is rewritten, since a node
277 // records no label to tell them apart and a spare definition renders
278 // nothing.
279 if matches := defs[comparableDestination(dest)]; len(matches) > 0 {
280 refs = append(refs, attachmentRef{attachmentArg: idx, isEmbed: isEmbed, defs: matches})
281 }
282 return ast.WalkContinue, nil
283 })
284 if err != nil {
285 return nil, err
286 }
287 return refs, nil
288}

Callers 1

newAttachableMarkdownFunction · 0.85

Calls 8

attachmentArgsByPathFunction · 0.85
linkReferenceDefinitionsFunction · 0.85
linkDestinationFunction · 0.85
blockRangeFunction · 0.85
inlineRangesFunction · 0.85
comparableDestinationFunction · 0.85
TypeMethod · 0.45

Tested by

no test coverage detected