diff options
author | Bjørn Erik Pedersen <[email protected]> | 2023-08-07 18:56:30 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-08-08 09:13:17 +0200 |
commit | d139f30234cbc60fef8a49611189519d40336dbe (patch) | |
tree | 4cf06ab0626007dd247c5f71e8b10b9f4c2d3811 /commands | |
parent | b1b69124122e50d15ba1e4b4c2cc3e13fd55e1f8 (diff) | |
download | hugo-d139f30234cbc60fef8a49611189519d40336dbe.tar.gz hugo-d139f30234cbc60fef8a49611189519d40336dbe.zip |
docs: Replace docs.json with docs.yaml
Updates #11345
Diffstat (limited to 'commands')
-rw-r--r-- | commands/gen.go | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/commands/gen.go b/commands/gen.go index 02df720c8..4b96ffc5c 100644 --- a/commands/gen.go +++ b/commands/gen.go @@ -14,6 +14,7 @@ package commands import ( + "bytes" "context" "encoding/json" "fmt" @@ -34,6 +35,7 @@ import ( "github.com/gohugoio/hugo/parser" "github.com/spf13/cobra" "github.com/spf13/cobra/doc" + "gopkg.in/yaml.v2" ) func newGenCommand() *genCommand { @@ -188,16 +190,8 @@ url: %s run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error { r.Println("Generate docs data to", docsHelperTarget) - targetFile := filepath.Join(docsHelperTarget, "docs.json") - - f, err := os.Create(targetFile) - if err != nil { - return err - } - defer f.Close() - - enc := json.NewEncoder(f) - enc.SetIndent("", " ") + var buf bytes.Buffer + jsonEnc := json.NewEncoder(&buf) configProvider := func() docshelper.DocProvider { conf := hugolib.DefaultConfig() @@ -207,7 +201,25 @@ url: %s } docshelper.AddDocProviderFunc(configProvider) - if err := enc.Encode(docshelper.GetDocProvider()); err != nil { + if err := jsonEnc.Encode(docshelper.GetDocProvider()); err != nil { + return err + } + + // Decode the JSON to a map[string]interface{} and then unmarshal it again to the correct format. + var m map[string]interface{} + if err := json.Unmarshal(buf.Bytes(), &m); err != nil { + return err + } + + targetFile := filepath.Join(docsHelperTarget, "docs.yaml") + + f, err := os.Create(targetFile) + if err != nil { + return err + } + defer f.Close() + yamlEnc := yaml.NewEncoder(f) + if err := yamlEnc.Encode(m); err != nil { return err } |