jmd-format 0.1.1 → 0.1.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/serializer.js +18 -2
package/package.json
CHANGED
package/src/serializer.js
CHANGED
|
@@ -29,6 +29,19 @@ export function* serializeLines(value, label = 'Document', frontmatter = null) {
|
|
|
29
29
|
for (const ln of lines) yield ln + '\n'
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// Mode markers attach directly to `#` in the root heading: `#- Order`,
|
|
33
|
+
// `#? Order`, `#! Order`. Callers pass the mark as a `- `, `? ` or `! `
|
|
34
|
+
// prefix on `label`; the serializer attaches it to `#` without a space
|
|
35
|
+
// between them. Plain data documents (no prefix) emit `# Label`.
|
|
36
|
+
function splitLabel(label) {
|
|
37
|
+
if (label.length >= 2
|
|
38
|
+
&& (label[0] === '-' || label[0] === '?' || label[0] === '!')
|
|
39
|
+
&& label[1] === ' ') {
|
|
40
|
+
return { mark: label[0], rest: label.slice(2) }
|
|
41
|
+
}
|
|
42
|
+
return { mark: '', rest: label }
|
|
43
|
+
}
|
|
44
|
+
|
|
32
45
|
function emitDocument(value, label, frontmatter, lines) {
|
|
33
46
|
if (frontmatter && Object.keys(frontmatter).length > 0) {
|
|
34
47
|
for (const [k, v] of Object.entries(frontmatter)) {
|
|
@@ -38,12 +51,15 @@ function emitDocument(value, label, frontmatter, lines) {
|
|
|
38
51
|
lines.push('')
|
|
39
52
|
}
|
|
40
53
|
|
|
54
|
+
const { mark, rest } = splitLabel(label)
|
|
55
|
+
const prefix = '#' + mark + ' '
|
|
56
|
+
|
|
41
57
|
if (Array.isArray(value)) {
|
|
42
|
-
const head =
|
|
58
|
+
const head = rest === '[]' ? prefix + '[]' : prefix + rest + '[]'
|
|
43
59
|
lines.push(head)
|
|
44
60
|
writeArrayItems(value, lines, 1)
|
|
45
61
|
} else if (value !== null && typeof value === 'object') {
|
|
46
|
-
lines.push(
|
|
62
|
+
lines.push(prefix + rest)
|
|
47
63
|
writeObjectFields(value, lines, 1)
|
|
48
64
|
} else {
|
|
49
65
|
throw new TypeError('Root value must be an object or array')
|