Skip to content

Commit 95b40d7

Browse files
authored
feat: add url_encode helper (#314)
1 parent f613b55 commit 95b40d7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Jellyfin.Plugin.Webhook/Helpers/HandlebarsFunctionHelpers.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Globalization;
3+
using System.Web;
34
using HandlebarsDotNet;
45

56
namespace Jellyfin.Plugin.Webhook.Helpers;
@@ -46,6 +47,18 @@ public static class HandlebarsFunctionHelpers
4647
}
4748
};
4849

50+
private static readonly HandlebarsHelper UrlEncodeHelper = (writer, context, parameters) =>
51+
{
52+
if (parameters.Length != 1)
53+
{
54+
throw new HandlebarsException("{{url_encode}} helper must have exactly one argument");
55+
}
56+
57+
var valueToEncode = GetStringValue(parameters[0]);
58+
var encodedValue = HttpUtility.UrlEncode(valueToEncode);
59+
writer.WriteSafeString(encodedValue);
60+
};
61+
4962
/// <summary>
5063
/// Register handlebars helpers.
5164
/// </summary>
@@ -57,6 +70,7 @@ public static void RegisterHelpers()
5770
{
5871
writer.WriteSafeString($"<a href='{parameters["url"]}'>{context["text"]}</a>");
5972
});
73+
Handlebars.RegisterHelper("url_encode", UrlEncodeHelper);
6074
}
6175

6276
/// <summary>

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ See [Templates](Jellyfin.Plugin.Webhook/Templates) for sample templates.
4747
- if the value of the parameter is not null or empty
4848
- link_to
4949
- wrap the $url and $text in an `<a>` tag
50+
- url_encode
51+
- encode the given text to url-encoded format (useful for including text with spaces in urls)
5052

5153
#### Variables:
5254

0 commit comments

Comments
 (0)