Skip to content

Commit

Permalink
fix: meta extraction at transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Apr 30, 2024
1 parent 4075a86 commit 2963074
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/yuin/goldmark v1.7.1
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
github.com/yuin/gopher-lua v1.1.0
gopkg.in/yaml.v3 v3.0.1
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,6 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf h1:rRz0YsF7VXj9fXRF6yQgFI7DzST+hsI3TeFSGupntu0=
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf/go.mod h1:ivKkcY8Zxw5ba0jldhZCYYQfGdb2K6u9tbYK1AwMIBc=
24 changes: 21 additions & 3 deletions pkg/alvu/alvu.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,23 +299,33 @@ func runTransfomers(filesToProcess []string, ac *AlvuConfig) ([]transformers.Tra
}

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

var meta map[string]interface{}
for _, transformer := range ac.Transformers[extension] {
nextContent, err := transformer.TransformContent(originalContent)
nextContent, err := transformer.TransformContent(mutableContent)
if err != nil {
return nil, fmt.Errorf("failed to transform file: %v, with error: %v", fileToNormalize, err)
}
originalContent = nextContent
newMeta, _, _ := transformer.ExtractMeta(originalContent)
if err != nil {
return nil, fmt.Errorf("failed to extract meta from file: %v, with error: %v", fileToNormalize, err)
}
if hasKeys(newMeta) {
meta = newMeta
}
mutableContent = nextContent
}

transformedFile, err := ac.createTransformedFile(fileToNormalize, string(originalContent))
transformedFile, err := ac.createTransformedFile(fileToNormalize, string(mutableContent))
if err != nil {
return nil, fmt.Errorf("failed to transform file: %v, with error: %v", fileToNormalize, err)
}

transformedFile.Meta = meta
normalizedFiles = append(normalizedFiles, transformedFile)
}
return normalizedFiles, nil
Expand Down Expand Up @@ -493,3 +503,11 @@ func injectInLegacySlot(htmlString string, replacement string) string {
}
return strings.Replace(htmlString, contentTag, replacement, contentTagPos)
}

func hasKeys(i map[string]interface{}) bool {
keys := make([]string, 0, len(i))
for k := range i {
keys = append(keys, k)
}
return len(keys) > 0
}
6 changes: 4 additions & 2 deletions pkg/alvu/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,19 @@ func (h *Hooks) ProcessFile(file transformers.TransformedFile) (hookedFile Hooke
)
fileTargetName = filepath.Clean(strings.TrimPrefix(fileTargetName, "/"))
fileTargetName = strings.Replace(fileTargetName, filepath.Ext(fileTargetName), ".html", 1)
fileTargetName = strings.TrimSpace(fileTargetName)

hookInput := struct {
Name string `json:"name"`
SourcePath string `json:"source_path"`
// DestPath string `json:"dest_path"`
// Meta map[string]interface{} `json:"meta"`
WriteableContent string `json:"content"`
Meta map[string]interface{} `json:"meta"`
WriteableContent string `json:"content"`
// HTMLContent string `json:"html"`
}{
Name: fileTargetName,
SourcePath: file.SourcePath,
Meta: file.Meta,
}

localCollection := []*HookSource{}
Expand Down
5 changes: 5 additions & 0 deletions transformers/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ func (mt *HTMLTransformer) TransformContent(input []byte) (result []byte, err er
return
}

func (mt *HTMLTransformer) ExtractMeta(input []byte) (result map[string]interface{}, content []byte, err error) {
result = map[string]interface{}{}
return
}

func (mt *HTMLTransformer) Init() {}
31 changes: 30 additions & 1 deletion transformers/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"gopkg.in/yaml.v3"
)

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

var buffer bytes.Buffer
err = mt.processor.Convert(input, &buffer)
_, content, err := mt.ExtractMeta(input)

if err != nil {
return
}

err = mt.processor.Convert(content, &buffer)
if err != nil {
return
}
Expand All @@ -39,6 +46,28 @@ func (mt *MarkdownTransformer) TransformContent(input []byte) (result []byte, er
return
}

func (mt *MarkdownTransformer) ExtractMeta(input []byte) (result map[string]interface{}, content []byte, err error) {
result = map[string]interface{}{}
sep := []byte("---")

content = input

if !bytes.HasPrefix(input, sep) {
return
}

metaParts := bytes.SplitN(content, sep, 3)
if len(metaParts) > 2 {
fmt.Printf("metaParts: %v\n", metaParts)
err = yaml.Unmarshal([]byte(metaParts[1]), &result)
if err != nil {
return
}
content = metaParts[2]
}
return
}

func (mt *MarkdownTransformer) EnsureProcessor() {
if mt.processor != nil {
return
Expand Down
2 changes: 2 additions & 0 deletions transformers/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ type TransformedFile struct {
SourcePath string
TransformedFile string
Extension string
Meta map[string]interface{}
}

type Transfomer interface {
TransformContent(data []byte) ([]byte, error)
ExtractMeta(data []byte) (map[string]interface{}, []byte, error)
}

0 comments on commit 2963074

Please sign in to comment.