Skip to content

Commit 2963074

Browse files
committed
fix: meta extraction at transformer
1 parent 4075a86 commit 2963074

File tree

7 files changed

+64
-6
lines changed

7 files changed

+64
-6
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/yuin/goldmark v1.7.1
1212
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
1313
github.com/yuin/gopher-lua v1.1.0
14+
gopkg.in/yaml.v3 v3.0.1
1415
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
1516
)
1617

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,6 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
191191
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
192192
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
193193
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
194+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
194195
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf h1:rRz0YsF7VXj9fXRF6yQgFI7DzST+hsI3TeFSGupntu0=
195196
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf/go.mod h1:ivKkcY8Zxw5ba0jldhZCYYQfGdb2K6u9tbYK1AwMIBc=

pkg/alvu/alvu.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,33 @@ func runTransfomers(filesToProcess []string, ac *AlvuConfig) ([]transformers.Tra
299299
}
300300

301301
originalContent, err := os.ReadFile(fileToNormalize)
302+
mutableContent := originalContent
302303
if err != nil {
303304
return nil, fmt.Errorf("failed to read file %v with error %v", fileToNormalize, err)
304305
}
305306

307+
var meta map[string]interface{}
306308
for _, transformer := range ac.Transformers[extension] {
307-
nextContent, err := transformer.TransformContent(originalContent)
309+
nextContent, err := transformer.TransformContent(mutableContent)
308310
if err != nil {
309311
return nil, fmt.Errorf("failed to transform file: %v, with error: %v", fileToNormalize, err)
310312
}
311-
originalContent = nextContent
313+
newMeta, _, _ := transformer.ExtractMeta(originalContent)
314+
if err != nil {
315+
return nil, fmt.Errorf("failed to extract meta from file: %v, with error: %v", fileToNormalize, err)
316+
}
317+
if hasKeys(newMeta) {
318+
meta = newMeta
319+
}
320+
mutableContent = nextContent
312321
}
313322

314-
transformedFile, err := ac.createTransformedFile(fileToNormalize, string(originalContent))
323+
transformedFile, err := ac.createTransformedFile(fileToNormalize, string(mutableContent))
315324
if err != nil {
316325
return nil, fmt.Errorf("failed to transform file: %v, with error: %v", fileToNormalize, err)
317326
}
318327

328+
transformedFile.Meta = meta
319329
normalizedFiles = append(normalizedFiles, transformedFile)
320330
}
321331
return normalizedFiles, nil
@@ -493,3 +503,11 @@ func injectInLegacySlot(htmlString string, replacement string) string {
493503
}
494504
return strings.Replace(htmlString, contentTag, replacement, contentTagPos)
495505
}
506+
507+
func hasKeys(i map[string]interface{}) bool {
508+
keys := make([]string, 0, len(i))
509+
for k := range i {
510+
keys = append(keys, k)
511+
}
512+
return len(keys) > 0
513+
}

pkg/alvu/hooks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,19 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
169169
)
170170
fileTargetName = filepath.Clean(strings.TrimPrefix(fileTargetName, "/"))
171171
fileTargetName = strings.Replace(fileTargetName, filepath.Ext(fileTargetName), ".html", 1)
172+
fileTargetName = strings.TrimSpace(fileTargetName)
172173

173174
hookInput := struct {
174175
Name string `json:"name"`
175176
SourcePath string `json:"source_path"`
176177
// DestPath string `json:"dest_path"`
177-
// Meta map[string]interface{} `json:"meta"`
178-
WriteableContent string `json:"content"`
178+
Meta map[string]interface{} `json:"meta"`
179+
WriteableContent string `json:"content"`
179180
// HTMLContent string `json:"html"`
180181
}{
181182
Name: fileTargetName,
182183
SourcePath: file.SourcePath,
184+
Meta: file.Meta,
183185
}
184186

185187
localCollection := []*HookSource{}

transformers/html/html.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ func (mt *HTMLTransformer) TransformContent(input []byte) (result []byte, err er
77
return
88
}
99

10+
func (mt *HTMLTransformer) ExtractMeta(input []byte) (result map[string]interface{}, content []byte, err error) {
11+
result = map[string]interface{}{}
12+
return
13+
}
14+
1015
func (mt *HTMLTransformer) Init() {}

transformers/markdown/markdown.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/yuin/goldmark/renderer/html"
1717
"github.com/yuin/goldmark/text"
1818
"github.com/yuin/goldmark/util"
19+
"gopkg.in/yaml.v3"
1920
)
2021

2122
type MarkdownTransformer struct {
@@ -30,7 +31,13 @@ func (mt *MarkdownTransformer) TransformContent(input []byte) (result []byte, er
3031
mt.EnsureProcessor()
3132

3233
var buffer bytes.Buffer
33-
err = mt.processor.Convert(input, &buffer)
34+
_, content, err := mt.ExtractMeta(input)
35+
36+
if err != nil {
37+
return
38+
}
39+
40+
err = mt.processor.Convert(content, &buffer)
3441
if err != nil {
3542
return
3643
}
@@ -39,6 +46,28 @@ func (mt *MarkdownTransformer) TransformContent(input []byte) (result []byte, er
3946
return
4047
}
4148

49+
func (mt *MarkdownTransformer) ExtractMeta(input []byte) (result map[string]interface{}, content []byte, err error) {
50+
result = map[string]interface{}{}
51+
sep := []byte("---")
52+
53+
content = input
54+
55+
if !bytes.HasPrefix(input, sep) {
56+
return
57+
}
58+
59+
metaParts := bytes.SplitN(content, sep, 3)
60+
if len(metaParts) > 2 {
61+
fmt.Printf("metaParts: %v\n", metaParts)
62+
err = yaml.Unmarshal([]byte(metaParts[1]), &result)
63+
if err != nil {
64+
return
65+
}
66+
content = metaParts[2]
67+
}
68+
return
69+
}
70+
4271
func (mt *MarkdownTransformer) EnsureProcessor() {
4372
if mt.processor != nil {
4473
return

transformers/transformers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ type TransformedFile struct {
44
SourcePath string
55
TransformedFile string
66
Extension string
7+
Meta map[string]interface{}
78
}
89

910
type Transfomer interface {
1011
TransformContent(data []byte) ([]byte, error)
12+
ExtractMeta(data []byte) (map[string]interface{}, []byte, error)
1113
}

0 commit comments

Comments
 (0)