Skip to content

Commit

Permalink
Add ability to load arbitrary CSS/JS assets to admin UI for customiza…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
knadh committed Dec 5, 2023
1 parent 0c30d24 commit 61471af
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
<script>window._ROOT_URL = "{{ .Consts.RootURL }}";</script>
<script defer src="{{ .Consts.RootURL }}/admin/static/main.js?v={{ .AssetVer }}"></script>
<script defer src="{{ .Consts.RootURL }}/admin/static/alpine.js?v={{ .AssetVer }}"></script>

{{ range $j := .Consts.AdminAssets -}}
{{- if (hasSuffix ".js" $j) -}}
<script defer src="{{ $j }}?v={{ $.AssetVer }}"></script>
{{- else if (hasSuffix ".css" $j) -}}
<link rel="stylesheet" type="text/css" href="{{ $j }}?v={{ $.AssetVer }}">
{{- end }}
{{ end }}
</head>
<body x-init="onLoad()" x-data="globalComponent()">
<template x-if="ready">
Expand Down
7 changes: 5 additions & 2 deletions cmd/dictpress/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"unicode"

"github.com/Masterminds/sprig/v3"
"github.com/jmoiron/sqlx"
"github.com/knadh/dictpress/internal/data"
"github.com/knadh/dictpress/tokenizers/indicphone"
Expand All @@ -30,6 +31,7 @@ func initConstants(ko *koanf.Koanf) Consts {
AdminPassword: ko.MustBytes("app.admin_password"),
EnableSubmissions: ko.Bool("app.enable_submissions"),
EnableGlossary: ko.Bool("glossary.enabled"),
AdminAssets: ko.Strings("app.admin_assets"),
}

if len(c.AdminUsername) < 6 {
Expand Down Expand Up @@ -89,10 +91,11 @@ func initFS() stuffbin.FileSystem {

func initAdminTemplates(app *App) *template.Template {
// Init admin templates.
tpls, err := stuffbin.ParseTemplatesGlob(nil, app.fs, "/admin/*.html")
tpls, err := stuffbin.ParseTemplatesGlob(sprig.FuncMap(), app.fs, "/admin/*.html")
if err != nil {
lo.Fatalf("error parsing e-mail notif templates: %v", err)
lo.Fatalf("error parsing admin templates: %v", err)
}

return tpls
}

Expand Down
1 change: 1 addition & 0 deletions cmd/dictpress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Lang struct {
type Consts struct {
Site string
RootURL string
AdminAssets []string
EnableSubmissions bool
EnableGlossary bool
AdminUsername, AdminPassword []byte
Expand Down
4 changes: 4 additions & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ admin_password = "dictpress_admin_password"
# Example: yourdictionary.site.com
root_url = "http://localhost:9000"

# (Optional) URLs to one or more JS/CSS files (ending in .js or .css) to load on the admin UI.
# This may be useful to integrate multi-lingual typing capabilities in admin UI boxes when dealing with different languages.
admin_assets = []

# Enable rendering arbitrary pages from (site_dir/pages/*.html).
# For instance, "about" for site_dir/pages/about.html will be
# rendered on site.com/pages/about where the template is defined
Expand Down

0 comments on commit 61471af

Please sign in to comment.