Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Sep 2, 2023
1 parent e9fd601 commit 2b01894
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/adrg/frontmatter v0.2.0
github.com/alecthomas/kong v0.8.0
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/fsnotify/fsnotify v1.6.0
github.com/gosimple/slug v1.13.1
github.com/labstack/echo/v4 v4.11.1
github.com/onsi/ginkgo/v2 v2.12.0
Expand All @@ -16,13 +17,13 @@ require (
github.com/yuin/goldmark-emoji v1.0.2
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
go.abhg.dev/goldmark/mermaid v0.4.0
go.uber.org/atomic v1.11.0
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
Expand All @@ -35,7 +36,6 @@ require (
github.com/stretchr/testify v1.8.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/net v0.14.0 // indirect
Expand Down
33 changes: 16 additions & 17 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,18 @@ func (r *Render) renderMarkdown(match string, funcMap template.FuncMap, layout *
return fmt.Errorf("could not render layout template (%s): %w", match, err)
}

newFilename := strings.Replace(match, r.sourcePath, r.buildPath, 1)
newFilename = strings.Replace(newFilename, ".md", ".html", 1)
withoutSlugFilename := strings.Replace(match, r.sourcePath, r.buildPath, 1)
withoutSlugFilename = strings.Replace(withoutSlugFilename, ".md", ".html", 1)
filenames := []string{withoutSlugFilename}

err = writeFile(newFilename, layoutWriter.String())
if err != nil {
return fmt.Errorf("could write new template (%s): %w", newFilename, err)
if !strings.Contains(withoutSlugFilename, "index.html") {
withSlugFilename := strings.Replace(withoutSlugFilename, ".html", "-"+slug.Make(viewDoc.Title())+".html", 1)
filenames = append(filenames, withSlugFilename)
}

if !strings.Contains(newFilename, "index.html") {
newFilename = strings.Replace(newFilename, ".html", "-"+slug.Make(viewDoc.Title())+".html", 1)

err = writeFile(newFilename, layoutWriter.String())
if err != nil {
return fmt.Errorf("could write new template (%s): %w", newFilename, err)
}
err = writeHTMLFiles(filenames, layoutWriter.String())
if err != nil {
return fmt.Errorf("could write new template: %w", err)
}

return nil
Expand All @@ -204,17 +201,19 @@ func readFile(filename string) (string, error) {
return string(contents), nil
}

func writeFile(filename, contents string) error {
dirPath := filepath.Dir(filename)
func writeHTMLFiles(filenames []string, contents string) error {
dirPath := filepath.Dir(filenames[0])

err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return fmt.Errorf("could not create path (%s): %w", dirPath, err)
}

err = os.WriteFile(filename, []byte(contents), os.ModePerm)
if err != nil {
return fmt.Errorf("could not write path (%s): %w", filename, err)
for _, filename := range filenames {
err = os.WriteFile(filename, []byte(contents), os.ModePerm)
if err != nil {
return fmt.Errorf("could not write path (%s): %w", filename, err)
}
}

return nil
Expand Down

0 comments on commit 2b01894

Please sign in to comment.