@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/yaml.js +27 -14
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "lint": "eslint .",
41
41
  "test": "mocha 'test/**/*.js' --timeout 60000"
42
42
  },
43
- "version": "0.7.1",
43
+ "version": "0.7.2",
44
44
  "dependencies": {
45
45
  "minimist": "^1.2.8"
46
46
  }
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. Nested mappings,
14
- * blank lines, and comments hold no keys and yield nothing.
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
- return this.text
23
- .split('\n')
24
- .map((line, index) => ({line, row: this.base + index}))
25
- .filter((spot) => /^[^\s#][^:]*:/u.test(spot.line))
26
- .map((spot) => {
27
- const colon = spot.line.indexOf(':');
28
- return {
29
- key: spot.line.slice(0, colon).trim(),
30
- value: spot.line.slice(colon + 1).trim(),
31
- row: spot.row
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