-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringify.js
244 lines (235 loc) · 7.46 KB
/
stringify.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const GitHubSlugger = require('github-slugger')
const markdownEscape = require('markdown-escape')
const groupSeries = require('commonform-group-series')
const has = require('has')
module.exports = (form, values, options) => {
options = options || {}
values = values || []
let formDepth = options.formDepth || 0
let rendered = ''
if (options.title && !options.frontMatter) {
rendered += `# ${customEscape(options.title)}\n\n`
formDepth++
}
if (options.version && !options.frontMatter) {
rendered += customEscape(options.version) + '\n\n'
}
if (options.frontMatter) {
rendered += '---\n'
if (options.title) rendered += `title: ${options.title}\n`
if (options.version) rendered += `version: ${options.version}\n`
rendered += '---\n\n'
}
if (options.ids) {
options.headingSlugger = new GitHubSlugger()
}
options.referenceSlugger = new GitHubSlugger()
rendered += render(form, values, formDepth, 0, [], options)
return rendered.trim() + '\n'
}
function render (form, values, formDepth, indentationLevel, formAddress, options) {
const groups = groupSeries(form)
let conspicuousMarker = ''
if (form.conspicuous) {
conspicuousMarker = form.conspicuous ? '!!!' : ''
if (groups[0].type === 'paragraph') conspicuousMarker += ' '
else conspicuousMarker += '\n\n'
}
return conspicuousMarker + groups
.map((group, index) => {
if (group.type === 'paragraph') {
return (
(
(indentationLevel || index === 0)
? ''
: (headingFor(formDepth, '(Continuing)', true, options) + '\n\n')
) +
group.content
.map(element => {
const realIndex = form.content.indexOf(element)
const address = formAddress.concat('content', realIndex)
return run(element, address, values, options)
})
.join('')
)
} else { // series
if (!indentationLevel) {
indentationLevel = group.content.every(element => {
return !containsAHeading(element)
})
? 1
: 0
}
const nextFormDepth = formDepth + 1
return group.content
.map(
indentationLevel > 0
? (child, index) => {
let body
let startsWithSeries
if (child.form) {
const firstElement = child.form.content[0]
startsWithSeries = (
typeof firstElement !== 'string' &&
has(firstElement, 'form')
)
const realIndex = form.content.indexOf(child)
const address = formAddress.concat(
'content', realIndex, 'form'
)
body = render(
child.form,
values,
nextFormDepth,
indentationLevel + 1,
address,
options
)
} else {
body = stringifyComponent(child, indentationLevel + 1)
}
let prefix = '-'
if (options.ordered) {
prefix = (index + 1) + '.'
}
return (
' '.repeat(indentationLevel - 1) +
prefix +
(startsWithSeries ? '\n\n' : ' ') +
body
)
}
: child => {
let body
if (child.form) {
const realIndex = form.content.indexOf(child)
const address = formAddress.concat(
'content', realIndex, 'form'
)
body = render(
child.form,
values,
nextFormDepth,
0,
address,
options
)
} else {
body = stringifyComponent(child, 0)
}
return (
headingFor(nextFormDepth, child.heading, false, options) +
'\n\n' +
body
)
}
)
.join('\n\n')
}
})
.join('\n\n')
}
function stringifyComponent (component, indentationLevel) {
let returned
returned = '<'
returned += component.component
if (!returned.endsWith('/')) returned += '/'
returned += component.version
returned += '>'
const substitutions = component.substitutions
const hasSubstitutions = (
Object.keys(substitutions.terms).length > 0 ||
Object.keys(substitutions.headings).length > 0 ||
Object.keys(substitutions.blanks).length > 0
)
if (hasSubstitutions) {
const indent = indentationLevel === 0 ? '' : ' '.repeat(indentationLevel - 1)
returned += ' substituting:\n'
returned += []
.concat(
Object.keys(substitutions.terms).map(from => {
const to = substitutions.terms[from]
return `${indent}- _${to}_ for _${from}_`
})
)
.concat(
Object.keys(substitutions.headings).map(from => {
const to = substitutions.headings[from]
return `${indent}- [${to}]() for [${from}]()`
})
)
.concat(
Object.keys(substitutions.blanks).map(index => {
const value = substitutions.blanks[index]
return `${indent}- "${value}" for blank ${parseInt(index)}`
})
)
.join('\n')
}
return returned
}
function formatHeading (formDepth, text) {
if (formDepth <= 6) {
return '#'.repeat(formDepth) + ' ' + text
}
throw new Error('Form indented too deep.')
}
function headingFor (formDepth, heading, suppressAnchor, options) {
if (heading) {
let returned = ''
if (!suppressAnchor && options.headingSlugger) {
const slug = options.headingSlugger.slug(heading)
returned += `<a id="${slug}"></a>\n`
}
return returned + formatHeading(formDepth, heading)
} else {
return formatHeading(formDepth, '(No Heading)')
}
}
function containsAHeading (child) {
return (
has(child, 'heading') ||
(
child.form &&
child.form.content.some(element => (
has(element, 'form') &&
containsAHeading(element)
))
)
)
}
function run (element, address, values, options) {
if (typeof element === 'string') {
return customEscape(element)
} else if (has(element, 'use')) {
return `_${customEscape(element.use)}_`
} else if (has(element, 'definition')) {
return `**${customEscape(element.definition)}**`
} else if (has(element, 'blank')) {
let value
const match = values.find(function (element) {
return sameAddress(element.blank, address)
})
if (match) value = match.value
return value || '``'
} else if (has(element, 'reference')) {
const heading = element.reference
options.referenceSlugger.reset()
const slug = options.referenceSlugger.slug(heading)
return `[${heading}](#${slug})`
} else if (has(element, 'link')) {
return `<${element.link}>`
} else {
throw new Error(`Invalid type: ${JSON.stringify(element)}`)
}
}
function sameAddress (a, b) {
if (a.length !== b.length) return false
for (let index = 0; index < a.length; index++) {
if (a[index] !== b[index]) return false
}
return true
}
function customEscape (string) {
return markdownEscape(string, ['slashes'])
}