From 6f8e576c9b89d24623caf8495d93200c1692c880 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:52:57 +0100 Subject: [PATCH] Add fallback-content-type property --- docs/configuration.md | 4 ++++ internal/feed/extension.go | 13 +++++++++---- internal/widget/extension.go | 20 +++++++++++--------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index d5856ca..f133ce2 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1006,12 +1006,16 @@ Display a widget provided by an external source (3rd party). If you want to lear | Name | Type | Required | Default | | ---- | ---- | -------- | ------- | | url | string | yes | | +| fallback-content-type | string | no | | | allow-potentially-dangerous-html | boolean | no | false | | parameters | key & value | no | | ##### `url` The URL of the extension. +##### `fallback-content-type` +Optionally specify the fallback content type of the extension if the URL does not return a valid `Widget-Content-Type` header. Currently the only supported value for this property is `html`. + ##### `allow-potentially-dangerous-html` Whether to allow the extension to display HTML. diff --git a/internal/feed/extension.go b/internal/feed/extension.go index 3aa499a..916ee78 100644 --- a/internal/feed/extension.go +++ b/internal/feed/extension.go @@ -27,9 +27,10 @@ const ( ) type ExtensionRequestOptions struct { - URL string `yaml:"url"` - Parameters map[string]string `yaml:"parameters"` - AllowHtml bool `yaml:"allow-potentially-dangerous-html"` + URL string `yaml:"url"` + FallbackContentType string `yaml:"fallback-content-type"` + Parameters map[string]string `yaml:"parameters"` + AllowHtml bool `yaml:"allow-potentially-dangerous-html"` } type Extension struct { @@ -88,7 +89,11 @@ func FetchExtension(options ExtensionRequestOptions) (Extension, error) { contentType, ok := ExtensionStringToType[response.Header.Get(ExtensionHeaderContentType)] if !ok { - contentType = ExtensionContentUnknown + contentType, ok = ExtensionStringToType[options.FallbackContentType] + + if !ok { + contentType = ExtensionContentUnknown + } } extension.Content = convertExtensionContent(options, body, contentType) diff --git a/internal/widget/extension.go b/internal/widget/extension.go index 547bbfe..4b443f2 100644 --- a/internal/widget/extension.go +++ b/internal/widget/extension.go @@ -12,12 +12,13 @@ import ( ) type Extension struct { - widgetBase `yaml:",inline"` - URL string `yaml:"url"` - Parameters map[string]string `yaml:"parameters"` - AllowHtml bool `yaml:"allow-potentially-dangerous-html"` - Extension feed.Extension `yaml:"-"` - cachedHTML template.HTML `yaml:"-"` + widgetBase `yaml:",inline"` + URL string `yaml:"url"` + FallbackContentType string `yaml:"fallback-content-type"` + Parameters map[string]string `yaml:"parameters"` + AllowHtml bool `yaml:"allow-potentially-dangerous-html"` + Extension feed.Extension `yaml:"-"` + cachedHTML template.HTML `yaml:"-"` } func (widget *Extension) Initialize() error { @@ -38,9 +39,10 @@ func (widget *Extension) Initialize() error { func (widget *Extension) Update(ctx context.Context) { extension, err := feed.FetchExtension(feed.ExtensionRequestOptions{ - URL: widget.URL, - Parameters: widget.Parameters, - AllowHtml: widget.AllowHtml, + URL: widget.URL, + FallbackContentType: widget.FallbackContentType, + Parameters: widget.Parameters, + AllowHtml: widget.AllowHtml, }) widget.canContinueUpdateAfterHandlingErr(err)