feat: array_to_sentence_string and number_of_words filters from Jekyll, #443

This commit is contained in:
Yang Jun
2024-05-13 00:24:35 +08:00
parent 4561ba3d53
commit 2fd2b819ec
9 changed files with 304 additions and 10 deletions
@@ -0,0 +1,27 @@
---
title: array_to_sentence_string
---
{% since %}v10.13.0{% endsince %}
把数组转化为句子,用于做标签列表。有一个可选的连接词参数。
输入
```liquid
{{ "foo,bar,baz" | split: "," | array_to_sentence_string }}
```
输出
```text
foo, bar, and baz
```
输入
```liquid
{{ "foo,bar,baz" | split: "," | array_to_sentence_string: "or" }}
```
输出
```text
foo, bar, or baz
```
@@ -0,0 +1,49 @@
---
title: number_of_words
---
{% since %}v10.13.0{% endsince %}
计算文本中的单词数。此过滤器接受一个可选参数,用于控制输入字符串中汉字-日语-韩语(CJK)字符的处理方式:
- 将 'cjk' 作为参数传递将会将每个检测到的 CJK 字符计为一个单词,无论是否由空格分隔。
- 将 'auto' (自动检测)作为参数传递与 'cjk' 类似,但如果过滤器用于可能包含或不包含 CJK 字符的字符串,则性能更好。
Input
```liquid
{{ "Hello world!" | number_of_words }}
```
Output
```text
2
```
Input
```liquid
{{ "你好hello世界world" | number_of_words }}
```
Output
```text
1
```
Input
```liquid
{{ "你好hello世界world" | number_of_words: "cjk" }}
```
Output
```text
6
```
Input
```liquid
{{ "你好hello世界world" | number_of_words: "auto" }}
```
Output
```text
6
```
+1 -1
View File
@@ -10,7 +10,7 @@ LiquidJS 共支持 40+ 个过滤器,可以分为如下几类:
类别 | 过滤器
--- | ---
数学 | plus, minus, modulo, times, floor, ceil, round, divided_by, abs, at_least, at_most
字符串 | append, prepend, capitalize, upcase, downcase, strip, lstrip, rstrip, strip_newlines, split, replace, replace_first, replace_last, remove, remove_first, remove_last, truncate, truncatewords, normalize_whitespace
字符串 | append, prepend, capitalize, upcase, downcase, strip, lstrip, rstrip, strip_newlines, split, replace, replace_first, replace_last, remove, remove_first, remove_last, truncate, truncatewords, normalize_whitespace, number_of_words, array_to_sentence_string
HTML/URI | escape, escape_once, url_encode, url_decode, strip_html, newline_to_br, xml_escape, cgi_escape, uri_escape
数组 | slice, map, sort, sort_natural, uniq, where, where_exp, group_by, group_by_exp, find, find_exp, first, last, join, reverse, concat, compact, size, push, pop, shift, unshift
日期 | date, date_to_xmlschema, date_to_rfc822, date_to_string, date_to_long_string