This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.js
280 lines (251 loc) · 8.99 KB
/
markdown.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*global marked*/
// Module core/markdown
// Handles the optional markdown processing.
//
// Markdown support is optional. It is enabled by setting the `format`
// property of the configuration object to "markdown."
//
// We use marked for parsing Markdown.
//
// Note that the content of SECTION elements, and elements with a
// class name of "note", "issue" or "req" are also parsed.
//
// The HTML created by the Markdown parser is turned into a nested
// structure of SECTION elements, following the strucutre given by
// the headings. For example, the following markup:
//
// Title
// -----
//
// ### Subtitle ###
//
// Here's some text.
//
// ### Another subtitle ###
//
// More text.
//
// will be transformed into:
//
// <section>
// <h2>Title</h2>
// <section>
// <h3>Subtitle</h3>
// <p>Here's some text.</p>
// </section>
// <section>
// <h3>Another subtitle</h3>
// <p>More text.</p>
// </section>
// </section>
//
define(
['core/marked'],
function (coreMarked) {
marked.setOptions({
gfm: false,
pedantic: false,
sanitize: false
});
function makeBuilder(doc) {
var root = doc.createDocumentFragment()
, stack = [root]
, current = root
, HEADERS = /H[1-6]/
;
function findPosition(header) {
return parseInt(header.tagName.charAt(1), 10);
}
function findParent(position) {
var parent;
while (position > 0) {
position--;
parent = stack[position];
if (parent) return parent;
}
}
function findHeader(node) {
node = node.firstChild;
while (node) {
if (HEADERS.test(node.tagName)) {
return node;
}
node = node.nextSibling;
}
return null;
}
function addHeader(header) {
var section = doc.createElement('section')
, position = findPosition(header)
;
section.appendChild(header);
findParent(position).appendChild(section);
stack[position] = section;
stack.length = position + 1;
current = section;
}
function addSection(node, process) {
var header = findHeader(node)
, position = header ? findPosition(header) : 1
, parent = findParent(position)
;
if (header) {
node.removeChild(header);
}
node.appendChild(process(node));
if (header) {
node.insertBefore(header, node.firstChild);
}
parent.appendChild(node);
current = parent;
}
function addElement(node) {
current.appendChild(node);
}
function getRoot() {
return root;
}
return {
addHeader: addHeader,
addSection: addSection,
addElement: addElement,
getRoot: getRoot
};
}
return {
toHTML: function(text) {
// As markdown is pulled from HTML > is already escaped, and
// thus blockquotes aren't picked up by the parser. This fixes
// it.
text = text.replace(/>/g, '>');
text = this.removeLeftPadding(text);
return marked(text);
},
removeLeftPadding: function(text) {
// Handles markdown content being nested
// inside elements with soft tabs. E.g.:
// <div>
// This is a title
// ---------------
//
// And this more text.
// </div
//
// Gets turned into:
// <div>
// <h2>This is a title</h2>
// <p>And this more text.</p>
// </div
//
// Rather than:
// <div>
// <pre><code>This is a title
// ---------------
//
// And this more text.</code></pre>
// </div
var match = text.match(/\n[ ]+\S/g)
, current
, min
;
if (match) {
min = match[0].length - 2;
for (var i = 0, length = match.length; i < length; i++) {
current = match[i].length - 2;
if (typeof min == 'undefined' || min > current) {
min = current;
}
}
var re = new RegExp("\n[ ]{0," + min + "}", "g");
text = text.replace(re, '\n');
}
return text;
},
processBody: function(doc) {
var fragment = doc.createDocumentFragment()
, div = doc.createElement('div')
, node
;
div.innerHTML = this.toHTML(doc.body.innerHTML);
while (node = div.firstChild) {
fragment.appendChild(node);
}
return fragment;
},
processSections: function(doc) {
var self = this;
$('section', doc).each(function() {
this.innerHTML = self.toHTML(this.innerHTML);
});
},
processIssuesNotesAndReqs: function(doc) {
var div = doc.createElement('div');
var self = this;
$('.issue, .note, .req', doc).each(function() {
div.innerHTML = self.toHTML(this.innerHTML);
this.innerHTML = '';
var node = div.firstChild;
while (node.firstChild) {
this.appendChild(node.firstChild);
}
});
},
structure: function(fragment, doc) {
function process(root) {
var node
, tagName
, stack = makeBuilder(doc)
;
while (node = root.firstChild) {
if (node.nodeType !== 1) {
root.removeChild(node);
continue;
}
tagName = node.tagName.toLowerCase();
switch (tagName) {
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
stack.addHeader(node);
break;
case 'section':
stack.addSection(node, process);
break;
default:
stack.addElement(node);
}
}
return stack.getRoot();
}
return process(fragment);
},
run: function (conf, doc, cb, msg) {
msg.pub("start", "core/markdown");
if (conf.format === 'markdown') {
// Marked, the Markdown implementation we're currently using
// parses markdown nested in markup (unless it's in a section element).
// Turns out this is both what we need and generally not what other
// parsers do.
// In case we switch to another parser later on, we'll need to
// uncomment the below line of code.
//
// this.processIssuesNotesAndReqs(doc);
this.processSections(doc);
// the processing done here blows away the ReSpec UI (or rather, the elements
// that it needs to reference). So we save a reference to the original element
// and re-inject it later
var $rsUI = $("#respec-ui");
var fragment = this.structure(this.processBody(doc), doc);
doc.body.innerHTML = '';
doc.body.appendChild(fragment);
if ($rsUI.length) $("#respec-ui").replaceWith($rsUI);
}
msg.pub("end", "core/markdown");
cb();
}
};
}
);