Skip to content

Commit

Permalink
Remove file extensions when making web path (#341).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Sep 12, 2024
1 parent eafcb16 commit d03a29d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cmd/build/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func getContent(path string, info os.FileInfo, err error, siteConfig readers.Sit
for configContentType, slug := range siteConfig.Routes {
if configContentType == contentType {
// Replace :filename.
slug = strings.Replace(slug, ":filename", strings.TrimSuffix(fileName, ".json"), -1)
slug = strings.Replace(slug, ":filename", fileName, -1)

// Replace :fields().
fieldReplacements := reField.FindAllStringSubmatch(slug, -1)
Expand Down Expand Up @@ -424,11 +424,21 @@ func makeWebPath(path string, fileName string) string {
path = strings.TrimSuffix(path, fileName)
} else {
// Remove file extension only from path for files other than index.json.
path = strings.TrimSuffix(path, ".json")
path = removeFileExtensionsFromPath(path, ".json")
}
return path
}

func removeFileExtensionsFromPath(path, ext string) string {
segments := strings.Split(path, "/")
for i, segment := range segments {
if strings.HasSuffix(segment, ext) {
segments[i] = strings.TrimSuffix(segment, ext)
}
}
return strings.Join(segments, "/")
}

func slugify(path string) string {
// Slugify output using reSlugify regex defined above.
return strings.Trim(reSlugify.ReplaceAllString(strings.ToLower(path), "-"), "-")
Expand Down

0 comments on commit d03a29d

Please sign in to comment.