'[': parse a link or an image or a footnote
(p *Markdown, data []byte, offset int)
| 230 | |
| 231 | // '[': parse a link or an image or a footnote |
| 232 | func link(p *Markdown, data []byte, offset int) (int, *Node) { |
| 233 | // no links allowed inside regular links, footnote, and deferred footnotes |
| 234 | if p.insideLink && (offset > 0 && data[offset-1] == '[' || len(data)-1 > offset && data[offset+1] == '^') { |
| 235 | return 0, nil |
| 236 | } |
| 237 | |
| 238 | var t linkType |
| 239 | switch { |
| 240 | // special case: ![^text] == deferred footnote (that follows something with |
| 241 | // an exclamation point) |
| 242 | case p.extensions&Footnotes != 0 && len(data)-1 > offset && data[offset+1] == '^': |
| 243 | t = linkDeferredFootnote |
| 244 | // ![alt] == image |
| 245 | case offset >= 0 && data[offset] == '!': |
| 246 | t = linkImg |
| 247 | offset++ |
| 248 | // ^[text] == inline footnote |
| 249 | // [^refId] == deferred footnote |
| 250 | case p.extensions&Footnotes != 0: |
| 251 | if offset >= 0 && data[offset] == '^' { |
| 252 | t = linkInlineFootnote |
| 253 | offset++ |
| 254 | } else if len(data)-1 > offset && data[offset+1] == '^' { |
| 255 | t = linkDeferredFootnote |
| 256 | } |
| 257 | // [text] == regular link |
| 258 | default: |
| 259 | t = linkNormal |
| 260 | } |
| 261 | |
| 262 | data = data[offset:] |
| 263 | |
| 264 | var ( |
| 265 | i = 1 |
| 266 | noteID int |
| 267 | title, link, altContent []byte |
| 268 | textHasNl = false |
| 269 | ) |
| 270 | |
| 271 | if t == linkDeferredFootnote { |
| 272 | i++ |
| 273 | } |
| 274 | |
| 275 | // look for the matching closing bracket |
| 276 | for level := 1; level > 0 && i < len(data); i++ { |
| 277 | switch { |
| 278 | case data[i] == '\n': |
| 279 | textHasNl = true |
| 280 | |
| 281 | case isBackslashEscaped(data, i): |
| 282 | continue |
| 283 | |
| 284 | case data[i] == '[': |
| 285 | level++ |
| 286 | |
| 287 | case data[i] == ']': |
| 288 | level-- |
| 289 | if level <= 0 { |
no test coverage detected
searching dependent graphs…