forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.js
More file actions
61 lines (54 loc) · 1.96 KB
/
data.js
File metadata and controls
61 lines (54 loc) · 1.96 KB
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
import { TokenizationError } from 'liquidjs'
import { THROW_ON_EMPTY, DataReferenceError } from './error-handling.js'
import { getDataByLanguage } from '#src/data-directory/lib/get-data.js'
const Syntax = /([a-z0-9/\\_.\-[\]]+)/i
const SyntaxHelp = "Syntax Error in 'data' - Valid syntax: data [path]"
export default {
parse(tagToken) {
if (!tagToken || !Syntax.test(tagToken.args)) {
throw new TokenizationError(SyntaxHelp, tagToken)
}
this.path = tagToken.args
this.tagToken = tagToken
},
async render(scope) {
let text = getDataByLanguage(this.path, scope.environments.currentLanguage)
if (text === undefined) {
if (scope.environments.currentLanguage === 'en') {
const message = `Can't find the key 'data ${this.path}' in the scope.`
if (THROW_ON_EMPTY) {
throw new DataReferenceError(message)
}
console.warn(message)
}
return
}
// Any time what we're about to replace in here has more than one line,
// if the use of `{% data ... %}` was itself indented, from the left,
// keep *that* indentation, in replaced output, for every line.
//
// For example:
//
// 1. Bullet point
// {% data variables.foo.bar %}
//
// In this example, the `{% data ...` starts with 3 whitespaces
// (based on the `1. Bull...` in the example). So put 3 whitespaces
// in front every line of the output.
if (text.split('\n').length > 0) {
const { input, begin } = this.tagToken
let i = 1
while (input.charAt(begin - i) === ' ') {
i++ // this goes one character "to the left"
}
const goBack = input.slice(begin - i, begin)
if (goBack.charAt(0) === '\n' && goBack.length > 1) {
const numSpaces = goBack.length - 1
text = text.trim().replace(/^/gm, ' '.repeat(numSpaces)).trim()
}
} else {
text = text.trim()
}
return this.liquid.parseAndRender(text, scope.environments)
},
}