mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-11-15 23:00:10 +00:00
Harvey Tindall
a0a25d64f1
doesn't work any better, but more efficient and doesn't require eyebleach after viewing.
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
|
|
stripmd "github.com/writeas/go-strip-markdown"
|
|
)
|
|
|
|
// StripAltText removes Markdown alt text from links and images and replaces them with just the URL.
|
|
// Currently uses the deepest alt text when links/images are nested.
|
|
func StripAltText(md string) string {
|
|
altTextStart := -1 // Start of alt text (between '[' & ']')
|
|
URLStart := -1 // Start of url (between '(' & ')')
|
|
URLEnd := -1
|
|
previousURLEnd := -2
|
|
out := ""
|
|
for i := range md {
|
|
if altTextStart != -1 && URLStart != -1 && md[i] == ')' {
|
|
URLEnd = i - 1
|
|
out += md[previousURLEnd+2:altTextStart-1] + md[URLStart:URLEnd+1]
|
|
previousURLEnd = URLEnd
|
|
altTextStart, URLStart, URLEnd = -1, -1, -1
|
|
continue
|
|
}
|
|
if md[i] == '[' && altTextStart == -1 {
|
|
altTextStart = i + 1
|
|
if i > 0 && md[i-1] == '!' {
|
|
altTextStart--
|
|
}
|
|
}
|
|
if i > 0 && md[i-1] == ']' && md[i] == '(' && URLStart == -1 {
|
|
URLStart = i + 1
|
|
}
|
|
}
|
|
if previousURLEnd+1 != len(md)-1 {
|
|
out += md[previousURLEnd+2:]
|
|
}
|
|
if out == "" {
|
|
return md
|
|
}
|
|
return out
|
|
}
|
|
|
|
func stripMarkdown(md string) string {
|
|
return strings.TrimPrefix(strings.TrimSuffix(stripmd.Strip(StripAltText(md)), "</p>"), "<p>")
|
|
}
|