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

Function attachAssetsToMarkdown

internal/attachments/references.go:97–196  ·  view source on GitHub ↗

attachAssetsToMarkdown points every rewritable markdown reference to an attached file at that file's asset URL. It reports one replacement per successfully rewritten attachment and which uploaded arguments remain to append. ![alt](./file) alone in a paragraph image: swap the path video: the ba

(v attachableMarkdown)

Source from the content-addressed store, hash-verified

95//
96// A path inside a code fence or an inline code span is left alone.
97func attachAssetsToMarkdown(v attachableMarkdown) (attachedMarkdown, error) {
98 md := v.markdown
99 if len(v.args) == 0 {
100 return attachedMarkdown{Rewritten: md}, nil
101 }
102
103 src, refs, attachmentArgs := []byte(md), v.refs, v.args
104
105 var edits []edit
106
107 // Looping over the markdown references to produce a plan for edits.
108 for _, r := range refs {
109 arg := attachmentArgs[r.attachmentArg]
110
111 // It's not been uploaded.
112 if arg.URL == "" {
113 continue
114 }
115
116 // Reference style links get rewritten at their definitions, not inline.
117 if r.referenceStyle() {
118 for _, def := range r.defs {
119 dest, ok := linkReferenceDestination(src, def)
120 // TODO: We skip the edit because a definition split across a blockquote
121 // carries the ">" marker, and rewriting it would mangle the quote. The
122 // fallback that appends unrewritten files operates per file. A rewritable
123 // reference to the same file elsewhere suppresses the append fallback,
124 // leaving this definition pointing at its original local path. We accept
125 // this because the tool leaves the reference exactly as the author wrote
126 // it, rewriting only what can be safely rewritten.
127 if !ok {
128 continue
129 }
130 edits = append(edits, edit{r.attachmentArg, dest, arg.URL})
131 }
132 continue
133 }
134
135 at := r.ranges
136 playerEmbed := arg.RendersAsPlayer && r.isEmbed
137
138 switch {
139 case !playerEmbed:
140 // Only the destination moves, so alt text, titles, and formatting
141 // inside the label survive.
142 edits = append(edits, edit{r.attachmentArg, at.dest, arg.URL})
143
144 case standsAlone(src, r.block, at.node):
145 // A player only renders from a bare URL alone in a paragraph, so
146 // the whole node goes and any alt text is dropped.
147 edits = append(edits, edit{r.attachmentArg, at.node, arg.URL})
148
149 default:
150 // Degrade to a link: only the "!" and the destination move, so a
151 // reference nested in the alt text is still rewritten in place.
152 //
153 // A literal "!" before the embed's own would pair with the "["
154 // left behind and re-form an embed, so it is absorbed into the

Callers 3

attachMethod · 0.85

Calls 6

linkReferenceDestinationFunction · 0.85
standsAloneFunction · 0.85
isEscapedFunction · 0.85
escapeAltFunction · 0.85
applyEditsFunction · 0.85
referenceStyleMethod · 0.80

Tested by 2