commands: Fix convert command

Closes #15012
This commit is contained in:
Joe Mooring
2026-06-08 23:56:51 -07:00
committed by Bjørn Erik Pedersen
parent 72495f9fba
commit 2602796cf1
2 changed files with 135 additions and 36 deletions
+31 -3
View File
@@ -263,11 +263,39 @@ func (c *convertCommand) convertContents(format metadecoders.Format) error {
site := c.h.Sites[0]
var pagesBackedByFile page.Pages
for _, p := range site.AllPages() {
workingDir := c.h.Sites[0].Deps.Conf.WorkingDir() + string(filepath.Separator)
isConvertible := func(p page.Page) bool {
// Skip pages not backed by a content file.
if p.File() == nil {
return false
}
// Skip content adapters.
if p.File().IsContentAdapter() {
return false
}
// Skip content files provided by modules, including vendored modules.
if !p.File().FileInfo().Meta().IsProject {
return false
}
// Skip content files in project mounts outside the working directory.
if !strings.HasPrefix(p.File().Filename(), workingDir) {
return false
}
return true
}
seen := make(map[string]bool)
var pagesBackedByFile page.Pages
for _, p := range c.h.Pages() {
if !isConvertible(p) {
continue
}
filename := p.File().Filename()
if seen[filename] {
continue
}
seen[filename] = true
pagesBackedByFile = append(pagesBackedByFile, p)
}
@@ -278,7 +306,7 @@ func (c *convertCommand) convertContents(format metadecoders.Format) error {
}
site.Log.Println("processing", len(pagesBackedByFile), "content files")
for _, p := range site.AllPages() {
for _, p := range pagesBackedByFile {
if err := c.convertAndSavePage(p, site, format); err != nil {
return err
}
+104 -33
View File
@@ -9,50 +9,121 @@ stdout 'to use TOML for the front matter'
hugo convert toYAML -h
stdout 'to use YAML for the front matter'
hugo convert toJSON -o myjsoncontent
stdout 'processing 4 content files'
grep '^{' myjsoncontent/content/mytoml.md
grep '^{' myjsoncontent/content/myjson.md
grep '^{' myjsoncontent/content/myyaml.md
grep '^{' myjsoncontent/content/bundle/index.md
exists myjsoncontent/content/bundle/data.txt
exists myjsoncontent/content/bundle/nested/asset.dat
hugo convert toYAML -o myyamlcontent
stdout 'processing 4 content files'
exists myyamlcontent/content/bundle/data.txt
exists myyamlcontent/content/bundle/nested/asset.dat
hugo convert toTOML -o mytomlcontent
stdout 'processing 4 content files'
exists mytomlcontent/content/bundle/data.txt
exists mytomlcontent/content/bundle/nested/asset.dat
cd project
# toJSON
hugo convert toJSON -o output/json
stdout 'processing 6 content files'
! stderr .
grep '^{' output/json/content/json.fr.md
grep '^{' output/json/content/toml.en.md
grep '^{' output/json/content/yaml.md
grep '^{' output/json/content/bundle/index.en.md
grep '^{' output/json/content/bundle/index.fr.md
grep '^{' output/json/internal/internal-mount.md
exists output/json/content/bundle/data.txt
exists output/json/content/bundle/nested/asset.dat
exists output/json/content/_content.gotmpl
! exists output/json/external
# toTOML
hugo convert toTOML -o output/toml
stdout 'processing 6 content files'
! stderr .
grep '^\+\+\+' output/toml/content/json.fr.md
grep '^\+\+\+' output/toml/content/toml.en.md
grep '^\+\+\+' output/toml/content/yaml.md
grep '^\+\+\+' output/toml/content/bundle/index.en.md
grep '^\+\+\+' output/toml/content/bundle/index.fr.md
grep '^\+\+\+' output/toml/internal/internal-mount.md
exists output/toml/content/bundle/data.txt
exists output/toml/content/bundle/nested/asset.dat
exists output/toml/content/_content.gotmpl
! exists output/toml/external
# toYAML
hugo convert toYAML -o output/yaml
stdout 'processing 6 content files'
! stderr .
grep '^---' output/yaml/content/json.fr.md
grep '^---' output/yaml/content/toml.en.md
grep '^---' output/yaml/content/yaml.md
grep '^---' output/yaml/content/bundle/index.en.md
grep '^---' output/yaml/content/bundle/index.fr.md
grep '^---' output/yaml/internal/internal-mount.md
exists output/yaml/content/bundle/data.txt
exists output/yaml/content/bundle/nested/asset.dat
exists output/yaml/content/_content.gotmpl
! exists output/yaml/external
-- hugo.toml --
baseURL = "http://example.org/"
-- content/mytoml.md --
+++
title = "TOML"
+++
TOML content
-- content/myjson.md --
-- project/hugo.toml --
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
[languages]
[languages.en]
weight = 1
[languages.fr]
weight = 2
[[module.mounts]]
source = 'content'
target = 'content'
[[module.mounts]]
source = 'internal'
target = 'content'
[module.mounts.sites.matrix]
languages = ['en']
[[module.mounts]]
source = '../external'
target = 'content'
[module.mounts.sites.matrix]
languages = ['en']
-- project/layouts/page.html --
{{ .Title }}
-- project/content/_content.gotmpl --
{{ $content := dict "mediaType" "text/markdown" "value" "Content created by content adapter" }}
{{ .AddPage (dict "path" "created-by-content-adapter" "title" "Created by content adapter" "content" $content) }}
{{ .EnableAllDimensions }}
-- project/content/json.fr.md --
{
"title": "JSON"
}
JSON content
-- content/myyaml.md --
-- project/content/toml.en.md --
+++
title = 'TOML'
+++
TOML content
-- project/content/yaml.md --
---
title: YAML
sites:
matrix:
languages: ['**']
---
YAML content
-- content/bundle/index.md --
-- project/content/bundle/index.en.md --
---
title: Bundle
title: Bundle EN
---
Bundle content
-- content/bundle/data.txt --
Bundle EN content
-- project/content/bundle/index.fr.md --
---
title: Bundle FR
---
Bundle FR content
-- project/content/bundle/data.txt --
bundle resource
-- content/bundle/nested/asset.dat --
-- project/content/bundle/nested/asset.dat --
nested resource
-- project/internal/internal-mount.md --
---
title: Internal Mount
---
Internal mount content
-- external/external-mount.md --
---
title: External Mount
---
External mount content