diff --git a/src/MyWebLog.Data/Data.fs b/src/MyWebLog.Data/Data.fs index 79c71c9..0a8401e 100644 --- a/src/MyWebLog.Data/Data.fs +++ b/src/MyWebLog.Data/Data.fs @@ -724,6 +724,15 @@ module Theme = resultOption; withRetryOptionDefault } + /// Retrieve a theme by its ID, excluding the text of templates + let findByIdWithoutText (themeId : ThemeId) = + rethink { + withTable Table.Theme + get themeId + merge (fun row -> r.HashMap ("templates", row["templates"].Without [| "text" |])) + resultOption; withRetryOptionDefault + } + /// Save a theme let save (theme : Theme) = rethink { diff --git a/src/MyWebLog/Handlers/Admin.fs b/src/MyWebLog/Handlers/Admin.fs index 87fa88e..a458398 100644 --- a/src/MyWebLog/Handlers/Admin.fs +++ b/src/MyWebLog/Handlers/Admin.fs @@ -163,7 +163,8 @@ let editPage pgId : HttpHandler = fun next ctx -> task { } match result with | Some (title, page) -> - let model = EditPageModel.fromPage page + let model = EditPageModel.fromPage page + let! templates = templatesForTheme ctx "page" return! Hash.FromAnonymousObject {| csrf = csrfToken ctx @@ -171,7 +172,7 @@ let editPage pgId : HttpHandler = fun next ctx -> task { metadata = Array.zip model.metaNames model.metaValues |> Array.mapi (fun idx (name, value) -> [| string idx; name; value |]) page_title = title - templates = templatesForTheme ctx "page" + templates = templates |} |> viewForTheme "admin" "page-edit" next ctx | None -> return! Error.notFound next ctx diff --git a/src/MyWebLog/Handlers/Helpers.fs b/src/MyWebLog/Handlers/Helpers.fs index 590f86d..36b2330 100644 --- a/src/MyWebLog/Handlers/Helpers.fs +++ b/src/MyWebLog/Handlers/Helpers.fs @@ -185,19 +185,20 @@ open System.Collections.Generic open System.IO /// Get the templates available for the current web log's theme (in a key/value pair list) -let templatesForTheme (ctx : HttpContext) (typ : string) = - seq { - KeyValuePair.Create ("", $"- Default (single-{typ}) -") - yield! - Path.Combine ("themes", ctx.WebLog.themePath) - |> Directory.EnumerateFiles - |> Seq.filter (fun it -> it.EndsWith $"{typ}.liquid") - |> Seq.map (fun it -> - let parts = it.Split Path.DirectorySeparatorChar - let template = parts[parts.Length - 1].Replace (".liquid", "") - KeyValuePair.Create (template, template)) - } - |> Array.ofSeq +let templatesForTheme (ctx : HttpContext) (typ : string) = backgroundTask { + match! Data.Theme.findByIdWithoutText (ThemeId ctx.WebLog.themePath) ctx.Conn with + | Some theme -> + return seq { + KeyValuePair.Create ("", $"- Default (single-{typ}) -") + yield! + theme.templates + |> Seq.ofList + |> Seq.filter (fun it -> it.name.EndsWith $"-{typ}" && it.name <> $"single-{typ}") + |> Seq.map (fun it -> KeyValuePair.Create (it.name, it.name)) + } + |> Array.ofSeq + | None -> return [| KeyValuePair.Create ("", $"- Default (single-{typ}) -") |] +} /// Get all authors for a list of posts as metadata items let getAuthors (webLog : WebLog) (posts : Post list) conn = diff --git a/src/MyWebLog/Maintenance.fs b/src/MyWebLog/Maintenance.fs index aa9856d..db656ac 100644 --- a/src/MyWebLog/Maintenance.fs +++ b/src/MyWebLog/Maintenance.fs @@ -122,7 +122,11 @@ let importLinks args sp = task { /// Load a theme from the given ZIP file let loadTheme (args : string[]) (sp : IServiceProvider) = task { if args.Length > 1 then - match Handlers.Admin.getThemeName args[1] with + let fileName = + match args[1].LastIndexOf Path.DirectorySeparatorChar with + | -1 -> args[1] + | it -> args[1][(it + 1)..] + match Handlers.Admin.getThemeName fileName with | Some themeName -> let conn = sp.GetRequiredService () let clean = if args.Length > 2 then bool.Parse args[2] else true