Skip to content

Commit 4955e75

Browse files
committed
feat: date filters from Jekyll
1 parent 842b45c commit 4955e75

14 files changed

+305
-31
lines changed

docs/source/_data/sidebar.yml

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ filters:
3636
compact: compact.html
3737
concat: concat.html
3838
date: date.html
39+
date_to_long_string: date_to_long_string.html
40+
date_to_rfc822: date_to_rfc822.html
41+
date_to_string: date_to_string.html
42+
date_to_xmlschema: date_to_xmlschema.html
3943
default: default.html
4044
divided_by: divided_by.html
4145
downcase: downcase.html
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: date_to_long_string
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
Convert a date to long format. Same with Jekyll `date_to_long_string` filter.
7+
8+
Input
9+
```liquid
10+
{{ site.time | date_to_long_string }}
11+
```
12+
13+
Output
14+
```text
15+
07 November 2008
16+
```
17+
18+
Input
19+
```liquid
20+
{{ site.time | date_to_long_string: "ordinal" }}
21+
```
22+
23+
Output
24+
```text
25+
7th November 2008
26+
```
27+
28+
Note that JavaScript `Date` has not timezone information, see [date][date] filter for details.
29+
30+
[date]: ./date.html

docs/source/filters/date_to_rfc822.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: date_to_rfc822
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
Convert a Date into the RFC-822 format used for RSS feeds, same as Jekyll filter `date_to_rfc822`.
7+
8+
Input
9+
```liquid
10+
{{ site.time | date_to_rfc822 }}
11+
```
12+
13+
Output
14+
```text
15+
Mon, 07 Nov 2008 13:07:54 -0800
16+
```
17+
18+
Note that JavaScript `Date` has not timezone information, see [date][date] filter for details.
19+
20+
[date]: ./date.html

docs/source/filters/date_to_string.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: date_to_string
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
Convert a date to short format. Same with Jekyll `date_to_string` filter.
7+
8+
Input
9+
```liquid
10+
{{ site.time | date_to_string }}
11+
```
12+
13+
Output
14+
```text
15+
07 Nov 2008
16+
```
17+
18+
Input
19+
```liquid
20+
{{ site.time | date_to_string: "ordinal", "US" }}
21+
```
22+
23+
Output
24+
```text
25+
Nov 7th, 2008
26+
```
27+
28+
Note that JavaScript `Date` has not timezone information, see [date][date] filter for details.
29+
30+
[date]: ./date.html
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: date_to_xmlschema
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
Convert a Date into XML Schema (ISO 8601) format, same as Jekyll filter `date_to_xmlschema`.
7+
8+
Input
9+
```liquid
10+
{{ site.time | date_to_xmlschema }}
11+
```
12+
13+
Output
14+
```text
15+
2008-11-07T13:07:54-08:00
16+
```
17+
18+
Note that JavaScript `Date` has not timezone information, see [date][date] filter for details.
19+
20+
[date]: ./date.html

docs/source/filters/overview.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Math | plus, minus, modulo, times, floor, ceil, round, divided_by, abs, at_least
1313
String | 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
1414
HTML/URI | escape, escape_once, url_encode, url_decode, strip_html, newline_to_br
1515
Array | 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
16-
Date | date
16+
Date | date, date_to_xmlschema, date_to_rfc822, date_to_string, date_to_long_string
1717
Misc | default, json, jsonify, inspect, raw, to_integer
1818

1919
[shopify/liquid]: https://github.com/Shopify/liquid
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: date_to_long_string
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
把日期转换为长格式(只支持 US/UK 两种),与 Jekyll 的 `date_to_long_string` 过滤器一样。
7+
8+
输入
9+
```liquid
10+
{{ site.time | date_to_long_string }}
11+
```
12+
13+
输出
14+
```text
15+
07 November 2008
16+
```
17+
18+
输入
19+
```liquid
20+
{{ site.time | date_to_long_string: "ordinal" }}
21+
```
22+
23+
输出
24+
```text
25+
7th November 2008
26+
```
27+
28+
29+
注意 JavaScript `Date` 没有时区信息,详情请参考 [date][date] 过滤器。
30+
31+
[date]: ./date.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: date_to_rfc822
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
把日期转换为 RFC-822 格式用于 RSS feed,与 Jekyll 的 `date_to_rfc822` 过滤器一样。
7+
8+
输入
9+
```liquid
10+
{{ site.time | date_to_rfc822 }}
11+
```
12+
13+
输入
14+
```text
15+
Mon, 07 Nov 2008 13:07:54 -0800
16+
```
17+
18+
注意 JavaScript `Date` 没有时区信息,详情请参考 [date][date] 过滤器。
19+
20+
[date]: ./date.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: date_to_string
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
把日期转换为短格式(只支持 US/UK 两种),与 Jekyll 的 `date_to_string` 过滤器一样。
7+
8+
输入
9+
```liquid
10+
{{ site.time | date_to_string }}
11+
```
12+
13+
输出
14+
```text
15+
07 Nov 2008
16+
```
17+
18+
输入
19+
```liquid
20+
{{ site.time | date_to_string: "ordinal", "US" }}
21+
```
22+
23+
输出
24+
```text
25+
Nov 7th, 2008
26+
```
27+
28+
注意 JavaScript `Date` 没有时区信息,详情请参考 [date][date] 过滤器。
29+
30+
[date]: ./date.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: date_to_xmlschema
3+
---
4+
{% since %}v10.13.0{% endsince %}
5+
6+
把日期转换为 XML Schema (ISO 8601) 格式,与 Jekyll 的 `date_to_xmlschema` 过滤器一样。
7+
8+
输入
9+
```liquid
10+
{{ site.time | date_to_xmlschema }}
11+
```
12+
13+
输出
14+
```text
15+
2008-11-07T13:07:54-08:00
16+
```
17+
18+
注意 JavaScript `Date` 没有时区信息,详情请参考 [date][date] 过滤器。
19+
20+
[date]: ./date.html

docs/source/zh-cn/filters/overview.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LiquidJS 共支持 40+ 个过滤器,可以分为如下几类:
1313
字符串 | 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
1414
HTML/URI | escape, escape_once, url_encode, url_decode, strip_html, newline_to_br
1515
数组 | 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
16-
日期 | date
16+
日期 | date, date_to_xmlschema, date_to_rfc822, date_to_string, date_to_long_string
1717
其他 | default, json, jsonify, inspect, raw, to_integer
1818

1919
[shopify/liquid]: https://github.com/Shopify/liquid

src/filters/date.ts

+39-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
import { toValue, stringify, isString, isNumber, TimezoneDate, LiquidDate, strftime, isNil } from '../util'
22
import { FilterImpl } from '../template'
3+
import { LiquidOptions } from '../liquid-options'
34

45
export function date (this: FilterImpl, v: string | Date, format?: string, timezoneOffset?: number | string) {
5-
const opts = this.context.opts
6+
const date = parseDate(v, this.context.opts, timezoneOffset)
7+
if (!date) return v
8+
format = toValue(format)
9+
format = isNil(format) ? this.context.opts.dateFormat : stringify(format)
10+
return strftime(date, format)
11+
}
12+
13+
export function date_to_xmlschema (this: FilterImpl, v: string | Date) {
14+
return date.call(this, v, '%Y-%m-%dT%H:%M:%S%:z')
15+
}
16+
17+
export function date_to_rfc822 (this: FilterImpl, v: string | Date) {
18+
return date.call(this, v, '%a, %d %b %Y %H:%M:%S %z')
19+
}
20+
21+
export function date_to_string (this: FilterImpl, v: string | Date, type?: string, style?: string) {
22+
return stringify_date.call(this, v, '%b', type, style)
23+
}
24+
25+
export function date_to_long_string (this: FilterImpl, v: string | Date, type?: string, style?: string) {
26+
return stringify_date.call(this, v, '%B', type, style)
27+
}
28+
29+
function stringify_date (this: FilterImpl, v: string | Date, month_type: string, type?: string, style?: string) {
30+
const date = parseDate(v, this.context.opts)
31+
if (!date) return v
32+
if (type === 'ordinal') {
33+
const d = date.getDate()
34+
return style === 'US'
35+
? strftime(date, `${month_type} ${d}%q, %Y`)
36+
: strftime(date, `${d}%q ${month_type} %Y`)
37+
}
38+
return strftime(date, `%d ${month_type} %Y`)
39+
}
40+
41+
function parseDate (v: string | Date, opts: LiquidOptions, timezoneOffset?: number | string): LiquidDate | undefined {
642
let date: LiquidDate
743
v = toValue(v)
8-
format = toValue(format)
9-
if (isNil(format)) format = opts.dateFormat
10-
else format = stringify(format)
1144
if (v === 'now' || v === 'today') {
1245
date = new Date()
1346
} else if (isNumber(v)) {
@@ -23,13 +56,13 @@ export function date (this: FilterImpl, v: string | Date, format?: string, timez
2356
} else {
2457
date = v
2558
}
26-
if (!isValidDate(date)) return v
59+
if (!isValidDate(date)) return
2760
if (timezoneOffset !== undefined) {
2861
date = new TimezoneDate(date, timezoneOffset)
2962
} else if (!(date instanceof TimezoneDate) && opts.timezoneOffset !== undefined) {
3063
date = new TimezoneDate(date, opts.timezoneOffset)
3164
}
32-
return strftime(date, format)
65+
return date
3366
}
3467

3568
function isValidDate (date: any): date is Date {

src/util/strftime.ts

+8-23
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,16 @@ function isLeapYear (d: LiquidDate) {
4545
const year = d.getFullYear()
4646
return !!((year & 3) === 0 && (year % 100 || (year % 400 === 0 && year)))
4747
}
48-
function getSuffix (d: LiquidDate) {
48+
function ordinal (d: LiquidDate) {
4949
const date = d.getDate()
50+
if ([11, 12, 13].includes(date)) return 'th'
5051

51-
let suffix = 'th'
52-
53-
switch (date) {
54-
case 11:
55-
case 12:
56-
case 13:
57-
break
58-
default:
59-
switch (date % 10) {
60-
case 1:
61-
suffix = 'st'
62-
break
63-
case 2:
64-
suffix = 'nd'
65-
break
66-
case 3:
67-
suffix = 'rd'
68-
break
69-
}
52+
switch (date % 10) {
53+
case 1: return 'st'
54+
case 2: return 'nd'
55+
case 3: return 'rd'
56+
default: return 'th'
7057
}
71-
72-
return suffix
7358
}
7459
function century (d: LiquidDate) {
7560
return parseInt(d.getFullYear().toString().substring(0, 2), 10)
@@ -138,7 +123,7 @@ const formatCodes = {
138123
},
139124
p: (d: LiquidDate) => (d.getHours() < 12 ? 'AM' : 'PM'),
140125
P: (d: LiquidDate) => (d.getHours() < 12 ? 'am' : 'pm'),
141-
q: (d: LiquidDate) => getSuffix(d),
126+
q: (d: LiquidDate) => ordinal(d),
142127
s: (d: LiquidDate) => Math.round(d.getTime() / 1000),
143128
S: (d: LiquidDate) => d.getSeconds(),
144129
u: (d: LiquidDate) => d.getDay() || 7,

0 commit comments

Comments
 (0)