ReplaceExt replaces the extension of `fp` with `ext` if the extension of `fp` is in `formats`. This is used in places where we need to normalize file extensions (e.g., `foo.mdx` -> `foo.md`) in order to respect format associations.
(fp string, formats map[string]string)
| 24 | // This is used in places where we need to normalize file extensions (e.g., |
| 25 | // `foo.mdx` -> `foo.md`) in order to respect format associations. |
| 26 | func ReplaceFileExt(fp string, formats map[string]string) string { |
| 27 | var ext string |
| 28 | |
| 29 | old := filepath.Ext(fp) |
| 30 | if normed, found := formats[strings.Trim(old, ".")]; found { |
| 31 | ext = "." + normed |
| 32 | fp = fp[0:len(fp)-len(old)] + ext |
| 33 | } |
| 34 | |
| 35 | return fp |
| 36 | } |