Skip to content

Commit

Permalink
use either node or default frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
swilen-iwanow committed Nov 17, 2020
1 parent e0153c3 commit 16d0105
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .ci/build
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ cd ${SOURCE_PATH}
###############################################################################

EFFECTIVE_VERSION_FILE="${VERSION_PATH}/version"
if [[ -f ${EFFECTIVE_VERSION_FILE} ]]
then
if [[ -f ${EFFECTIVE_VERSION_FILE} ]]; then
VERSION_FILE="${EFFECTIVE_VERSION_FILE}"
else
VERSION_FILE="$(${READLINK_BIN} -f "${SOURCE_PATH}/VERSION")"
Expand Down Expand Up @@ -74,5 +73,6 @@ else
go build \
-v \
-o ${BINARY_PATH}/docforge \
-ldflags "-w -X github.com/gardener/docforge/pkg/version.Version=$(git rev-parse HEAD)" \
cmd/*.go
fi
31 changes: 13 additions & 18 deletions pkg/processors/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package processors

import (
"bytes"
"io/ioutil"
"strings"

"github.com/gardener/docforge/pkg/markdown"
Expand All @@ -25,6 +23,7 @@ func (f *FrontMatter) Process(documentBlob []byte, node *api.Node) ([]byte, erro
nodeFmBytes, fmBytes, content []byte
props, fm, docFm map[string]interface{}
ok bool
err error
)
// Frontmatter from node
if props = node.Properties; props == nil {
Expand All @@ -45,11 +44,6 @@ func (f *FrontMatter) Process(documentBlob []byte, node *api.Node) ([]byte, erro
}
}

nodeFmBytes, err := yaml.Marshal(fm)
if err != nil {
return nil, err
}

// document front matter
if fmBytes, content, err = markdown.StripFrontMatter(documentBlob); err != nil {
return nil, err
Expand All @@ -59,21 +53,22 @@ func (f *FrontMatter) Process(documentBlob []byte, node *api.Node) ([]byte, erro
return nil, err
}

for propertyKey, propertyValue := range docFm {
if _, ok := fm[propertyKey]; !ok {
fm[propertyKey] = propertyValue
}
}

nodeFmBytes, err = yaml.Marshal(fm)
if err != nil {
return nil, err
}

// TODO: merge node + doc frontmatter per configurable strategy:
// - merge where node frontmatter entries win over document frontmatter
// - merge where document frontmatter entries win over node frontmatter
// - merge where document frontmatter are merged with node frontmatter ignoring duplicates (currently impl.)
buf := bytes.NewBuffer([]byte{})
if fmBytes != nil {
buf.Write(fmBytes)
}
if nodeFmBytes != nil {
buf.Write(nodeFmBytes)
}
if fmBytes, err = ioutil.ReadAll(buf); err != nil {
return nil, err
}
if documentBlob, err = markdown.InsertFrontMatter(fmBytes, content); err != nil {
if documentBlob, err = markdown.InsertFrontMatter(nodeFmBytes, content); err != nil {
return nil, err
}
return documentBlob, nil
Expand Down
1 change: 0 additions & 1 deletion pkg/processors/frontmatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ title: Test1
},
wantErr: nil,
wantDocument: `---
title: Test1
title: Test2
---
`,
Expand Down

0 comments on commit 16d0105

Please sign in to comment.