forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liquid.js
63 lines (52 loc) · 2.09 KB
/
liquid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { Liquid, defaultOperators } = require('liquidjs')
const path = require('path')
const semver = require('semver')
// GHE versions are not valid SemVer, but can be coerced...
// https://github.com/npm/node-semver#coercion
function matchesVersionString (input) {
return typeof input === 'string' && input.match(/^(?:[a-z](?:[a-z-]*[a-z])?@)?\d+(?:\.\d+)*/)
}
// Support new version formats where version = plan@release
// e.g., [email protected], where enterprise-server is the plan and 2.21 is the release
// e.g., free-pro-team@latest, where free-pro-team is the plan and latest is the release
// in addition to legacy formats where the version passed is simply 2.21
function splitVersion (version) {
// The default plan when working with versions is "enterprise-server".
// Default to that value here to support backward compatibility from before
// plans were explicitly included.
let plan = 'enterprise-server'
let release
const lastIndex = version.lastIndexOf('@')
if (lastIndex === -1) {
release = version
} else {
plan = version.slice(0, lastIndex)
release = version.slice(lastIndex + 1)
}
return [plan, release]
}
const engine = new Liquid({
root: path.join(process.cwd(), 'includes'),
extname: '.html',
dynamicPartials: false,
operators: {
...defaultOperators,
ver_gt: (left, right) => {
if (!matchesVersionString(left)) return false
if (!matchesVersionString(right)) return false
const [leftPlan, leftRelease] = splitVersion(left)
const [rightPlan, rightRelease] = splitVersion(right)
if (leftPlan !== rightPlan) return false
return semver.gt(semver.coerce(leftRelease), semver.coerce(rightRelease))
},
ver_lt: (left, right) => {
if (!matchesVersionString(left)) return false
if (!matchesVersionString(right)) return false
const [leftPlan, leftRelease] = splitVersion(left)
const [rightPlan, rightRelease] = splitVersion(right)
if (leftPlan !== rightPlan) return false
return semver.lt(semver.coerce(leftRelease), semver.coerce(rightRelease))
}
}
})
module.exports = engine