-
Notifications
You must be signed in to change notification settings - Fork 61.6k
/
Copy pathliquid-utils.js
48 lines (39 loc) · 1.33 KB
/
liquid-utils.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
import { Tokenizer } from 'liquidjs'
const liquidTokenCache = new Map()
export function getLiquidTokens(content, { noCache = false } = {}) {
if (!content) return []
if (noCache) {
const tokenizer = new Tokenizer(content)
return tokenizer.readTopLevelTokens()
}
if (liquidTokenCache.has(content)) {
return liquidTokenCache.get(content)
}
const tokenizer = new Tokenizer(content)
const tokens = tokenizer.readTopLevelTokens()
liquidTokenCache.set(content, tokens)
return liquidTokenCache.get(content)
}
export const OUTPUT_OPEN = '{%'
export const OUTPUT_CLOSE = '%}'
export const TAG_OPEN = '{{'
export const TAG_CLOSE = '}}'
export const conditionalTags = ['if', 'elseif', 'unless', 'case', 'ifversion']
export function getPositionData(token, lines) {
// Liquid indexes are 0-based, but we want to
// covert to the system used by Markdownlint
const begin = token.begin + 1
const end = token.end + 1
// Account for the newline character at the end
// of each line that is not represented in the
// `lines` array
const lineLengths = lines.map((line) => line.length + 1)
let count = begin
let lineNumber = 1
for (const lineLength of lineLengths) {
if (count - lineLength <= 0) break
count = count - lineLength
lineNumber++
}
return { lineNumber, column: count, length: end - begin }
}