Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Disable outputEscape for some specific filters #565

Closed
ybouane opened this issue Dec 4, 2022 · 1 comment
Closed

Disable outputEscape for some specific filters #565

ybouane opened this issue Dec 4, 2022 · 1 comment
Labels

Comments

@ybouane
Copy link

ybouane commented Dec 4, 2022

Hello,
I'm using the outputEscape option to escape output by default.
However, for some specific filters I want to disable the escaping, how can that be done?
How to integrate the functionality of the raw filter into my own filters. (I saw in the code that the raw filter is hardcoded :/ )

Example:
{{ "<b>Hello \nworld.</b>" }}
would output:
&lt;b&gt;hello world&lt;/b&gt;

I made a nl2br filter:
const nl2br = function(str) {return escapeHtml(str).replace(/\r?\n/g, '<br>')};

So for:
{{ "<b>Hello \nworld.</b>" | nl2br }}
the output is:
&amp;lt;b&amp;gt;hello &lt;br&gt;world&amp;lt;/b&amp;gt;
(it escapes twice)

Whereas I actually want:
&lt;b&gt;hello <br>world&lt;/b&gt;

The way to achieve that would be to type in {{ "<b>Hello \nworld.</b>" | nl2br | raw }} (and remove the escapeHtml step from the function), but I wanted to check if it was possible to avoid that?

I tried storing a variable isEscaped in the context of the nl2br filter and checking if is set on my outputEscape function which works in a simple use-case but fails in this example:
{% assign "a" | nl2br %} {{ "<b>Hello \nworld.</b>" }}

In this case, the second statement skips the escaping which is undesirable.

Thanks for your help!

@harttle
Copy link
Owner

harttle commented Dec 4, 2022

outputEscape is undefined by default, {{ "<b>Hello \nworld.</b>" }} will output "<b>Hello \nworld.</b>" directly, by default.

I tried storing a variable isEscaped in the context of the nl2br filter

You're missing =, the correct syntax is:

{% assign foo = "<a>" | escape %}

Live demo: https://liquidjs.com/playground.html#eyUgYXNzaWduIGZvbyA9ICI8YT4iIHwgZXNjYXBlICV9Cnt7IGZvbyB9fQ==,e30=

@harttle harttle closed this as completed Dec 4, 2022
Repository owner locked and limited conversation to collaborators Dec 4, 2022
@harttle harttle converted this issue into discussion #566 Dec 4, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants