Skip to content

Commit

Permalink
feat: add netlify-identity module (#147)
Browse files Browse the repository at this point in the history
* feat: add the `decap-cms-head-end` and `decap-cms-body-end` hooks

---------

Co-authored-by: Razon Yang <razonyang@gmail.com>
  • Loading branch information
pleasantone and razonyang authored Feb 9, 2024
1 parent 1f7ccc5 commit 0e35b70
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
min = "0.111.3"
# extended = true

[[module.imports]]
path = "github.com/hugomods/hugopress"

[outputFormats]
[outputFormats.DecapCMSConfig]
baseName = "decap-cms-config"
Expand Down
2 changes: 2 additions & 0 deletions layouts/_default/decap-cms.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
{{- warnf "[decap-cms] please append %q into the %q output in configuration." "DecapCMSConfig" "home" }}
{{- end }}
{{ partialCached "decap-cms/hooks/head-end" . }}
{{ partial "hugopress/functions/render-hooks" (dict "Name" "decap-cms-head-end" "Page" .) }}
</head>
<body>
{{ partialCached "decap-cms/script" . }}
{{ partialCached "decap-cms/init" . }}
{{ partialCached "decap-cms/hooks/body-end" . }}
{{ partial "hugopress/functions/render-hooks" (dict "Name" "decap-cms-body-end" "Page" .) }}
</body>
</html>
19 changes: 19 additions & 0 deletions modules/netlify-identity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Netlify Identity Support for Decap CMS

## Configurations

```toml
[params.decap_cms._netlify_identity]
js_url = "https://identity.netlify.com/v1/netlify-identity-widget.js"
```

## Inject Partials

If your theme doesn't implement HugoPress, you will need to inject the following partials into your homepage.

```html
{{- if .IsHome }}
{{ partialCached "hugopress/modules/decap-cms-netlify-identity/script" . }}
{{ partialCached "hugopress/modules/decap-cms-netlify-identity/callback-script" . }}
{{- end }}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import params from '@params'

if (window.netlifyIdentity) {
window.netlifyIdentity.on('init', user => {
if (!user) {
window.netlifyIdentity.on('login', () => {
document.location.href = params.admin_path
})
}
})
}
3 changes: 3 additions & 0 deletions modules/netlify-identity/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/hugomods/decap-cms/modules/netlify-identity

go 1.18
12 changes: 12 additions & 0 deletions modules/netlify-identity/hugo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[module.imports]]
path = "github.com/hugomods/decap-cms"

[params.hugopress.modules.decap-cms-netlify-identity.hooks.head-end]

[params.hugopress.modules.decap-cms-netlify-identity.hooks.body-end]

[params.hugopress.modules.decap-cms-netlify-identity.hooks.decap-cms-head-end]
cacheable = true

[params.decap_cms._netlify_identity]
js_url = "https://identity.netlify.com/v1/netlify-identity-widget.js"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- $adminPath := "" }}
{{- range where site.Pages "Layout" "decap-cms" }}
{{- $adminPath = .RelPermalink }}
{{- break }}
{{- end }}
{{- if ne $adminPath "" }}
{{- $targetPath := "js/netlify-identity-callback.js" }}
{{- $opts := dict
"targetPath" $targetPath
"minify" hugo.IsProduction
"params" (dict
"admin_path" $adminPath)
}}
{{- $js := resources.Get "decap-cms/modules/netlify-identity/callback.js" | js.Build $opts }}
{{- if hugo.IsProduction }}
{{- $js = $js | fingerprint }}
{{- end }}
<script src="{{ $js.RelPermalink }}"></script>
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- if .Page.IsHome }}
{{ partialCached "hugopress/modules/decap-cms-netlify-identity/callback-script" . }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ partialCached "hugopress/modules/decap-cms-netlify-identity/script" . }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- if .Page.IsHome }}
{{ partialCached "hugopress/modules/decap-cms-netlify-identity/script" . }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="{{ site.Params.decap_cms._netlify_identity.js_url }}"></script>

0 comments on commit 0e35b70

Please sign in to comment.