forked from xen/markdown-tg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-tg.js
1417 lines (1307 loc) · 52.7 KB
/
markdown-tg.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @flow */
/**
* Markdown-tg
* ===============
*
* Markdown-tg is simple parser for Telegram subset of Markdown.
* It is not individual project and mostly rip-off of the Simple
* Markdown by the amazing Khan Academy.
*
* It also contains important parts of the other projects, for the
* reference check https://github.com/Khan/simple-markdown/blob/master/simple-markdown.js
*
* LICENSE (MIT):
*
* Some code changes by Mikhail Kashkin 2018.
*
* Most code taken from (c) 2014 Khan Academy.
*
* Portions adapted from marked.js copyright (c) 2011-2014
* Christopher Jeffrey (https://github.com/chjj/).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
(function () {
var CR_NEWLINE_R = /\r\n?/g;
var TAB_R = /\t/g;
var FORMFEED_R = /\f/g
// Turn various crazy whitespace into easy to process things
var preprocess = function (source) {
return source.replace(CR_NEWLINE_R, '\n')
.replace(FORMFEED_R, '')
.replace(TAB_R, ' ');
};
/**
* Creates a parser for a given set of rules, with the precedence
* specified as a list of rules.
*
* @rules: an object containing
* rule type -> {match, order, parse} objects
* (lower order is higher precedence)
* (Note: `order` is added to defaultRules after creation so that
* the `order` of defaultRules in the source matches the `order`
* of defaultRules in terms of `order` fields.)
*
* @returns The resulting parse function, with the following parameters:
* @source: the input source string to be parsed
* @state: an optional object to be threaded through parse
* calls. Allows clients to add stateful operations to
* parsing, such as keeping track of how many levels deep
* some nesting is. For an example use-case, see passage-ref
* parsing in src/widgets/passage/passage-markdown.jsx
*/
var parserFor = function (rules) {
// Sorts rules in order of increasing order, then
// ascending rule name in case of ties.
var ruleList = Object.keys(rules);
ruleList.forEach(function (type) {
var order = rules[type].order;
if ((typeof order !== 'number' || !isFinite(order)) &&
typeof console !== 'undefined') {
console.warn(
"markdown-tg: Invalid order for rule `" + type + "`: " +
order
);
}
});
ruleList.sort(function (typeA, typeB) {
var ruleA = rules[typeA];
var ruleB = rules[typeB];
var orderA = ruleA.order;
var orderB = ruleB.order;
// First sort based on increasing order
if (orderA !== orderB) {
return orderA - orderB;
}
var secondaryOrderA = ruleA.quality ? 0 : 1;
var secondaryOrderB = ruleB.quality ? 0 : 1;
if (secondaryOrderA !== secondaryOrderB) {
return secondaryOrderA - secondaryOrderB;
// Then based on increasing unicode lexicographic ordering
} else if (typeA < typeB) {
return -1;
} else if (typeA > typeB) {
return 1;
} else {
// Rules should never have the same name,
// but this is provided for completeness.
return 0;
}
});
var nestedParse = function (source, state) {
var result = [];
state = state || {};
// We store the previous capture so that match functions can
// use some limited amount of lookbehind. Lists use this to
// ensure they don't match arbitrary '- ' or '* ' in inline
// text (see the list rule for more information).
var prevCapture = "";
while (source) {
// store the best match, it's rule, and quality:
var ruleType = null;
var rule = null;
var capture = null;
var quality = NaN;
// loop control variables:
var i = 0;
var currRuleType = ruleList[0];
var currRule = rules[currRuleType];
do {
var currOrder = currRule.order;
var currCapture = currRule.match(source, state, prevCapture);
if (currCapture) {
var currQuality = currRule.quality ? currRule.quality(
currCapture,
state,
prevCapture
) : 0;
// This should always be true the first time because
// the initial quality is NaN (that's why there's the
// condition negation).
if (!(currQuality <= quality)) {
ruleType = currRuleType;
rule = currRule;
capture = currCapture;
quality = currQuality;
}
}
// Move on to the next item.
// Note that this makes `currRule` be the next item
i++;
currRuleType = ruleList[i];
currRule = rules[currRuleType];
} while (
// keep looping while we're still within the ruleList
currRule && (
// if we don't have a match yet, continue
!capture || (
// or if we have a match, but the next rule is
// at the same order, and has a quality measurement
// functions, then this rule must have a quality
// measurement function (since they are sorted before
// those without), and we need to check if there is
// a better quality match
currRule.order === currOrder &&
currRule.quality
)
)
);
// TODO(aria): Write tests for this
if (!capture) {
throw new Error(
"could not find rule to match content: " + source
);
}
var parsed = rule.parse(capture, nestedParse, state);
// We maintain the same object here so that rules can
// store references to the objects they return and
// modify them later. (oops sorry! but this adds a lot
// of power--see reflinks.)
if (Array.isArray(parsed)) {
Array.prototype.push.apply(result, parsed);
}
else {
// We also let rules override the default type of
// their parsed node if they would like to, so that
// there can be a single output function for all links,
// even if there are several rules to parse them.
if (parsed.type == null) {
parsed.type = ruleType;
}
result.push(parsed);
}
prevCapture = capture[0];
source = source.substring(prevCapture.length);
}
return result;
};
var outerParse = function (source, state) {
return nestedParse(preprocess(source), state);
};
return outerParse;
};
// Creates a match function for an inline scoped element from a regex
var inlineRegex = function (regex) {
var match = function (source, state) {
if (state.inline) {
return regex.exec(source);
} else {
return null;
}
};
match.regex = regex;
return match;
};
// Creates a match function for a block scoped element from a regex
var blockRegex = function (regex) {
var match = function (source, state) {
if (state.inline) {
return null;
} else {
return regex.exec(source);
}
};
match.regex = regex;
return match;
};
// Creates a match function from a regex, ignoring block/inline scope
var anyScopeRegex = function (regex) {
var match = function (source, state) {
return regex.exec(source);
};
match.regex = regex;
return match;
};
var reactFor = function (outputFunc) {
var nestedOutput = function (ast, state) {
state = state || {};
if (Array.isArray(ast)) {
var oldKey = state.key;
var result = [];
// map nestedOutput over the ast, except group any text
// nodes together into a single string output.
var lastWasString = false;
for (var i = 0; i < ast.length; i++) {
state.key = '' + i;
var nodeOut = nestedOutput(ast[i], state);
var isString = (typeof nodeOut === "string");
if (isString && lastWasString) {
result[result.length - 1] += nodeOut;
} else {
result.push(nodeOut);
}
lastWasString = isString;
}
state.key = oldKey;
return result;
} else {
return outputFunc(ast, nestedOutput, state);
}
};
return nestedOutput;
};
var htmlFor = function (outputFunc) {
var nestedOutput = function (ast, state) {
state = state || {};
if (Array.isArray(ast)) {
return ast.map(function (node) {
return nestedOutput(node, state);
}).join("");
} else {
return outputFunc(ast, nestedOutput, state);
}
};
return nestedOutput;
};
var TYPE_SYMBOL =
(typeof Symbol === 'function' && Symbol.for &&
Symbol.for('react.element')) ||
0xeac7;
var reactElement = function (type, key, props) {
// Debugging assertions. To be commented out when committed
// TODO(aria): Figure out a better way of having dev asserts
/*
if (props === null || typeof props !== "object") {
throw new Error("props of " + type + " must be an object");
}
if (key !== null && typeof key !== "string") {
throw new Error("key of " + type + " must be a string, got " + key);
}
*/
return {
$$typeof: TYPE_SYMBOL,
type: type,
key: key,
ref: null,
props: props,
_owner: null
};
};
// Returns a closed HTML tag.
// tagName: Name of HTML tag (eg. "em" or "a")
// content: Inner content of tag
// attributes: Optional extra attributes of tag as an object of key-value pairs
// eg. { "href": "http://google.com" }. Falsey attributes are filtered out.
// isClosed: boolean that controls whether tag is closed or not (eg. img tags).
// defaults to true
var htmlTag = function (tagName, content, attributes, isClosed) {
attributes = attributes || {};
isClosed = typeof isClosed !== 'undefined' ? isClosed : true;
var attributeString = "";
for (var attr in attributes) {
// Removes falsey attributes
if (Object.prototype.hasOwnProperty.call(attributes, attr) &&
attributes[attr]) {
attributeString += " " + attr + '="' + attributes[attr] + '"';
}
}
var unclosedTag = "<" + tagName + attributeString + ">";
if (isClosed) {
return unclosedTag + content + "</" + tagName + ">";
} else {
return unclosedTag;
}
};
var EMPTY_PROPS = {};
var sanitizeUrl = function (url) {
if (url == null) {
return null;
}
try {
var prot = decodeURIComponent(url)
.replace(/[^A-Za-z0-9/:]/g, '')
.toLowerCase();
if (prot.indexOf('javascript:') === 0) {
return null;
}
} catch (e) {
// decodeURIComponent sometimes throws a URIError
// See `decodeURIComponent('a%AFc');`
// http://stackoverflow.com/questions/9064536/javascript-decodeuricomponent-malformed-uri-exception
return null;
}
return url;
};
var UNESCAPE_URL_R = /\\([^0-9A-Za-z\s])/g;
var unescapeUrl = function (rawUrlString) {
return rawUrlString.replace(UNESCAPE_URL_R, "$1");
};
// Parse some content with the parser `parse`, with state.inline
// set to true. Useful for block elements; not generally necessary
// to be used by inline elements (where state.inline is already true.
var parseInline = function (parse, content, state) {
var isCurrentlyInline = state.inline || false;
state.inline = true;
var result = parse(content, state);
state.inline = isCurrentlyInline;
return result;
};
var parseBlock = function (parse, content, state) {
var isCurrentlyInline = state.inline || false;
state.inline = false;
var result = parse(content + "\n\n", state);
state.inline = isCurrentlyInline;
return result;
};
var parseCaptureInline = function (capture, parse, state) {
return {
content: parseInline(parse, capture[1], state)
};
};
var ignoreCapture = function () { return {}; };
// // recognize a `*` `-`, `+`, `1.`, `2.`... list bullet
// var LIST_BULLET = "(?:[*+-]|\\d+\\.)";
// // recognize the start of a list item:
// // leading space plus a bullet plus a space (` * `)
// var LIST_ITEM_PREFIX = "( *)(" + LIST_BULLET + ") +";
// var LIST_ITEM_PREFIX_R = new RegExp("^" + LIST_ITEM_PREFIX);
// // recognize an individual list item:
// // * hi
// // this is part of the same item
// //
// // as is this, which is a new paragraph in the same item
// //
// // * but this is not part of the same item
// var LIST_ITEM_R = new RegExp(
// LIST_ITEM_PREFIX +
// "[^\\n]*(?:\\n" +
// "(?!\\1" + LIST_BULLET + " )[^\\n]*)*(\n|$)",
// "gm"
// );
var BLOCK_END_R = /\n{2,}$/;
// recognize the end of a paragraph block inside a list item:
// two or more newlines at end end of the item
var LIST_BLOCK_END_R = BLOCK_END_R;
var LIST_ITEM_END_R = / *\n+$/;
// check whether a list item has paragraphs: if it does,
// we leave the newlines at the end
// var LIST_R = new RegExp(
// "^( *)(" + LIST_BULLET + ") " +
// "[\\s\\S]+?(?:\n{2,}(?! )" +
// "(?!\\1" + LIST_BULLET + " )\\n*" +
// // the \\s*$ here is so that we can parse the inside of nested
// // lists, where our content might end before we receive two `\n`s
// "|\\s*\n*$)"
// );
var LIST_LOOKBEHIND_R = /(?:^|\n)( *)$/;
// var TABLES = (function () {
// // predefine regexes so we don't have to create them inside functions
// // sure, regex literals should be fast, even inside functions, but they
// // aren't in all browsers.
// var TABLE_HEADER_TRIM = /^ *| *\| *$/g;
// var TABLE_CELLS_TRIM = /\n+$/;
// var PLAIN_TABLE_ROW_TRIM = /^ *\| *| *\| *$/g;
// var NPTABLE_ROW_TRIM = /^ *| *$/g;
// var TABLE_ROW_SPLIT = / *\| */;
// var TABLE_RIGHT_ALIGN = /^ *-+: *$/;
// var TABLE_CENTER_ALIGN = /^ *:-+: *$/;
// var TABLE_LEFT_ALIGN = /^ *:-+ *$/;
// var parseTableAlignCapture = function (alignCapture) {
// if (TABLE_RIGHT_ALIGN.test(alignCapture)) {
// return "right";
// } else if (TABLE_CENTER_ALIGN.test(alignCapture)) {
// return "center";
// } else if (TABLE_LEFT_ALIGN.test(alignCapture)) {
// return "left";
// } else {
// return null;
// }
// };
// var parseTableHeader = function (trimRegex, capture, parse, state) {
// var headerText = capture[1]
// .replace(trimRegex, "")
// .split(TABLE_ROW_SPLIT);
// return headerText.map(function (text) {
// return parse(text, state);
// });
// };
// var parseTableAlign = function (trimRegex, capture, parse, state) {
// var alignText = capture[2]
// .replace(trimRegex, "")
// .split(TABLE_ROW_SPLIT);
// return alignText.map(parseTableAlignCapture);
// };
// var parseTableCells = function (capture, parse, state) {
// var rowsText = capture[3]
// .replace(TABLE_CELLS_TRIM, "")
// .split("\n");
// return rowsText.map(function (rowText) {
// var cellText = rowText
// .replace(PLAIN_TABLE_ROW_TRIM, "")
// .split(TABLE_ROW_SPLIT);
// return cellText.map(function (text) {
// return parse(text, state);
// });
// });
// };
// var parseNpTableCells = function (capture, parse, state) {
// var rowsText = capture[3]
// .replace(TABLE_CELLS_TRIM, "")
// .split("\n");
// return rowsText.map(function (rowText) {
// var cellText = rowText.split(TABLE_ROW_SPLIT);
// return cellText.map(function (text) {
// return parse(text, state);
// });
// });
// };
// var parseTable = function (capture, parse, state) {
// state.inline = true;
// var header = parseTableHeader(TABLE_HEADER_TRIM, capture, parse, state);
// var align = parseTableAlign(TABLE_HEADER_TRIM, capture, parse, state);
// var cells = parseTableCells(capture, parse, state);
// state.inline = false;
// return {
// type: "table",
// header: header,
// align: align,
// cells: cells
// };
// };
// var parseNpTable = function (capture, parse, state) {
// state.inline = true;
// var header = parseTableHeader(NPTABLE_ROW_TRIM, capture, parse, state);
// var align = parseTableAlign(NPTABLE_ROW_TRIM, capture, parse, state);
// var cells = parseNpTableCells(capture, parse, state);
// state.inline = false;
// return {
// type: "table",
// header: header,
// align: align,
// cells: cells
// };
// };
// return {
// parseTable: parseTable,
// parseNpTable: parseNpTable,
// NPTABLE_REGEX: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/
// };
// })();
var LINK_INSIDE = "(?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*";
var LINK_HREF_AND_TITLE =
"\\s*<?((?:[^\\s\\\\]|\\\\.)*?)>?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*";
var AUTOLINK_MAILTO_CHECK_R = /mailto:/i;
var parseRef = function (capture, state, refNode) {
var ref = (capture[2] || capture[1])
.replace(/\s+/g, ' ')
.toLowerCase();
// We store information about previously seen defs on
// state._defs (_ to deconflict with client-defined
// state). If the def for this reflink/refimage has
// already been seen, we can use its target/source
// and title here:
if (state._defs && state._defs[ref]) {
var def = state._defs[ref];
// `refNode` can be a link or an image. Both use
// target and title properties.
refNode.target = def.target;
refNode.title = def.title;
}
// In case we haven't seen our def yet (or if someone
// overwrites that def later on), we add this node
// to the list of ref nodes for that def. Then, when
// we find the def, we can modify this link/image AST
// node :).
// I'm sorry.
state._refs = state._refs || {};
state._refs[ref] = state._refs[ref] || [];
state._refs[ref].push(refNode);
return refNode;
};
/*
Telegram officially support only this subset (from https://core.telegram.org/bots/api#markdown-style):
*bold text*
_italic text_
[inline URL](http://www.example.com/)
[inline mention of a user](tg://user?id=123456789)
`inline fixed-width code`
```block_language
pre-formatted fixed-width code block
```
But some other things it renders also. For example @username and links. We need to support them also.
*/
var defaultRules = {
// heading: {
// match: blockRegex(/^ *(#{1,6}) *([^\n]+?) *#* *(?:\n *)+\n/),
// parse: function (capture, parse, state) {
// return {
// level: capture[1].length,
// content: parseInline(parse, capture[2], state)
// };
// },
// react: function (node, output, state) {
// return reactElement(
// 'h' + node.level,
// state.key,
// {
// children: output(node.content, state)
// }
// );
// },
// html: function (node, output, state) {
// return htmlTag("h" + node.level, output(node.content, state));
// }
// },
// nptable: {
// match: blockRegex(TABLES.NPTABLE_REGEX),
// // For perseus-markdown temporary backcompat:
// regex: TABLES.NPTABLE_REGEX,
// parse: TABLES.parseNpTable
// },
// lheading: {
// match: blockRegex(/^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/),
// parse: function (capture, parse, state) {
// return {
// type: "heading",
// level: capture[2] === '=' ? 1 : 2,
// content: parseInline(parse, capture[1], state)
// };
// }
// },
// hr: {
// match: blockRegex(/^( *[-*_]){3,} *(?:\n *)+\n/),
// parse: ignoreCapture,
// react: function (node, output, state) {
// return reactElement(
// 'hr',
// state.key,
// EMPTY_PROPS
// );
// },
// html: function (node, output, state) {
// return "<hr>";
// }
// },
codeBlock: {
match: blockRegex(/^(?: [^\n]+\n*)+(?:\n *)+\n/),
parse: function (capture, parse, state) {
var content = capture[0]
.replace(/^ /gm, '')
.replace(/\n+$/, '');
return {
lang: undefined,
content: content
};
},
react: function (node, output, state) {
var className = node.lang ?
"markdown-code-" + node.lang :
undefined;
return reactElement(
'pre',
state.key,
{
children: reactElement(
'code',
null,
{
className: className,
children: node.content
}
)
}
);
},
html: function (node, output, state) {
var className = node.lang ?
"markdown-code-" + node.lang :
undefined;
var codeBlock = htmlTag("code", node.content, {
class: className
});
return htmlTag("pre", codeBlock);
}
},
fence: {
match: blockRegex(/^ *(`{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n *)+\n/),
parse: function (capture, parse, state) {
return {
type: "codeBlock",
lang: capture[2] || undefined,
content: capture[3]
};
}
},
// blockQuote: {
// match: blockRegex(/^( *>[^\n]+(\n[^\n]+)*\n*)+\n{2,}/),
// parse: function (capture, parse, state) {
// var content = capture[0].replace(/^ *> ?/gm, '');
// return {
// content: parse(content, state)
// };
// },
// react: function (node, output, state) {
// return reactElement(
// 'blockquote',
// state.key,
// {
// children: output(node.content, state)
// }
// );
// },
// html: function (node, output, state) {
// return htmlTag("blockquote", output(node.content, state));
// }
// },
// list: {
// match: function (source, state, prevCapture) {
// // We only want to break into a list if we are at the start of a
// // line. This is to avoid parsing "hi * there" with "* there"
// // becoming a part of a list.
// // You might wonder, "but that's inline, so of course it wouldn't
// // start a list?". You would be correct! Except that some of our
// // lists can be inline, because they might be inside another list,
// // in which case we can parse with inline scope, but need to allow
// // nested lists inside this inline scope.
// var isStartOfLineCapture = LIST_LOOKBEHIND_R.exec(prevCapture);
// var isListBlock = state._list || !state.inline;
// if (isStartOfLineCapture && isListBlock) {
// source = isStartOfLineCapture[1] + source;
// var res = LIST_R.exec(source);
// return LIST_R.exec(source);
// } else {
// return null;
// }
// },
// parse: function (capture, parse, state) {
// var bullet = capture[2];
// var ordered = bullet.length > 1;
// var start = ordered ? +bullet : undefined;
// var items = capture[0]
// .replace(LIST_BLOCK_END_R, "\n")
// .match(LIST_ITEM_R);
// var lastItemWasAParagraph = false;
// var itemContent = items.map(function (item, i) {
// // We need to see how far indented this item is:
// var space = LIST_ITEM_PREFIX_R.exec(item)[0].length;
// // And then we construct a regex to "unindent" the subsequent
// // lines of the items by that amount:
// var spaceRegex = new RegExp("^ {1," + space + "}", "gm");
// // Before processing the item, we need a couple things
// var content = item
// // remove indents on trailing lines:
// .replace(spaceRegex, '')
// // remove the bullet:
// .replace(LIST_ITEM_PREFIX_R, '');
// // Handling "loose" lists, like:
// //
// // * this is wrapped in a paragraph
// //
// // * as is this
// //
// // * as is this
// var isLastItem = (i === items.length - 1);
// var containsBlocks = content.indexOf("\n\n") !== -1;
// // Any element in a list is a block if it contains multiple
// // newlines. The last element in the list can also be a block
// // if the previous item in the list was a block (this is
// // because non-last items in the list can end with \n\n, but
// // the last item can't, so we just "inherit" this property
// // from our previous element).
// var thisItemIsAParagraph = containsBlocks ||
// (isLastItem && lastItemWasAParagraph);
// lastItemWasAParagraph = thisItemIsAParagraph;
// // backup our state for restoration afterwards. We're going to
// // want to set state._list to true, and state.inline depending
// // on our list's looseness.
// var oldStateInline = state.inline;
// var oldStateList = state._list;
// state._list = true;
// // Parse inline if we're in a tight list, or block if we're in
// // a loose list.
// var adjustedContent;
// if (thisItemIsAParagraph) {
// state.inline = false;
// adjustedContent = content.replace(LIST_ITEM_END_R, "\n\n");
// } else {
// state.inline = true;
// adjustedContent = content.replace(LIST_ITEM_END_R, "");
// }
// var result = parse(adjustedContent, state);
// // Restore our state before returning
// state.inline = oldStateInline;
// state._list = oldStateList;
// return result;
// });
// return {
// ordered: ordered,
// start: start,
// items: itemContent
// };
// },
// react: function (node, output, state) {
// var ListWrapper = node.ordered ? "ol" : "ul";
// return reactElement(
// ListWrapper,
// state.key,
// {
// start: node.start,
// children: node.items.map(function (item, i) {
// return reactElement(
// 'li',
// '' + i,
// {
// children: output(item, state)
// }
// );
// })
// }
// );
// },
// html: function (node, output, state) {
// var listItems = node.items.map(function (item) {
// return htmlTag("li", output(item, state));
// }).join("");
// var listTag = node.ordered ? "ol" : "ul";
// var attributes = {
// start: node.start
// };
// return htmlTag(listTag, listItems, attributes);
// }
// },
// def: {
// // TODO(aria): This will match without a blank line before the next
// // block element, which is inconsistent with most of the rest of
// // markdown-tg.
// match: blockRegex(
// /^ *\[([^\]]+)\]: *<?([^\s>]*)>?(?: +["(]([^\n]+)[")])? *\n(?: *\n)?/
// ),
// parse: function (capture, parse, state) {
// var def = capture[1]
// .replace(/\s+/g, ' ')
// .toLowerCase();
// var target = capture[2];
// var title = capture[3];
// // Look for previous links/images using this def
// // If any links/images using this def have already been declared,
// // they will have added themselves to the state._refs[def] list
// // (_ to deconflict with client-defined state). We look through
// // that list of reflinks for this def, and modify those AST nodes
// // with our newly found information now.
// // Sorry :(.
// if (state._refs && state._refs[def]) {
// // `refNode` can be a link or an image
// state._refs[def].forEach(function (refNode) {
// refNode.target = target;
// refNode.title = title;
// });
// }
// // Add this def to our map of defs for any future links/images
// // In case we haven't found any or all of the refs referring to
// // this def yet, we add our def to the table of known defs, so
// // that future reflinks can modify themselves appropriately with
// // this information.
// state._defs = state._defs || {};
// state._defs[def] = {
// target: target,
// title: title,
// };
// // return the relevant parsed information
// // for debugging only.
// return {
// def: def,
// target: target,
// title: title,
// };
// },
// react: function () { return null; },
// html: function () { return ""; }
// },
// table: {
// match: blockRegex(/^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/),
// parse: TABLES.parseTable,
// react: function (node, output, state) {
// var getStyle = function (colIndex) {
// return node.align[colIndex] == null ? {} : {
// textAlign: node.align[colIndex]
// };
// };
// var headers = node.header.map(function (content, i) {
// return reactElement(
// 'th',
// '' + i,
// {
// style: getStyle(i),
// scope: 'col',
// children: output(content, state)
// }
// );
// });
// var rows = node.cells.map(function (row, r) {
// return reactElement(
// 'tr',
// '' + r,
// {
// children: row.map(function (content, c) {
// return reactElement(
// 'td',
// '' + c,
// {
// style: getStyle(c),
// children: output(content, state)
// }
// );
// })
// }
// );
// });
// return reactElement(
// 'table',
// state.key,
// {
// children: [reactElement(
// 'thead',
// 'thead',
// {
// children: reactElement(
// 'tr',
// null,
// {
// children: headers
// }
// )
// }
// ), reactElement(
// 'tbody',
// 'tbody',
// {
// children: rows
// }
// )]
// }
// );
// },
// html: function (node, output, state) {
// var getStyle = function (colIndex) {
// return node.align[colIndex] == null ? "" :
// "text-align:" + node.align[colIndex] + ";";
// };
// var headers = node.header.map(function (content, i) {
// return htmlTag("th", output(content, state),
// { style: getStyle(i), scope: "col" });
// }).join("");
// var rows = node.cells.map(function (row) {
// var cols = row.map(function (content, c) {
// return htmlTag("td", output(content, state),
// { style: getStyle(c) });
// }).join("");
// return htmlTag("tr", cols);
// }).join("");
// var thead = htmlTag("thead", htmlTag("tr", headers));
// var tbody = htmlTag("tbody", rows);
// return htmlTag("table", thead + tbody);
// }