From 81d77620c6ce0462f2096311cd645145cb154809 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Sat, 16 May 2026 15:26:21 -0700 Subject: [PATCH] commands: Fix import from Jekyll - Look for _config.yml, _config.yaml, or _config.toml - Fix theme submodule URL - Fix config filename in the instructions - Add tests Closes #14795 Closes #14906 --- commands/import.go | 57 +++++++++++++++----------- testscripts/commands/import_jekyll.txt | 53 +++++++++++++++++++++--- 2 files changed, 80 insertions(+), 30 deletions(-) diff --git a/commands/import.go b/commands/import.go index 8d36fb923..e07ebedeb 100644 --- a/commands/import.go +++ b/commands/import.go @@ -466,41 +466,48 @@ func (c *importCommand) importFromJekyll(args []string) error { c.r.Println("Now, start Hugo by yourself:") c.r.Println("cd " + args[1]) c.r.Println("git init") - c.r.Println("git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke themes/ananke") - c.r.Println("echo \"theme = 'ananke'\" > hugo.toml") + c.r.Println("git submodule add https://github.com/gohugo-ananke/ananke themes/ananke") + c.r.Println("echo \"theme: ananke\" >> hugo.yaml") c.r.Println("hugo server") return nil } func (c *importCommand) loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]any { - path := filepath.Join(jekyllRoot, "_config.yml") + for _, candidate := range []struct { + filename string + format metadecoders.Format + }{ + {"_config.yml", metadecoders.YAML}, + {"_config.yaml", metadecoders.YAML}, + {"_config.toml", metadecoders.TOML}, + } { + path := filepath.Join(jekyllRoot, candidate.filename) + exists, err := helpers.Exists(path, fs) + if err != nil || !exists { + continue + } - exists, err := helpers.Exists(path, fs) + f, err := fs.Open(path) + if err != nil { + continue + } + b, err := io.ReadAll(f) + f.Close() + if err != nil { + continue + } - if err != nil || !exists { - c.r.Println("_config.yaml not found: Is the specified Jekyll root correct?") - return nil + m, err := metadecoders.Default.UnmarshalToMap(b, candidate.format) + if err != nil { + continue + } + + return m } - f, err := fs.Open(path) - if err != nil { - return nil - } - - defer f.Close() - - b, err := io.ReadAll(f) - if err != nil { - return nil - } - - m, err := metadecoders.Default.UnmarshalToMap(b, metadecoders.YAML) - if err != nil { - return nil - } - - return m + c.r.Println("no config file (_config.yml, _config.yaml, or _config.toml) found: is the specified Jekyll root correct?") + return nil } func (c *importCommand) parseJekyllFilename(filename string) (time.Time, string, error) { diff --git a/testscripts/commands/import_jekyll.txt b/testscripts/commands/import_jekyll.txt index b38d62f26..b79a0face 100644 --- a/testscripts/commands/import_jekyll.txt +++ b/testscripts/commands/import_jekyll.txt @@ -6,12 +6,55 @@ stdout 'Import a project from another system' hugo import jekyll -h stdout 'hugo import from Jekyll\.' -hugo import jekyll my-jekyll-project my-hugo-project -checkfilecount 1 my-hugo-project/content/post -grep 'example\.org' my-hugo-project/hugo.yaml +# No config file +hugo import jekyll jekyll-no-config hugo-no-config +checkfilecount 1 hugo-no-config/content/post +grep 'example\.org' hugo-no-config/hugo.yaml -# A simple Jekyll project. --- my-jekyll-project/_posts/2012-01-18-hello-world.markdown -- +# Has _config.yml file +hugo import jekyll jekyll-yml hugo-yml +checkfilecount 1 hugo-yml/content/post +grep 'https://yml\.example\.org/' hugo-yml/hugo.yaml + +# Has _config.yaml file +hugo import jekyll jekyll-yaml hugo-yaml +checkfilecount 1 hugo-yaml/content/post +grep 'https://yaml\.example\.org/' hugo-yaml/hugo.yaml + +# Has _config.toml file +hugo import jekyll jekyll-toml hugo-toml +checkfilecount 1 hugo-toml/content/post +grep 'https://toml\.example\.org/' hugo-toml/hugo.yaml + +# Jekyll projects used by the tests above. +-- jekyll-no-config/_posts/2012-01-18-hello-world.markdown -- +--- +layout: post +title: "Hello World" +--- +Hello world! +-- jekyll-yml/_config.yml -- +title: "Jekyll YML Site" +url: "https://yml.example.org/" +-- jekyll-yml/_posts/2012-01-18-hello-world.markdown -- +--- +layout: post +title: "Hello World" +--- +Hello world! +-- jekyll-yaml/_config.yaml -- +title: "Jekyll YAML Site" +url: "https://yaml.example.org/" +-- jekyll-yaml/_posts/2012-01-18-hello-world.markdown -- +--- +layout: post +title: "Hello World" +--- +Hello world! +-- jekyll-toml/_config.toml -- +title = "Jekyll TOML Site" +url = "https://toml.example.org/" +-- jekyll-toml/_posts/2012-01-18-hello-world.markdown -- --- layout: post title: "Hello World"