attachmentArgForDestination is where the two halves of this package meet: it takes a markdown destination and answers which attached file, if any, it names. Only a local path can name one, and markdown offers several spellings for the same path, so each is resolved to an absolute path and looked up
(dest, markdownDir string, byPath map[string]int)
| 364 | // relative spelling is tried in markdownDir before the working directory, so |
| 365 | // the file beside the markdown wins when both exist. |
| 366 | func attachmentArgForDestination(dest, markdownDir string, byPath map[string]int) (int, bool) { |
| 367 | if dest == "" || strings.HasPrefix(dest, "#") || isRemoteDestination(dest) { |
| 368 | return 0, false |
| 369 | } |
| 370 | spellings := candidatePaths(dest) |
| 371 | paths := make([]string, 0, 2*len(spellings)) |
| 372 | if markdownDir != "" { |
| 373 | for _, s := range spellings { |
| 374 | if isRelativePath(s) { |
| 375 | paths = append(paths, filepath.Join(markdownDir, s)) |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | paths = append(paths, spellings...) |
| 380 | for _, path := range paths { |
| 381 | abs, err := filepath.Abs(path) |
| 382 | if err != nil { |
| 383 | continue |
| 384 | } |
| 385 | if i, ok := byPath[abs]; ok { |
| 386 | return i, true |
| 387 | } |
| 388 | } |
| 389 | return 0, false |
| 390 | } |
| 391 | |
| 392 | // isRelativePath reports whether path is relative to a directory, and so can |
| 393 | // be placed inside the one the markdown was read from. filepath.IsAbs is not |
no test coverage detected