-
Notifications
You must be signed in to change notification settings - Fork 9
/
marked-forms.js
219 lines (168 loc) · 6.45 KB
/
marked-forms.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
/**
* marked-forms.js
*
* forms-renderer for marked.js
* generates labels and input controls from [text ?input?](name)
*
* usage: marked.use(markedForms(opts))
*
* Copyright (c) 2015-2024 Jürgen Leschner - github.com/jldec - MIT license
*
**/
/*eslint no-unused-vars: "off"*/
module.exports = function markedForms(opts) {
opts = opts || {};
// for state machine used by renderOption
var listState = { pending:'' };
return {
renderer: { link:link, listitem:listitem, list:list, paragraph:paragraph },
tokenizer: opts.allowSpacesInLinks ? { link: tokenizeLink } : {}
};
//--//--//--//--//--//--//--//--//--//--//
// patch the link tokenizer regexp on first usage (ONLY if opts.allowSpacesInLinks)
function tokenizeLink(src) {
if (!this._marked_forms_patched_link_rule) {
var rules = this.rules.inline;
rules.link = new RegExp(rules.link.source.replace('|[^\\s\\x00-\\x1f', '|[^"\\x00-\\x1f'));
this._marked_forms_patched_link_rule = true;
}
return false;
}
// markdown link syntax extension for forms
function link(href, title, text) {
var reLabelFirst = /^(.*?)\s*\?([^?\s]*)\?(\*?)(X?)(H?)(M?)$/;
var reLabelAfter = /^\?([^?\s]*)\?(\*?)(X?)(H?)(M?)\s*(.*)$/;
var m = text.match(reLabelFirst);
if (m) return renderInput(m[1], m[2], m[3], m[4], m[5], m[6], href, title, true);
m = text.match(reLabelAfter);
if (m) return renderInput(m[6], m[1], m[2], m[3], m[4], m[5], href, title, false);
return false; // fallbacks.link.call(this, href, title, text);
}
// capture listitems for select, checklist, radiolist
function listitem(text) {
if (inList()) {
// capture value in trailing "text" - unescape makes regexp work
var m = unescapeQuotes(text).match(/^(.*)\s+"([^"]*)"\s*$/);
var txt = m ? escapeQuotes(m[1]) : text;
var val = m ? escapeQuotes(m[2]) : text;
return renderOption(txt, val);
}
return false; // fallbacks.listitem.call(this, text);
}
// strip p tags while collecting listitems
function paragraph(text) {
if (inList()) return text;
return false; // fallbacks.paragraph(text);
}
// rendering the list terminates listitem collector
function list(body, ordered) {
if (inList()) return body + endList();
return false; // fallbacks.list.call(this, body, ordered);
}
function renderInput(text, type, required, checked, hidden, modern, name, css, labelFirst) {
required = required ? ' required' : '';
checked = checked ? ' checked' : '';
hidden = hidden ? ' style="display:none;"' : '';
var disabled = hidden ? ' disabled' : ''; // hidden fields are also disabled
css = (required + (css ? ' ' + css : '')).slice(1);
var value = '';
if (type === 'submit' || type === 'button' || type === 'hidden') {
value = text;
text = '';
} else if (type === 'checkbox' || type === 'radio') {
value = 'checked';
}
if ( ! (type === 'submit' || type === 'button' || type === 'label')) {
name = name || text;
}
if (type === 'submit') {
hidden = disabled = ''; // don't allow submit to be hidden/disabled - breaks chrome auto-validation
}
if (name === '-') { name = ''; }
var id = name && name.toLowerCase().replace(/[^\w]+/g, '-');
var labelfor = id;
if (!modern && (type === 'checklist' || type === 'radiolist')) {
labelfor = '';
}
var label = text ?
'\n<label' + hidden + attr('for', labelfor) + attr('class', css) + '>' + text + '</label>' :
'';
var out = endList();
if (type === 'label') return out + label;
var el = 'input';
if (type === 'select' || type === 'checklist' || type === 'radiolist') {
el = (type === 'select' ? 'select' : (modern ? 'ul' : ''));
startList(id, type, name, el, label, labelFirst, modern, required, checked);
if (modern && !css) { css = type; }
if (el === 'ul') { name=''; }
type = '';
}
if (type === 'textarea') {
el = type;
type = '';
}
var input = el ?
'\n<' + el + hidden + disabled + required + checked +
attr('type', type) +
attr('name', name) +
attr('value', value) +
attr('id', id) +
attr('class', css) + '>' :
'';
if (el === 'textarea') { input += '</' + el + '>'; }
if (labelFirst) { out += label + input; }
else if (inList()) { out += input; }
else { out += input + label; }
return out;
}
function renderOption(text, value) {
var out;
var list = listState;
if (list.type === 'select') {
out = '\n<option' + attr('name', list.name) + attr('value', value, true) + '>' ;
return out + text + '</option>';
}
var id = list.modern ? (list.id + '-' + (++list.count)) : '';
var type = {checklist:'checkbox', radiolist:'radio'}[list.type];
var openLabel = text ? '\n<label' + attr('class', type) + attr('for', id) + '>' : '';
var closeLabel = text ? '</label>' : '';
out = '<input' + list.required + list.checked + attr('id', id) + attr('class', list.required) + attr('type', type) + attr('name', list.name) + attr('value', value, true) + '>' ;
if ( list.modern && list.labelFirst) return '<li class="' + type + '">' + openLabel + text + closeLabel + out + '</li>';
if ( list.modern && !list.labelFirst) return '<li class="' + type + '">' + out + openLabel + text + closeLabel + '</li>';
if (!list.modern && list.labelFirst) return openLabel + text + out + closeLabel;
if (!list.modern && !list.labelFirst) return openLabel + out + text + closeLabel;
}
// mini state machine for listitem capture
// used for select, checklist, and radiolist
function startList(id, type, name, el, label, labelFirst, modern, required, checked) {
listState = {
pending : '\n' + (el ? '</' + el + '>' : '') + (labelFirst ? '' : label ),
id : id,
count : 0,
type : type,
name : (type !== 'select' ? name : ''),
labelFirst : labelFirst,
modern : modern,
required : required,
checked : checked
};
}
function inList() {
return !!listState.pending;
}
function endList() {
var out = listState.pending;
listState = { pending:'' };
return out;
}
// utility
function attr(nme, val, all) {
return val || all ? ' ' + nme + '="' + escapeQuotes(val) + '"' : '';
}
function escapeQuotes(s) {
return s.replace(/"/g, '"');
}
function unescapeQuotes(s) {
return s.replace(/"/g, '"');
}
};