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.  alone in a paragraph image: swap the path video: the ba
(v attachableMarkdown)
| 95 | // |
| 96 | // A path inside a code fence or an inline code span is left alone. |
| 97 | func 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 |