Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #184 #1

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ An attached link is [here](<path-to-image>)
```

**NOTE**: Be careful with `Attachment`! If your path string is a subset of
another longer string or referenced in text, you may get undesired behavior.
another longer string or referenced in text, it will be replaced as well.
An exception to this is when the path string is wrapped in [], e.g. [Attachment.txt](Attachment.txt)
only replaces the latter link target, but not the link text.

Mark also supports macro definitions, which are defined as regexps which will
be replaced with specified template:
Expand Down
14 changes: 14 additions & 0 deletions pkg/mark/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {
)

found = true

// Recover occurrences of "[replace]" (needle in brackets) to prevent link texts of links to basename-only attachments
// like "[Attachment.txt](Attachment.txt)" from getting replaced as well.
toBrackets := append(append([]byte("["), to...), "]"...)
fromBrackets := append(append([]byte("["), from...), "]"...)
if bytes.Contains(markdown, toBrackets) {
log.Debugf(nil, "recovering link text: %q -> %q", toBrackets, fromBrackets)

markdown = bytes.ReplaceAll(
markdown,
toBrackets,
fromBrackets,
)
}
}

if !found {
Expand Down