@yegor256/dogent 0.7.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/yaml.js +27 -14
package/package.json
CHANGED
package/src/yaml.js
CHANGED
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
*
|
|
11
11
|
* A frontmatter block read as a flat YAML mapping. Splits itself line by
|
|
12
12
|
* line and emits one pair per top-level "key: value", carrying the key,
|
|
13
|
-
* the value, and the absolute line the key sits on.
|
|
14
|
-
*
|
|
13
|
+
* the value, and the absolute line the key sits on. Folds a block scalar
|
|
14
|
+
* value ("|" or ">") from its indented continuation lines into one value.
|
|
15
|
+
* Nested mappings, blank lines, and comments hold no keys and yield nothing.
|
|
15
16
|
*/
|
|
16
17
|
class Yaml {
|
|
17
18
|
constructor(text, base) {
|
|
@@ -19,18 +20,30 @@ class Yaml {
|
|
|
19
20
|
this.base = base;
|
|
20
21
|
}
|
|
21
22
|
pairs() {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.map((line, index) =>
|
|
25
|
-
.filter((
|
|
26
|
-
.map((
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
23
|
+
const lines = this.text.split('\n');
|
|
24
|
+
return lines
|
|
25
|
+
.map((line, index) => index)
|
|
26
|
+
.filter((index) => /^[^\s#][^:]*:/u.test(lines[index]))
|
|
27
|
+
.map((index) => this.pair(lines, index));
|
|
28
|
+
}
|
|
29
|
+
pair(lines, index) {
|
|
30
|
+
const colon = lines[index].indexOf(':');
|
|
31
|
+
const value = lines[index].slice(colon + 1).trim();
|
|
32
|
+
return {
|
|
33
|
+
key: lines[index].slice(0, colon).trim(),
|
|
34
|
+
value: /^[|>][+-]?\d*$/u.test(value) ? Yaml.fold(lines, index) : value,
|
|
35
|
+
row: this.base + index
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
static fold(lines, index) {
|
|
39
|
+
const rest = lines.slice(index + 1);
|
|
40
|
+
const stop = rest.findIndex((line) => line.trim() !== '' && !/^\s/u.test(line));
|
|
41
|
+
const end = stop === -1 ? rest.length : stop;
|
|
42
|
+
return rest
|
|
43
|
+
.slice(0, end)
|
|
44
|
+
.map((line) => line.trim())
|
|
45
|
+
.join(' ')
|
|
46
|
+
.trim();
|
|
34
47
|
}
|
|
35
48
|
}
|
|
36
49
|
|