Skip to content

Commit e19586f

Browse files
committed
fix(hooks): overlapping of content
fixes a bug where if 2 hooks replaced the content of the same file it'd fail
1 parent de61584 commit e19586f

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

pkg/alvu/hooks.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
161161
fileData, _ := os.ReadFile(file.TransformedFile)
162162
hookedFile.content = fileData
163163

164+
fileTargetName := strings.TrimPrefix(
165+
file.SourcePath,
166+
filepath.Join(h.ac.RootPath, "pages"),
167+
)
168+
fileTargetName = filepath.Clean(strings.TrimPrefix(fileTargetName, "/"))
169+
fileTargetName = strings.Replace(fileTargetName, filepath.Ext(fileTargetName), ".html", 1)
170+
164171
hookInput := struct {
165172
Name string `json:"name"`
166173
SourcePath string `json:"source_path"`
@@ -169,17 +176,14 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
169176
WriteableContent string `json:"content"`
170177
// HTMLContent string `json:"html"`
171178
}{
172-
Name: filepath.Clean(strings.TrimPrefix(strings.TrimPrefix(file.SourcePath, filepath.Join(h.ac.RootPath, "pages")), "/")),
173-
SourcePath: file.SourcePath,
174-
WriteableContent: string(fileData),
179+
Name: fileTargetName,
180+
SourcePath: file.SourcePath,
175181
}
176182

177-
hookJsonInput, _ := json.Marshal(hookInput)
178-
179183
localCollection := []*HookSource{}
180184

181-
filePathSplits := strings.Split(file.SourcePath, string(filepath.Separator))
182-
nonRootPath := filepath.Join(filePathSplits[1:]...)
185+
nonRootPath := strings.TrimPrefix(file.SourcePath, filepath.Join(h.ac.RootPath, "pages"))
186+
nonRootPath = strings.TrimPrefix(nonRootPath, "/")
183187

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

201+
hookInput.WriteableContent = string(hookedFile.content)
202+
hookJsonInput, _ := json.Marshal(hookInput)
203+
197204
if hookFunc == lua.LNil {
198205
continue
199206
}
@@ -224,6 +231,9 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
224231

225232
if fromPlug["transform"] != nil {
226233
hookedFile.transform = fmt.Sprintf("%v", fromPlug["transform"])
234+
} else {
235+
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.")
236+
hookedFile.transform = ".md"
227237
}
228238

229239
if fromPlug["data"] != nil {

0 commit comments

Comments
 (0)