liquidjs icon indicating copy to clipboard operation
liquidjs copied to clipboard

Support for all built-in Jekyll filters: slugify, number_of_words, date_to_xmlschema, date_to_rfc822

Open Nowaker opened this issue 3 years ago • 14 comments

liquidjs is "missing" a couple Jekyll-specific filters:

  • slugify
  • number_of_words
  • date_to_xmlschema
  • date_to_rfc822

https://jekyllrb.com/docs/liquid/filters

Would you be willing to include them as part of Liquidjs? If so, I'll create a PR. Please let me know.

Nowaker avatar Jan 19 '22 20:01 Nowaker

It'll be great to have these filters! Except for Jekyll logic specific ones like relative_url.

harttle avatar Jan 20 '22 02:01 harttle

It'll be great to have these filters! Except for Jekyll logic specific ones like relative_url.

harttle avatar Jan 20 '22 02:01 harttle

I'm specifically missing the push filter to add an element to an array. Liquid's append seems to be adding a string to my array character-wise. @harttle such new filter would be added to https://github.com/harttle/liquidjs/blob/master/src/filters/array.ts right? And it could look something like this? (Inspired by concat)

export function push<T> (v: T[], arg: T): T[] {
  assert(arguments.length === 2, 'push expects 2 arguments')
  v = toValue(v)
  arg = toValue(arg)
  return toArray(v).concat([arg])
}

Or even just:

export function push<T> (v: T[], arg: T): T[] {
  return concat(v, [arg])
}

I added this in #611. I hope you like the implementation and decide to accept the PR. Once the PR is merged, does the Playground automatically update to support it?

TomasHubelbauer avatar May 22 '23 16:05 TomasHubelbauer

Ideas for how to support the other filters. I used a full checkbox where I found the filter already in the codebase but did not check too deeply if it is really it or how it is implemented.

Update (by @harttle): will use the following list to track latest status.

  • [ ] relative_url - probably no reasonable way to support?
  • [ ] absolute_url - just append to https://example.com/? Not super meaningful but could be a difference between someone being able to use LiquidJS Playground out of the box or not if they didn't care for the URL but their code was using this filter.
  • [ ] date_to_xmlschema - I think this is just new Date(toValue(v)).toISOString()?
  • [ ] date_to_rfc822 - I think this is just new Date(toValue(v)).toUTCString()?
  • [ ] date_to_string - similar to the above but custom formatting
  • [ ] date_to_string: "ordinal", "US" - ditto
  • [ ] date_to_long_string - ditto
  • [ ] date_to_long_string: "ordinal" - ditto
  • [x] where - implement using filter as this is for arrays only - already done
  • [ ] where_exp - this could be filter + eval for simplicity I think or is there a mechanism for expression parsing?
  • [x] find - array find
  • [x] find_exp - again find + eval unless there is an expression parser already
  • [x] group_by - reduce
  • [x] group_by_exp - reduce + eval
  • [ ] xml_escape - not sure what exactly this does but probably some/full overlap with encodeURIComponent
  • [ ] cgi_escape - or maybe this is encodeURIComponent?
  • [ ] uri_escape - or this? :D
  • [ ] number_of_words - this one is gonna be complex and I don't have any experience with handling Asian scripts but for the start I could add a simple latin oriented filter which splits by a space and returns the length of that
  • [ ] array_to_sentence_string - looks like a simple join with an extra condition
  • [ ] markdownify - probably can lean on some popular MD NPM library here - I made #620 + #621
  • [ ] smartify - this might be just a replace?
  • [ ] sassify - again a candidate for an NPM package
  • [ ] slugify - should be possible with a regex assuming the rules of the slugification are well known
  • [ ] jsonify- just JSON.stringify - this could potentially be an alias for the existing json filter?
  • [ ] normalize_whitespace - regex
  • [x] sort - array sort - already done
  • [x] sample - array index access + Math.random - I made #612
  • [ ] to_integer - Number constructor
  • [x] push, pop, shift, unshift - the JS array method - I made #611 + #614
  • [x] pop - the JS array method
  • [x] shift - the JS array method
  • [x] unshift - the JS array method
  • [ ] inspect - I think we can just use JSON.stringify here and we don't need to follow the same format as Jekyll but the one possible edge case is if someone used the output of this in capture

I am also interested in include_relative and it would be cool to have the Playground have some UI to allow you to upload a file for this and process it client-side to make this work.

TomasHubelbauer avatar May 22 '23 16:05 TomasHubelbauer

I added this in https://github.com/harttle/liquidjs/pull/611. I hope you like the implementation and decide to accept the PR.

As long as Shopify/liquid implements this, I'm good to also add this feature.

Once the PR is merged, does the Playground automatically update to support it?

Yes, it's on CI build for master branch.

harttle avatar May 23 '23 18:05 harttle

According to this link it is one of the built-in Jekyll filters: https://jekyllrb.com/docs/liquid/filters. I took this issue to mean there is interest in adding Jekyll-specific filters on top of the base Liquid language. Are you willing to still accept this filter in that case?

TomasHubelbauer avatar May 23 '23 18:05 TomasHubelbauer

@harttle I don't see a CI build here: https://github.com/harttle/liquidjs/actions Did you mean the GitHub Pages deployment step? Or the Check workflow?

TomasHubelbauer avatar May 23 '23 18:05 TomasHubelbauer

@TomasHubelbauer sorry I thought it was brought from shopify.

But it's also OK to bring jekyll filters to LiquidJS as long as it's not jekyll specific (I mean relying on Jekyll config or anything only available in Jekyll, or any concept only makes sense for Jekyll sites).

I don't see a CI build here

There's a docs flow https://github.com/harttle/liquidjs/blob/master/.github/workflows/docs.yml

harttle avatar May 23 '23 18:05 harttle

Then I regret to inform you that I think #611 might not be working. :( I am trying the snippet in the comments there in the Playground to no avail…

TomasHubelbauer avatar May 23 '23 18:05 TomasHubelbauer

No worry, there's a lot work remaining anyway. Thanks for your effort.

harttle avatar May 23 '23 18:05 harttle

@harttle I added tests for push in #614 and turns out that implementation is fine, the reason it doesn't work on the Playground must be something else. Locally I had a problem running build:docs:

+ ./bin/build-contributors.sh
sed: -e: No such file or directory
sed: 1: "docs/themes/navy/layout ...": extra characters at the end of d command

But on CI there is no such issue: https://github.com/harttle/liquidjs/actions/runs/5060635227/jobs/9083770703

So I don't think this is why push is not working on the Playground. Do you have any ideas on why else it might be?

TomasHubelbauer avatar May 25 '23 19:05 TomasHubelbauer

It'll be great to have these filters! Except for Jekyll logic specific ones like relative_url.

@harttle - Any chance that the group_by filter can be added?

bzerangue avatar Apr 08 '24 16:04 bzerangue

Added group_by, group_by_exp, find, find_exp in the latest version, @bzerangue please try v10.11.0

harttle avatar Apr 14 '24 11:04 harttle

Awesome! Thank you! I will check it out.

On Sun, Apr 14, 2024 at 6:38 AM Jun Yang @.***> wrote:

Added group_by, group_by_exp, find, find_exp in the latest version, @bzerangue https://github.com/bzerangue please try v10.11.0

— Reply to this email directly, view it on GitHub https://github.com/harttle/liquidjs/issues/443#issuecomment-2054019519, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAGUG2YIGJH3SYRJMHIZFDY5JTDNAVCNFSM5MK4IPU2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBVGQYDCOJVGE4Q . You are receiving this because you were mentioned.Message ID: @.***>

bzerangue avatar Apr 16 '24 02:04 bzerangue