From 49c86e78558afd39291cf66a2a1f62375c8df880 Mon Sep 17 00:00:00 2001 From: Mirco Veltri Date: Fri, 16 Sep 2022 09:49:30 +0200 Subject: [PATCH 1/8] feat(newResource): make uses of sveltekit group layout possible --- cmd/generateMenu.go | 29 +---- cmd/generateRss.go | 13 +- cmd/generateSitemap.go | 15 +-- cmd/newResource.go | 116 ++++++++++++------ cmd/root.go | 6 +- common/collections.go | 1 - config/defaults.go | 14 ++- config/menu.go | 45 ------- config/pages.go | 16 +-- config/template_data.go | 27 ++-- helpers/menu.go | 11 ++ helpers/pages.go | 29 +---- helpers/resources.go | 64 ++++++---- helpers/templates.go | 30 ++++- internal/builder/constants.go | 2 + internal/builder/menu_builder.go | 7 +- internal/builder/resource_builder.go | 3 + internal/fsm/sveltin_fsm.go | 23 +++- internal/tpltypes/menu.go | 21 ++++ internal/tpltypes/nopage.go | 21 ++++ internal/tpltypes/project.go | 24 ++++ internal/tpltypes/resource.go | 15 +++ resources/defaults.go | 1 + .../templates/resource/api/apiIndex.gotxt | 2 +- .../templates/resource/api/apiSlug.gotxt | 4 +- .../templates/resource/layout.svelte.gotxt | 5 + .../internal/templates/resource/lib.gotxt | 4 +- .../templates/resource/page.server.ts.gotxt | 4 +- .../templates/resource/page.svelte.gotxt | 4 +- .../internal/templates/resource/slug.ts.gotxt | 8 +- .../internal/templates/site/menu.js.ts.gotxt | 94 +++++++------- .../internal/templates/xml/rss.xml.gotxt | 14 ++- .../internal/templates/xml/sitemap.xml.gotxt | 30 ++--- resources/sveltin.yaml | 1 + utils/text.go | 10 ++ 35 files changed, 406 insertions(+), 307 deletions(-) delete mode 100644 config/menu.go create mode 100644 helpers/menu.go create mode 100644 internal/tpltypes/menu.go create mode 100644 internal/tpltypes/nopage.go create mode 100644 internal/tpltypes/project.go create mode 100644 internal/tpltypes/resource.go create mode 100644 resources/internal/templates/resource/layout.svelte.gotxt diff --git a/cmd/generateMenu.go b/cmd/generateMenu.go index 8bdcfd02..6c8cbbef 100644 --- a/cmd/generateMenu.go +++ b/cmd/generateMenu.go @@ -10,11 +10,8 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/sveltinio/sveltin/common" - "github.com/sveltinio/sveltin/config" "github.com/sveltinio/sveltin/helpers" "github.com/sveltinio/sveltin/helpers/factory" - "github.com/sveltinio/sveltin/internal/composer" "github.com/sveltinio/sveltin/internal/markup" "github.com/sveltinio/sveltin/resources" "github.com/sveltinio/sveltin/utils" @@ -46,37 +43,23 @@ func RunGenerateMenuCmd(cmd *cobra.Command, args []string) { cfg.log.Plain(markup.H1("Generating the menu structure file")) - projectFolder := cfg.fsManager.GetFolder(RootFolder) + cfg.log.Info("Getting list of all resources contents") + existingResources := helpers.GetAllResources(cfg.fs, cfg.sveltin.GetContentPath()) + contents := helpers.GetResourceContentMap(cfg.fs, existingResources, cfg.sveltin.GetContentPath()) - cfg.log.Info("Getting list of existing public pages") + cfg.log.Info("Getting list of all routes") allRoutes := helpers.GetAllRoutes(cfg.fs, cfg.pathMaker.GetPathToRoutes()) - allResources := helpers.GetAllResources(cfg.fs, cfg.pathMaker.GetPathToExistingResources()) - allRoutesExceptsResource := common.Difference(allRoutes, allResources) - // exclude api folder from the list - publicPages := common.Difference(allRoutesExceptsResource, []string{ApiFolder}) - - cfg.log.Info("Getting list of existing resources") - availableResources := helpers.GetAllResourcesWithContentName(cfg.fs, cfg.pathMaker.GetPathToExistingResources(), withContentFlag) // GET FOLDER: config configFolder := cfg.fsManager.GetFolder(ConfigFolder) // ADD FILE: config/menu.js cfg.log.Info("Saving the menu.js.ts file") - menuFile := &composer.File{ - Name: "menu.js.ts", - TemplateID: "menu", - TemplateData: &config.TemplateData{ - Menu: &config.MenuConfig{ - Resources: availableResources, - Pages: publicPages, - WithContent: withContentFlag, - }, - }, - } + menuFile := cfg.fsManager.NewMenuFile("menu", &cfg.project, allRoutes, contents, withContentFlag) configFolder.Add(menuFile) // SET FOLDER STRUCTURE + projectFolder := cfg.fsManager.GetFolder(RootFolder) projectFolder.Add(configFolder) // GENERATE THE FOLDER TREE diff --git a/cmd/generateRss.go b/cmd/generateRss.go index 9dc4565d..df8ad977 100644 --- a/cmd/generateRss.go +++ b/cmd/generateRss.go @@ -10,7 +10,6 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/sveltinio/sveltin/common" "github.com/sveltinio/sveltin/helpers" "github.com/sveltinio/sveltin/helpers/factory" "github.com/sveltinio/sveltin/internal/markup" @@ -38,25 +37,19 @@ func RunGenerateRSSCmd(cmd *cobra.Command, args []string) { cfg.log.Plain(markup.H1("Generating the RSS feed file")) - cfg.log.Info("Getting all existing resources") + cfg.log.Info("Getting list of all resources contents") existingResources := helpers.GetAllResources(cfg.fs, cfg.pathMaker.GetPathToExistingResources()) - - cfg.log.Info("Getting all resources contents") contents := helpers.GetResourceContentMap(cfg.fs, existingResources, cfg.sveltin.GetContentPath()) - cfg.log.Info("Getting all existing public pages") + cfg.log.Info("Getting list of all routes") allRoutes := helpers.GetAllRoutes(cfg.fs, cfg.pathMaker.GetPathToRoutes()) - allRoutesExceptsResource := common.Difference(allRoutes, existingResources) - // exclude api folder from the list - pages := common.Difference(allRoutesExceptsResource, []string{ApiFolder}) - //pages := helpers.GetAllPublicPages(cfg.fs, cfg.pathMaker.GetPathToPublicPages()) // GET FOLDER: static staticFolder := cfg.fsManager.GetFolder(StaticFolder) // NEW FILE: static/rss.xml cfg.log.Info("Saving the file to the static folder") - rssFile := cfg.fsManager.NewNoPage("rss", &cfg.project, existingResources, contents, nil, pages) + rssFile := cfg.fsManager.NewNoPageFile("rss", &cfg.project, allRoutes, contents) staticFolder.Add(rssFile) // SET FOLDER STRUCTURE diff --git a/cmd/generateSitemap.go b/cmd/generateSitemap.go index 5feb5920..5c143eaa 100644 --- a/cmd/generateSitemap.go +++ b/cmd/generateSitemap.go @@ -10,7 +10,6 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/sveltinio/sveltin/common" "github.com/sveltinio/sveltin/helpers" "github.com/sveltinio/sveltin/helpers/factory" "github.com/sveltinio/sveltin/internal/markup" @@ -38,27 +37,19 @@ func RunGenerateSitemapCmd(cmd *cobra.Command, args []string) { cfg.log.Plain(markup.H1("Generating the sitemap file")) - cfg.log.Info("Getting list of existing resources") - existingResources := helpers.GetAllResources(cfg.fs, cfg.sveltin.GetContentPath()) - cfg.log.Info("Getting list of all resources contents") + existingResources := helpers.GetAllResources(cfg.fs, cfg.sveltin.GetContentPath()) contents := helpers.GetResourceContentMap(cfg.fs, existingResources, cfg.sveltin.GetContentPath()) - cfg.log.Info("Getting list of all resources metadata") - metadata := helpers.GetResourceMetadataMap(cfg.fs, existingResources, cfg.sveltin.GetRoutesPath()) - - cfg.log.Info("Getting all existing public pages") + cfg.log.Info("Getting list of all routes") allRoutes := helpers.GetAllRoutes(cfg.fs, cfg.pathMaker.GetPathToRoutes()) - allRoutesExceptsResource := common.Difference(allRoutes, existingResources) - // exclude api folder from the list - pages := common.Difference(allRoutesExceptsResource, []string{ApiFolder}) // GET FOLDER: static staticFolder := cfg.fsManager.GetFolder(StaticFolder) // NEW FILE: static/rss.xml cfg.log.Info("Saving the file to the static folder") - sitemapFile := cfg.fsManager.NewNoPage("sitemap", &cfg.project, existingResources, contents, metadata, pages) + sitemapFile := cfg.fsManager.NewNoPageFile("sitemap", &cfg.project, allRoutes, contents) staticFolder.Add(sitemapFile) // SET FOLDER STRUCTURE diff --git a/cmd/newResource.go b/cmd/newResource.go index 357c1409..ec3be313 100644 --- a/cmd/newResource.go +++ b/cmd/newResource.go @@ -20,6 +20,7 @@ import ( "github.com/sveltinio/sveltin/internal/composer" sveltinerr "github.com/sveltinio/sveltin/internal/errors" "github.com/sveltinio/sveltin/internal/markup" + "github.com/sveltinio/sveltin/internal/tpltypes" "github.com/sveltinio/sveltin/resources" "github.com/sveltinio/sveltin/utils" @@ -28,9 +29,16 @@ import ( //============================================================================= +var ( + group string + withSlugLayout bool +) + +//============================================================================= + var newResourceCmd = &cobra.Command{ Use: "resource [name]", - Aliases: []string{"r", "route"}, + Aliases: []string{"route", "r"}, Short: "Create a new resource (route).", Long: resources.GetASCIIArt() + ` Command used to create new resources. @@ -61,27 +69,33 @@ func RunNewResourceCmd(cmd *cobra.Command, args []string) { resourceName, err := promptResourceName(args) utils.ExitIfError(err) + resourceData := &tpltypes.ResourceData{ + Name: resourceName, + Group: group, + SlugLayout: withSlugLayout, + } + // MAKE FOLDER STRUCTURE: content folder - headingText := fmt.Sprintf("Creating '%s' as resource", resourceName) + headingText := fmt.Sprintf("Creating '%s' as resource", resourceData.Name) cfg.log.Plain(markup.H1(headingText)) - contentFolder, err := makeResourceFolderStructure(ContentFolder, resourceName, cfg) + contentFolder, err := makeResourceFolderStructure(ContentFolder, resourceData, cfg) utils.ExitIfError(err) // MAKE FOLDER STRUCTURE: src/params - paramsFolder, err := makeResourceFolderStructure(ParamsFolder, resourceName, cfg) + paramsFolder, err := makeResourceFolderStructure(ParamsFolder, resourceData, cfg) utils.ExitIfError(err) // MAKE FOLDER STRUCTURE: src/lib folder - libFolder, err := makeResourceFolderStructure(LibFolder, resourceName, cfg) + libFolder, err := makeResourceFolderStructure(LibFolder, resourceData, cfg) utils.ExitIfError(err) // MAKE FOLDER STRUCTURE: src/routes//{index.svelte, index.ts, [slug].svelte, [slug].json.ts} - routesFolder, err := makeResourceFolderStructure(RoutesFolder, resourceName, cfg) + routesFolder, err := makeResourceFolderStructure(RoutesFolder, resourceData, cfg) utils.ExitIfError(err) // MAKE FOLDER STRUCTURE: src/routes/api/ folder - apiFolder, err := makeResourceFolderStructure(ApiFolder, resourceName, cfg) + apiFolder, err := makeResourceFolderStructure(ApiFolder, resourceData, cfg) utils.ExitIfError(err) // SET FOLDER STRUCTURE @@ -103,8 +117,14 @@ func RunNewResourceCmd(cmd *cobra.Command, args []string) { common.PrintHelperTextNewResource(resourceName) } +func resourceCmdFlags(cmd *cobra.Command) { + cmd.Flags().StringVarP(&group, "group", "g", "", "Group name for resource routes (https://kit.svelte.dev/docs/advanced-routing#advanced-layouts)") + cmd.Flags().BoolVarP(&withSlugLayout, "slug", "", false, "Use a different layout for the slug pages (https://kit.svelte.dev/docs/advanced-routing#advanced-layouts-layout)") +} + func init() { newCmd.AddCommand(newResourceCmd) + resourceCmdFlags(newResourceCmd) } //============================================================================= @@ -131,18 +151,18 @@ func promptResourceName(inputs []string) (string, error) { //============================================================================= -func makeResourceFolderStructure(folderName string, resourceName string, cfg appConfig) (*composer.Folder, error) { +func makeResourceFolderStructure(folderName string, resourceData *tpltypes.ResourceData, cfg appConfig) (*composer.Folder, error) { switch folderName { case ContentFolder: - return createResourceContentLocalFolder(resourceName), nil + return createResourceContentLocalFolder(resourceData), nil case ParamsFolder: - return createResourceParamsLocalFolder(), nil + return createResourceParamsLocalFolder(resourceData), nil case LibFolder: - return createResourceLibLocalFolder(resourceName), nil + return createResourceLibLocalFolder(resourceData), nil case RoutesFolder: - return createResourceRoutesLocalFolder(cfg, resourceName), nil + return createResourceRoutesLocalFolder(cfg, resourceData), nil case ApiFolder: - return createResourceAPIRoutesLocalFolder(resourceName), nil + return createResourceAPIRoutesLocalFolder(resourceData), nil default: err := errors.New("something went wrong: folder not found as mapped resource for sveltin projects") return nil, sveltinerr.NewDefaultError(err) @@ -151,33 +171,34 @@ func makeResourceFolderStructure(folderName string, resourceName string, cfg app //============================================================================= -func createResourceContentLocalFolder(resourceName string) *composer.Folder { +func createResourceContentLocalFolder(resourceData *tpltypes.ResourceData) *composer.Folder { // GET FOLDER: content folder contentFolder := cfg.fsManager.GetFolder(ContentFolder) // NEW FOLDER: content/. Here is where the "new content" command saves files cfg.log.Info("Content folder") - resourceContentFolder := composer.NewFolder(resourceName) + resourceContentFolder := composer.NewFolder(resourceData.Name) contentFolder.Add(resourceContentFolder) return contentFolder } -func createResourceLibLocalFolder(resourceName string) *composer.Folder { +func createResourceLibLocalFolder(resourceData *tpltypes.ResourceData) *composer.Folder { // GET FOLDER: src/lib folder libFolder := cfg.fsManager.GetFolder(LibFolder) // NEW FOLDER: /src/lib/ - resourceLibFolder := composer.NewFolder(resourceName) + resourceLibFolder := composer.NewFolder(resourceData.Name) // NEW FILE: src/lib//load.ts cfg.log.Info("Lib files") libFile := &composer.File{ - Name: utils.ToLibFile(resourceName), + Name: utils.ToLibFile(resourceData.Name), TemplateID: LibFolder, TemplateData: &config.TemplateData{ - Name: resourceName, - Config: cfg.sveltin, + Name: resourceData.Name, + ResourceData: resourceData, + Config: cfg.sveltin, }, } resourceLibFolder.Add(libFile) @@ -186,7 +207,7 @@ func createResourceLibLocalFolder(resourceName string) *composer.Folder { return libFolder } -func createResourceParamsLocalFolder() *composer.Folder { +func createResourceParamsLocalFolder(resourceData *tpltypes.ResourceData) *composer.Folder { cfg.log.Info("Parameters matchers") // GET FOLDER: src/params folder paramsFolder := cfg.fsManager.GetFolder(ParamsFolder) @@ -196,7 +217,8 @@ func createResourceParamsLocalFolder() *composer.Folder { Name: "string.js", TemplateID: StringMatcher, TemplateData: &config.TemplateData{ - Config: cfg.sveltin, + ResourceData: resourceData, + Config: cfg.sveltin, }, } // Add file to folder @@ -207,8 +229,9 @@ func createResourceParamsLocalFolder() *composer.Folder { Name: "slug.js", TemplateID: GenericMatcher, TemplateData: &config.TemplateData{ - Name: "slug", - Config: cfg.sveltin, + Name: "slug", + ResourceData: resourceData, + Config: cfg.sveltin, }, } // Add file to folder @@ -217,12 +240,12 @@ func createResourceParamsLocalFolder() *composer.Folder { return paramsFolder } -func createResourceRoutesLocalFolder(cfg appConfig, resourceName string) *composer.Folder { +func createResourceRoutesLocalFolder(cfg appConfig, resourceData *tpltypes.ResourceData) *composer.Folder { // GET FOLDER: src/routes folder routesFolder := cfg.fsManager.GetFolder(RoutesFolder) // NEW FOLDER: src/routes/ - resourceRoutesFolder := composer.NewFolder(resourceName) + resourceRoutesFolder := composer.NewFolder(resourceData.Name) // NEW FILE: src/routes//{+page.svelte, +page.server.ts} cfg.log.Info("Routes") for _, item := range []string{IndexFile, IndexEndpointFile} { @@ -230,8 +253,9 @@ func createResourceRoutesLocalFolder(cfg appConfig, resourceName string) *compos Name: helpers.GetResourceRouteFilename(item, cfg.sveltin), TemplateID: item, TemplateData: &config.TemplateData{ - Name: resourceName, - Config: cfg.sveltin, + Name: resourceData.Name, + ResourceData: resourceData, + Config: cfg.sveltin, }, } resourceRoutesFolder.Add(f) @@ -240,38 +264,53 @@ func createResourceRoutesLocalFolder(cfg appConfig, resourceName string) *compos // NEW FOLDER: src/routes//[slug] slugFolder := composer.NewFolder("[slug]") // NEW FILE: src/routes//[slug]{+page.svelte, +page.ts} - for _, item := range []string{SlugFile, SlugEndpointFile} { + slugFiles := []string{SlugFile, SlugEndpointFile} + if resourceData.SlugLayout { + slugFiles = append(slugFiles, SlugLayoutFile) + } + for _, item := range slugFiles { f := &composer.File{ Name: helpers.GetResourceRouteFilename(item, cfg.sveltin), TemplateID: item, TemplateData: &config.TemplateData{ - Name: resourceName, - Config: cfg.sveltin, + Name: resourceData.Name, + ResourceData: resourceData, + Config: cfg.sveltin, }, } slugFolder.Add(f) } resourceRoutesFolder.Add(slugFolder) - routesFolder.Add(resourceRoutesFolder) + + if utils.IsEmpty(resourceData.Group) { + routesFolder.Add(resourceRoutesFolder) + } else { + // NEW FOLDER: src/routes/(group_name)/ + resourceGroupRoutesFolder := composer.NewFolder(fmt.Sprintf("(%s)", resourceData.Group)) + resourceGroupRoutesFolder.Add(resourceRoutesFolder) + routesFolder.Add(resourceGroupRoutesFolder) + + } return routesFolder } -func createResourceAPIRoutesLocalFolder(resourceName string) *composer.Folder { +func createResourceAPIRoutesLocalFolder(resourceData *tpltypes.ResourceData) *composer.Folder { cfg.log.Info("REST endpoints") // GET FOLDER: src/routes/api/ folder apiFolder := cfg.fsManager.GetFolder(ApiFolder) // NEW FOLDER: src/routes/api// - resourceAPIFolder := composer.NewFolder(resourceName) + resourceAPIFolder := composer.NewFolder(resourceData.Name) // NEW FILE: src/routes/api///+server.ts apiFile := &composer.File{ Name: cfg.sveltin.GetAPIFilename(), TemplateID: ApiIndexFile, TemplateData: &config.TemplateData{ - Name: resourceName, - Config: cfg.sveltin, + Name: resourceData.Name, + ResourceData: resourceData, + Config: cfg.sveltin, }, } resourceAPIFolder.Add(apiFile) @@ -283,8 +322,9 @@ func createResourceAPIRoutesLocalFolder(resourceName string) *composer.Folder { Name: cfg.sveltin.GetAPIFilename(), TemplateID: ApiSlugFile, TemplateData: &config.TemplateData{ - Name: resourceName, - Config: cfg.sveltin, + Name: resourceData.Name, + ResourceData: resourceData, + Config: cfg.sveltin, }, } slugStringFolder.Add(apiSlugFile) diff --git a/cmd/root.go b/cmd/root.go index 56df678a..66c416a8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -21,6 +21,7 @@ import ( sveltinerr "github.com/sveltinio/sveltin/internal/errors" "github.com/sveltinio/sveltin/internal/fsm" "github.com/sveltinio/sveltin/internal/pathmaker" + "github.com/sveltinio/sveltin/internal/tpltypes" "github.com/sveltinio/sveltin/resources" logger "github.com/sveltinio/yinlog" "gopkg.in/yaml.v3" @@ -31,7 +32,7 @@ import ( type appConfig struct { log *logger.Logger sveltin *config.SveltinConfig - project config.ProjectConfig + project tpltypes.ProjectData pathMaker *pathmaker.SveltinPathMaker fsManager *fsm.SveltinFSManager startersMap map[string]config.StarterTemplate @@ -72,6 +73,7 @@ const ( IndexEndpointFile string = "indexendpoint" SlugFile string = "slug" SlugEndpointFile string = "slugendpoint" + SlugLayoutFile string = "sluglayout" SettingsFile string = ".sveltin-settings.yaml" DotEnvProdFile string = ".env.production" ) @@ -137,7 +139,7 @@ func initAppConfig() { cfg.fs = afero.NewOsFs() } -func loadEnvFile(filename string) (config config.ProjectConfig, err error) { +func loadEnvFile(filename string) (config tpltypes.ProjectData, err error) { currentDir, _ := os.Getwd() viper.AddConfigPath(currentDir) viper.SetConfigName(filename) diff --git a/common/collections.go b/common/collections.go index 4c36c045..9b9e5a0b 100644 --- a/common/collections.go +++ b/common/collections.go @@ -47,7 +47,6 @@ func Unique(s []string) []string { for _, elem := range s { if len(elem) != 0 { if _, value := inResult[elem]; !value { - inResult[elem] = true uniqueValues = append(uniqueValues, elem) } diff --git a/config/defaults.go b/config/defaults.go index 14fbbb5e..428a6c73 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -146,24 +146,26 @@ func (c *SveltinConfig) GetIndexPageFilename() string { return c.Pages.Index } -// GetIndexEndpointFilename returns a string representing the path to the 'index' -// file relative to the current working directory. +// GetIndexEndpointFilename returns '+page.svelte' file. func (c *SveltinConfig) GetIndexEndpointFilename() string { return c.Pages.IndexEndpoint } -// GetSlugPageFilename returns a string representing the path to the 'slug' -// file relative to the current working directory. +// GetSlugPageFilename returns '+page.svelte' filename for the slug. func (c *SveltinConfig) GetSlugPageFilename() string { return c.Pages.Slug } -// GetSlugEndpointFilename returns a string representing the path to the 'slug' -// file relative to the current working directory. +// GetSlugEndpointFilename returns '+page.ts' as filename for the slug. func (c *SveltinConfig) GetSlugEndpointFilename() string { return c.Pages.SlugEndpoint } +// GetSlugLayoutFilename returns '+layout.svelte' as filename for the slug. +func (c *SveltinConfig) GetSlugLayoutFilename() string { + return c.Pages.SlugLayout +} + // GetContentPageFilename returns a string representing the path to the 'content' folder // relative to the current working directory. func (c *SveltinConfig) GetContentPageFilename() string { diff --git a/config/menu.go b/config/menu.go deleted file mode 100644 index c5b11dda..00000000 --- a/config/menu.go +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright © 2021-present Sveltin contributors - * - * Use of this source code is governed by Apache 2.0 license - * that can be found in the LICENSE file. - */ - -// Package config ... -package config - -// MenuConfig is the struct representing a menu item. -type MenuConfig struct { - Resources []*ResourceItem - Pages []string - WithContent bool -} - -// ResourceItem is a struct representing a resource and its content as menu item. -type ResourceItem struct { - name string - contents []string -} - -// NewResourceItem returns a pointer to a ResourceItem struct. -func NewResourceItem(name string) *ResourceItem { - return &ResourceItem{ - name: name, - contents: []string{}, - } -} - -// GetName returns a string representing the resource item name. -func (r *ResourceItem) GetName() string { - return r.name -} - -// GetContents returns a slice of strings with contents for a resource. -func (r *ResourceItem) GetContents() []string { - return r.contents -} - -// AddChild appends an item to the slice of contents. -func (r *ResourceItem) AddChild(name string) { - r.contents = append(r.contents, name) -} diff --git a/config/pages.go b/config/pages.go index 4d3e704c..8cc126d0 100644 --- a/config/pages.go +++ b/config/pages.go @@ -15,19 +15,5 @@ type Pages struct { IndexEndpoint string `mapstructure:"indexendpoint"` Slug string `mapstructure:"slug"` SlugEndpoint string `mapstructure:"slugendpoint"` -} - -// NoPage is the struct representing a no-public page (sitemap and rss) for a sveltin project. -type NoPage struct { - Config *ProjectConfig - Items *NoPageItems -} - -// NoPageItems is the struct representing an item -// of no-public page (sitemap and rss) for a sveltin project. -type NoPageItems struct { - Resources []string - Content map[string][]string - Metadata map[string][]string - Pages []string + SlugLayout string `mapstructure:"sluglayout"` } diff --git a/config/template_data.go b/config/template_data.go index 87370e63..ac08ba3d 100644 --- a/config/template_data.go +++ b/config/template_data.go @@ -8,18 +8,21 @@ // Package config ... package config +import "github.com/sveltinio/sveltin/internal/tpltypes" + // TemplateData is the struct representing all the data to be passed to a template file. type TemplateData struct { - ProjectName string - NPMClient string - BaseURL string - PortNumber string - Name string - Resource string - Type string - Config *SveltinConfig - Menu *MenuConfig - NoPage *NoPage - Theme *ThemeData - Misc string + ProjectName string + NPMClient string + BaseURL string + PortNumber string + Name string + Resource string + Type string + Config *SveltinConfig + ResourceData *tpltypes.ResourceData + MenuData *tpltypes.MenuData + NoPageData *tpltypes.NoPageData + Theme *ThemeData + Misc string } diff --git a/helpers/menu.go b/helpers/menu.go new file mode 100644 index 00000000..3c3f9f11 --- /dev/null +++ b/helpers/menu.go @@ -0,0 +1,11 @@ +package helpers + +import "github.com/sveltinio/sveltin/internal/tpltypes" + +// NewMenuItems return a NoPageItems. +func NewMenuItems(resources []string, content map[string][]string) *tpltypes.MenuItems { + r := new(tpltypes.MenuItems) + r.Resources = resources + r.Content = content + return r +} diff --git a/helpers/pages.go b/helpers/pages.go index b08dd655..863392ab 100644 --- a/helpers/pages.go +++ b/helpers/pages.go @@ -9,29 +9,10 @@ package helpers import ( - "log" - - "github.com/spf13/afero" "github.com/sveltinio/sveltin/config" + "github.com/sveltinio/sveltin/internal/tpltypes" ) -// GetAllRoutes return a slice of all available public page names as string. -func GetAllRoutes(fs afero.Fs, path string) []string { - files, err := afero.ReadDir(fs, path) - if err != nil { - log.Fatalf("Something went wrong visiting the folder %s. Are you sure it exists?", path) - } - - routes := []string{} - for _, f := range files { - if f.IsDir() { - routes = append(routes, f.Name()) - } - } - - return routes -} - // GetResourceRouteFilename returns a string representing the index and slug routes for a resource. func GetResourceRouteFilename(txt string, c *config.SveltinConfig) string { switch txt { @@ -43,6 +24,8 @@ func GetResourceRouteFilename(txt string, c *config.SveltinConfig) string { return c.GetSlugPageFilename() case "slugendpoint": return c.GetSlugEndpointFilename() + case "sluglayout": + return c.GetSlugLayoutFilename() default: return "" } @@ -61,11 +44,9 @@ func PublicPageFilename(pageType string) string { } // NewNoPageItems return a NoPageItems. -func NewNoPageItems(resources []string, content map[string][]string, metadata map[string][]string, pages []string) *config.NoPageItems { - r := new(config.NoPageItems) +func NewNoPageItems(resources []string, content map[string][]string) *tpltypes.NoPageItems { + r := new(tpltypes.NoPageItems) r.Resources = resources r.Content = content - r.Metadata = metadata - r.Pages = pages return r } diff --git a/helpers/resources.go b/helpers/resources.go index 3c08b00e..cf9f13d7 100644 --- a/helpers/resources.go +++ b/helpers/resources.go @@ -10,7 +10,9 @@ package helpers import ( "log" + "os" "path/filepath" + "regexp" "strings" "github.com/spf13/afero" @@ -45,36 +47,52 @@ func GetAllResources(fs afero.Fs, path string) []string { return resources } -// GetAllResourcesWithContentName traverses the resource contents and returns a slice of ResourceItem. -func GetAllResourcesWithContentName(fs afero.Fs, path string, children bool) []*config.ResourceItem { - var result []*config.ResourceItem - exists, _ := afero.DirExists(fs, path) - if exists { - files, err := afero.ReadDir(fs, path) - if err != nil { - log.Fatalf("Something went wrong visiting the folder %s. Are you sure it exists?", path) - } - for _, f := range files { - if f.IsDir() { - item := config.NewResourceItem(f.Name()) - if children { - subFolders, err := afero.ReadDir(fs, filepath.Join(path, f.Name())) - if err != nil { - log.Fatalf("Something went wrong visiting the subfolder %s. Are you sure it exists?", f.Name()) - } - for _, s := range subFolders { - if s.IsDir() { - item.AddChild(s.Name()) +// GetAllRoutes return a slice of all routes names as string. +func GetAllRoutes(fs afero.Fs, path string) []string { + entries := []string{} + if common.DirExists(fs, path) { + walkFunc := func(filepath string, info os.FileInfo, err error) error { + if info.IsDir() { + replacer := strings.NewReplacer(path, "", "/[slug]", "") + res := replacer.Replace(filepath) + res = strings.TrimSpace(res) + + if !strings.HasPrefix(res, "/api") { + // Match (group) name + re := regexp.MustCompile(`\/\((.*?)\)`) + if re.MatchString(res) { + submatchall := re.FindAllString(res, -1) + + for _, element := range submatchall { + element = strings.ReplaceAll(res, element, "") + element = strings.Replace(element, "/", "", 1) + + if !common.Contains(entries, element) { + entries = append(entries, element) + } + } + } else { + if !common.Contains(entries, res) { + entries = append(entries, res) } } } - result = append(result, item) - + return nil } + return nil + } + + err := afero.Walk(fs, path, walkFunc) + if err != nil { + log.Fatalf("Something went wrong visiting the folder %s. Are you sure it exists?", path) } } - return result + routes := []string{} + for _, file := range entries { + routes = append(routes, strings.Replace(file, "/", "", 1)) + } + return routes } // GetResourceContentMap returns a map of resources and relative contents. diff --git a/helpers/templates.go b/helpers/templates.go index 71868d4c..ddba59db 100644 --- a/helpers/templates.go +++ b/helpers/templates.go @@ -9,14 +9,40 @@ package helpers import ( + "bytes" + "embed" + "log" + "path/filepath" template "text/template" "github.com/sveltinio/sveltin/config" ) +// TplConfig is the struct representing all is needed by a template file +// (path to the template, functions map and template data). +type TplConfig struct { + PathToTplFile string + Funcs template.FuncMap + Data config.TemplateData +} + +// Run executes the templates and return the content as []byte. +func (tplConfig *TplConfig) Run(embedFS *embed.FS) []byte { + pathToTplFile := tplConfig.PathToTplFile + tplFilename := filepath.Base(tplConfig.PathToTplFile) + funcMap := tplConfig.Funcs + + tmpl := template.Must(template.New(tplFilename).Funcs(funcMap).ParseFS(embedFS, pathToTplFile)) + var writer bytes.Buffer + if err := tmpl.ExecuteTemplate(&writer, tplFilename, tplConfig.Data); err != nil { + log.Fatalln(err.Error()) + } + return writer.Bytes() +} + // BuildTemplate creates TplConfig struct with all is needed for a golang template to be executed -func BuildTemplate(tplPath string, funcs template.FuncMap, data *config.TemplateData) *config.TplConfig { - c := new(config.TplConfig) +func BuildTemplate(tplPath string, funcs template.FuncMap, data *config.TemplateData) *TplConfig { + c := new(TplConfig) c.PathToTplFile = tplPath c.Funcs = funcs c.Data = *data diff --git a/internal/builder/constants.go b/internal/builder/constants.go index 893cc8dc..f5b92f57 100644 --- a/internal/builder/constants.go +++ b/internal/builder/constants.go @@ -73,6 +73,8 @@ const ( Slug string = "slug" // SlugEndpoint is the string for the 'slug' file. SlugEndpoint string = "slugendpoint" + // SlugLayout is the string from the 'layout' file + SlugLayout string = "sluglayout" //============================================================================= diff --git a/internal/builder/menu_builder.go b/internal/builder/menu_builder.go index ab1e573a..25e365fb 100644 --- a/internal/builder/menu_builder.go +++ b/internal/builder/menu_builder.go @@ -9,6 +9,7 @@ package builder import ( + "path/filepath" "strings" "text/template" @@ -57,16 +58,20 @@ func (b *MenuContentBuilder) SetTemplateData(artifactData *config.TemplateData) func (b *MenuContentBuilder) setFuncs() { b.Funcs = template.FuncMap{ + "StringsJoin": strings.Join, + "Base": filepath.Base, "Capitalize": func(txt string) string { return utils.ToTitle(txt) }, - "StringsJoin": strings.Join, "ToURL": func(txt string) string { return utils.ToURL(txt) }, "PlusOne": func(x int) int { return utils.PlusOne(x) }, + "MinusOne": func(x int) int { + return utils.MinusOne(x) + }, "Sum": func(x int, y int) int { return utils.Sum(x, y) }, diff --git a/internal/builder/resource_builder.go b/internal/builder/resource_builder.go index a9a1c04f..6f8a08e6 100644 --- a/internal/builder/resource_builder.go +++ b/internal/builder/resource_builder.go @@ -67,6 +67,9 @@ func (b *ResourceContentBuilder) setPathToTplFile() error { case SlugEndpoint: b.PathToTplFile = b.EmbeddedResources[SlugEndpoint] return nil + case SlugLayout: + b.PathToTplFile = b.EmbeddedResources[SlugLayout] + return nil case Lib: b.PathToTplFile = b.EmbeddedResources[Lib] return nil diff --git a/internal/fsm/sveltin_fsm.go b/internal/fsm/sveltin_fsm.go index 12c4f0f4..e74d8c3c 100644 --- a/internal/fsm/sveltin_fsm.go +++ b/internal/fsm/sveltin_fsm.go @@ -16,6 +16,7 @@ import ( "github.com/sveltinio/sveltin/helpers" "github.com/sveltinio/sveltin/internal/composer" "github.com/sveltinio/sveltin/internal/pathmaker" + "github.com/sveltinio/sveltin/internal/tpltypes" ) // SveltinFSManager is the struct for a pathmaker. @@ -83,15 +84,29 @@ func (s *SveltinFSManager) NewPublicPage(name string, language string) *composer } } -// NewNoPage returns a pointer to a 'no-public page' File. -func (s *SveltinFSManager) NewNoPage(name string, projectConfig *config.ProjectConfig, resources []string, contents map[string][]string, metadata map[string][]string, pages []string) *composer.File { +// NewNoPageFile returns a pointer to a 'no-public page' File. +func (s *SveltinFSManager) NewNoPageFile(name string, projectConfig *tpltypes.ProjectData, resources []string, contents map[string][]string) *composer.File { return &composer.File{ Name: name + ".xml", TemplateID: name, TemplateData: &config.TemplateData{ - NoPage: &config.NoPage{ + NoPageData: &tpltypes.NoPageData{ Config: projectConfig, - Items: helpers.NewNoPageItems(resources, contents, metadata, pages), + Items: helpers.NewNoPageItems(resources, contents), + }, + }, + } +} + +// NewMenuFile returns a pointer to a 'no-public page' File. +func (s *SveltinFSManager) NewMenuFile(name string, projectConfig *tpltypes.ProjectData, resources []string, contents map[string][]string, withContentFlag bool) *composer.File { + return &composer.File{ + Name: name + ".js.ts", + TemplateID: name, + TemplateData: &config.TemplateData{ + MenuData: &tpltypes.MenuData{ + Items: helpers.NewMenuItems(resources, contents), + WithContent: withContentFlag, }, }, } diff --git a/internal/tpltypes/menu.go b/internal/tpltypes/menu.go new file mode 100644 index 00000000..de1c1ebb --- /dev/null +++ b/internal/tpltypes/menu.go @@ -0,0 +1,21 @@ +/** + * Copyright © 2021-present Sveltin contributors + * + * Use of this source code is governed by Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package tpltypes + +// MenuData is the struct representing a menu item. +type MenuData struct { + Items *MenuItems + WithContent bool +} + +// MenuItems is a struct representing a resource and its content as menu item. +type MenuItems struct { + Name string + Resources []string + Content map[string][]string +} diff --git a/internal/tpltypes/nopage.go b/internal/tpltypes/nopage.go new file mode 100644 index 00000000..6324d28f --- /dev/null +++ b/internal/tpltypes/nopage.go @@ -0,0 +1,21 @@ +/** + * Copyright © 2021-present Sveltin contributors + * + * Use of this source code is governed by Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package tpltypes + +// NoPageData is the struct representing a no-public page (sitemap and rss) for a sveltin project. +type NoPageData struct { + Config *ProjectData + Items *NoPageItems +} + +// NoPageItems is the struct representing an item +// of no-public page (sitemap and rss) for a sveltin project. +type NoPageItems struct { + Resources []string + Content map[string][]string +} diff --git a/internal/tpltypes/project.go b/internal/tpltypes/project.go new file mode 100644 index 00000000..8d2183b3 --- /dev/null +++ b/internal/tpltypes/project.go @@ -0,0 +1,24 @@ +/** + * Copyright © 2021-present Sveltin contributors + * + * Use of this source code is governed by Apache 2.0 license + * that can be found in the LICENSE file. + */ + +// Package tpltypes ... +package tpltypes + +// ProjectData is the struct used to map the env.production file props. +type ProjectData struct { + BaseURL string `mapstructure:"VITE_PUBLIC_BASE_PATH"` + SitemapChangeFreq string `mapstructure:"sitemapChangeFreq"` + SitemapPriority string `mapstructure:"sitemapPriority"` + SvelteKitBuildFolder string `mapstructure:"SVELTEKIT_BUILD_FOLDER"` + FTPHost string `mapstructure:"FTP_HOST"` + FTPPort int `mapstructure:"FTP_PORT"` + FTPUser string `mapstructure:"FTP_USER"` + FTPPassword string `mapstructure:"FTP_PASSWORD"` + FTPServerFolder string `mapstructure:"FTP_SERVER_FOLDER"` + FTPDialTimeout int `mapstructure:"FTP_DIAL_TIMEOUT"` + FTPEPSVMode bool `mapstructure:"FTP_EPSV"` +} diff --git a/internal/tpltypes/resource.go b/internal/tpltypes/resource.go new file mode 100644 index 00000000..0c73ef74 --- /dev/null +++ b/internal/tpltypes/resource.go @@ -0,0 +1,15 @@ +/** + * Copyright © 2021-present Sveltin contributors + * + * Use of this source code is governed by Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package tpltypes + +// ResourceData is the struct representing the user configs for the new resource. +type ResourceData struct { + Name string + Group string + SlugLayout bool +} diff --git a/resources/defaults.go b/resources/defaults.go index 5883e761..762cbd56 100644 --- a/resources/defaults.go +++ b/resources/defaults.go @@ -55,6 +55,7 @@ var SveltinResourceFS = map[string]string{ "indexendpoint": "internal/templates/resource/page.server.ts.gotxt", "slug": "internal/templates/resource/slug.svelte.gotxt", "slugendpoint": "internal/templates/resource/slug.ts.gotxt", + "sluglayout": "internal/templates/resource/layout.svelte.gotxt", } // SveltinAPIFS is the map for the api template files. diff --git a/resources/internal/templates/resource/api/apiIndex.gotxt b/resources/internal/templates/resource/api/apiIndex.gotxt index b1bc5b08..3542033a 100644 --- a/resources/internal/templates/resource/api/apiIndex.gotxt +++ b/resources/internal/templates/resource/api/apiIndex.gotxt @@ -1,4 +1,4 @@ -import { list } from '$lib/{{ .Name }}/load{{ .Name | ToVariableName | Capitalize }}'; +import { list } from '$lib/{{ .ResourceData.Name }}/load{{ .ResourceData.Name | ToVariableName | Capitalize }}'; import { error } from '@sveltejs/kit'; export const prerender = true; diff --git a/resources/internal/templates/resource/api/apiSlug.gotxt b/resources/internal/templates/resource/api/apiSlug.gotxt index 3bbd7a6e..176cbb37 100644 --- a/resources/internal/templates/resource/api/apiSlug.gotxt +++ b/resources/internal/templates/resource/api/apiSlug.gotxt @@ -1,4 +1,4 @@ -import { getSingle } from '$lib/{{ .Name }}/load{{ .Name | ToVariableName | Capitalize }}'; +import { getSingle } from '$lib/{{ .ResourceData.Name }}/load{{ .ResourceData.Name | ToVariableName | Capitalize }}'; import { error } from '@sveltejs/kit'; export const prerender = true; @@ -7,7 +7,7 @@ export async function GET({ url }) { const { pathname } = url; const slug = pathname.substring(pathname.lastIndexOf('/') + 1); if (!slug) { - throw error(404, '{{ .Name | ToVariableName | Capitalize }} not found'); + throw error(404, '{{ .ResourceData.Name | ToVariableName | Capitalize }} not found'); } const data = await getSingle(slug); diff --git a/resources/internal/templates/resource/layout.svelte.gotxt b/resources/internal/templates/resource/layout.svelte.gotxt new file mode 100644 index 00000000..deea3d46 --- /dev/null +++ b/resources/internal/templates/resource/layout.svelte.gotxt @@ -0,0 +1,5 @@ + + + diff --git a/resources/internal/templates/resource/lib.gotxt b/resources/internal/templates/resource/lib.gotxt index 08cf069c..3f70eeaa 100644 --- a/resources/internal/templates/resource/lib.gotxt +++ b/resources/internal/templates/resource/lib.gotxt @@ -1,7 +1,7 @@ import type { Sveltin } from 'src/sveltin'; export async function list() { - const contentFiles = import.meta.glob('/{{ .Config.Paths.Content }}/{{ .Name }}/**/*.{svelte.md,md,svx}'); + const contentFiles = import.meta.glob('/{{ .Config.Paths.Content }}/{{ .ResourceData.Name }}/**/*.{svelte.md,md,svx}'); const contentFilesArray = Object.entries(contentFiles); const contents = await Promise.all( contentFilesArray.map(async ([path, resolver]) => { @@ -20,7 +20,7 @@ export async function list() { return publishedByDate; }; -{{ $slugName := .Name | ToSlug}} +{{ $slugName := .ResourceData.Name | ToSlug}} export async function getSingle(slug: string) { const resourceName = '{{ $slugName }}'; const publishedByDate = await list(); diff --git a/resources/internal/templates/resource/page.server.ts.gotxt b/resources/internal/templates/resource/page.server.ts.gotxt index 27d3a7b4..0e67c2de 100644 --- a/resources/internal/templates/resource/page.server.ts.gotxt +++ b/resources/internal/templates/resource/page.server.ts.gotxt @@ -1,8 +1,8 @@ import type { Sveltin } from 'src/sveltin'; -import { list } from '$lib/{{ .Name }}/load{{ .Name | ToVariableName | Capitalize }}'; +import { list } from '$lib/{{ .ResourceData.Name }}/load{{ .ResourceData.Name | ToVariableName | Capitalize }}'; export async function load() { - const resourceName = '{{ .Name }}'; + const resourceName = '{{ .ResourceData.Name }}'; const data = await list(); const items: Array = []; diff --git a/resources/internal/templates/resource/page.svelte.gotxt b/resources/internal/templates/resource/page.svelte.gotxt index 5bfeac2f..eafa6cdb 100644 --- a/resources/internal/templates/resource/page.svelte.gotxt +++ b/resources/internal/templates/resource/page.svelte.gotxt @@ -1,4 +1,4 @@ -{{ $varName := .Name | ToVariableName | ReplaceIfNested }} +{{ $varName := .ResourceData.Name | ToVariableName | ReplaceIfNested }} diff --git a/resources/internal/templates/resource/lib.gotxt b/resources/internal/templates/resource/lib.gotxt index 3f70eeaa..8ad87411 100644 --- a/resources/internal/templates/resource/lib.gotxt +++ b/resources/internal/templates/resource/lib.gotxt @@ -1,7 +1,7 @@ import type { Sveltin } from 'src/sveltin'; export async function list() { - const contentFiles = import.meta.glob('/{{ .Config.Paths.Content }}/{{ .ResourceData.Name }}/**/*.{svelte.md,md,svx}'); + const contentFiles = import.meta.glob('/{{ .Config.Paths.Content }}/{{ .Resource.Name }}/**/*.{svelte.md,md,svx}'); const contentFilesArray = Object.entries(contentFiles); const contents = await Promise.all( contentFilesArray.map(async ([path, resolver]) => { @@ -20,7 +20,7 @@ export async function list() { return publishedByDate; }; -{{ $slugName := .ResourceData.Name | ToSlug}} +{{ $slugName := .Resource.Name | ToSlug}} export async function getSingle(slug: string) { const resourceName = '{{ $slugName }}'; const publishedByDate = await list(); diff --git a/resources/internal/templates/resource/metadata/libList.gotxt b/resources/internal/templates/resource/metadata/libList.gotxt index e2fa9853..f8764213 100644 --- a/resources/internal/templates/resource/metadata/libList.gotxt +++ b/resources/internal/templates/resource/metadata/libList.gotxt @@ -1,5 +1,5 @@ -{{ $resourceName := .Resource | ToVariableName | Capitalize }} -{{ $mdName := .Name | ToSnakeCase }} +{{- $resourceName := .Metadata.Resource | ToVariableName | Capitalize -}} +{{- $mdName := .Metadata.Name | ToSnakeCase -}} import type { Sveltin } from 'src/sveltin'; import { groupedByMany } from '$lib/utils/collections.js'; import { list } from './load{{ $resourceName }}'; diff --git a/resources/internal/templates/resource/metadata/libSingle.gotxt b/resources/internal/templates/resource/metadata/libSingle.gotxt index 94191c1f..db6ffae3 100644 --- a/resources/internal/templates/resource/metadata/libSingle.gotxt +++ b/resources/internal/templates/resource/metadata/libSingle.gotxt @@ -1,5 +1,5 @@ -{{ $resourceName := .Resource | ToVariableName | Capitalize }} -{{ $mdName := .Name | ToSnakeCase }} +{{- $resourceName := .Metadata.Resource | ToVariableName | Capitalize -}} +{{- $mdName := .Metadata.Name | ToSnakeCase -}} import type { Sveltin } from 'src/sveltin'; import { groupedByOne } from '$lib/utils/collections.js'; import { list } from './load{{ $resourceName }}'; diff --git a/resources/internal/templates/resource/metadata/page.server.ts.gotxt b/resources/internal/templates/resource/metadata/page.server.ts.gotxt index aa3d2279..a06ea6e0 100644 --- a/resources/internal/templates/resource/metadata/page.server.ts.gotxt +++ b/resources/internal/templates/resource/metadata/page.server.ts.gotxt @@ -1,6 +1,6 @@ import type { Sveltin } from 'src/sveltin'; -{{ $mdName := .Name | ToVariableName | Capitalize}} -import { all } from '$lib/{{ .Resource }}/load{{ $mdName }}'; +{{- $mdName := .Metadata.Name | ToVariableName | Capitalize -}} +import { all } from '$lib/{{ .Metadata.Resource }}/load{{ $mdName }}'; export async function load() { const data = await all(); diff --git a/resources/internal/templates/resource/metadata/page.svelte.gotxt b/resources/internal/templates/resource/metadata/page.svelte.gotxt index 7c2f457a..2297e9d2 100644 --- a/resources/internal/templates/resource/metadata/page.svelte.gotxt +++ b/resources/internal/templates/resource/metadata/page.svelte.gotxt @@ -5,24 +5,24 @@ export let data: PageData; $: ({ metadata } = data); -{{ $mdName := .Name | ToVariableName}} +{{ $mdName := .Metadata.Name | ToVariableName}}
-

Grouped by {{ .Name | Capitalize }}

+

Grouped by {{ .Metadata.Name | Capitalize }}

{#if metadata.length != 0} {#each metadata as {{ $mdName}} }

- { {{ $mdName }}.name} + { {{ $mdName }}.name}

{/each} {:else}

- Please, check all your content ensuring the YAML frontmatter contains "{{ .Name | ToVariableName}}". + Please, check all your content ensuring the YAML frontmatter contains "{{ .Metadata.Name | ToVariableName}}".

{/if}
diff --git a/resources/internal/templates/resource/metadata/slug.svelte.gotxt b/resources/internal/templates/resource/metadata/slug.svelte.gotxt index 6577037e..78d5576e 100644 --- a/resources/internal/templates/resource/metadata/slug.svelte.gotxt +++ b/resources/internal/templates/resource/metadata/slug.svelte.gotxt @@ -13,7 +13,7 @@

{slug} [ Total: {itemsCounter} ]

{:else} diff --git a/resources/internal/templates/resource/metadata/slug.ts.gotxt b/resources/internal/templates/resource/metadata/slug.ts.gotxt index 3f4f1040..c619a742 100644 --- a/resources/internal/templates/resource/metadata/slug.ts.gotxt +++ b/resources/internal/templates/resource/metadata/slug.ts.gotxt @@ -1,5 +1,5 @@ -{{ $mdName := .Name | ToVariableName | Capitalize }} -import { groupedBy } from '$lib/{{ .Resource }}/load{{ $mdName }}'; +{{- $mdName := .Metadata.Name | ToVariableName | Capitalize -}} +import { groupedBy } from '$lib/{{ .Metadata.Resource }}/load{{ $mdName }}'; import { error } from '@sveltejs/kit'; export async function load({ params }) { diff --git a/resources/internal/templates/resource/page.server.ts.gotxt b/resources/internal/templates/resource/page.server.ts.gotxt index 0e67c2de..04bca948 100644 --- a/resources/internal/templates/resource/page.server.ts.gotxt +++ b/resources/internal/templates/resource/page.server.ts.gotxt @@ -1,8 +1,8 @@ import type { Sveltin } from 'src/sveltin'; -import { list } from '$lib/{{ .ResourceData.Name }}/load{{ .ResourceData.Name | ToVariableName | Capitalize }}'; +import { list } from '$lib/{{ .Resource.Name }}/load{{ .Resource.Name | ToVariableName | Capitalize }}'; export async function load() { - const resourceName = '{{ .ResourceData.Name }}'; + const resourceName = '{{ .Resource.Name }}'; const data = await list(); const items: Array = []; diff --git a/resources/internal/templates/resource/page.svelte.gotxt b/resources/internal/templates/resource/page.svelte.gotxt index eafa6cdb..3a9fed40 100644 --- a/resources/internal/templates/resource/page.svelte.gotxt +++ b/resources/internal/templates/resource/page.svelte.gotxt @@ -1,4 +1,4 @@ -{{ $varName := .ResourceData.Name | ToVariableName | ReplaceIfNested }} +{{ $varName := .Resource.Name | ToVariableName | ReplaceIfNested }}