Skip to content

Commit

Permalink
fix(hooks): overlapping of content
Browse files Browse the repository at this point in the history
fixes a bug where if 2 hooks replaced the content of the same file it'd fail
  • Loading branch information
barelyhuman committed Apr 26, 2024
1 parent de61584 commit e19586f
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions pkg/alvu/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
fileData, _ := os.ReadFile(file.TransformedFile)
hookedFile.content = fileData

fileTargetName := strings.TrimPrefix(
file.SourcePath,
filepath.Join(h.ac.RootPath, "pages"),
)
fileTargetName = filepath.Clean(strings.TrimPrefix(fileTargetName, "/"))
fileTargetName = strings.Replace(fileTargetName, filepath.Ext(fileTargetName), ".html", 1)

hookInput := struct {
Name string `json:"name"`
SourcePath string `json:"source_path"`
Expand All @@ -169,17 +176,14 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
WriteableContent string `json:"content"`
// HTMLContent string `json:"html"`
}{
Name: filepath.Clean(strings.TrimPrefix(strings.TrimPrefix(file.SourcePath, filepath.Join(h.ac.RootPath, "pages")), "/")),
SourcePath: file.SourcePath,
WriteableContent: string(fileData),
Name: fileTargetName,
SourcePath: file.SourcePath,
}

hookJsonInput, _ := json.Marshal(hookInput)

localCollection := []*HookSource{}

filePathSplits := strings.Split(file.SourcePath, string(filepath.Separator))
nonRootPath := filepath.Join(filePathSplits[1:]...)
nonRootPath := strings.TrimPrefix(file.SourcePath, filepath.Join(h.ac.RootPath, "pages"))
nonRootPath = strings.TrimPrefix(nonRootPath, "/")

if len(h.forSpecificFiles[nonRootPath]) > 0 {
localCollection = append(localCollection, h.forSpecificFiles[nonRootPath]...)
Expand All @@ -194,6 +198,9 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
hook := localCollection[i]
hookFunc := hook.luaState.GetGlobal("Writer")

hookInput.WriteableContent = string(hookedFile.content)
hookJsonInput, _ := json.Marshal(hookInput)

if hookFunc == lua.LNil {
continue
}
Expand Down Expand Up @@ -224,6 +231,9 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke

if fromPlug["transform"] != nil {
hookedFile.transform = fmt.Sprintf("%v", fromPlug["transform"])
} else {
h.ac.logger.Warning("Auto transformation of content returned from the hooks will be removed in v0.3,\n please return a `transform` property from the hooks instead.")
hookedFile.transform = ".md"
}

if fromPlug["data"] != nil {
Expand Down

0 comments on commit e19586f

Please sign in to comment.