HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Markdown to HTML, again

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
againmarkdownhtml

Problem

I made another markdown parser. (original, this one on github). Look, I'm not crazy (sorta), I just like making markdown parsers…

``
String.prototype.replaceAll = function(find, replace) {
if (typeof find == 'string') return this.split(find).join(replace);
var t = this, i, j;
while (typeof(i = find.shift()) == 'string' && typeof(j = replace.shift()) == 'string') t = t.replaceAll(i || '', j || '');
return t;
};
function html(input, replaceQuoteOff) {
if (replaceQuoteOff) return input.toString().replaceAll(['&', '', gt);
var paren = '#' + Math.random().toString();
while (input.indexOf(paren) != -1) paren = '#' + Math.random().toString();
input = input.replaceAll('\\(', paren);
var cparen = '#' + Math.random().toString();
while (input.indexOf(cparen) != -1 || [paren].indexOf(cparen) != -1) cparen = '#' + Math.random().toString();
input = input.replaceAll('\\)', cparen);
var carrot = '#' + Math.random().toString();
while (input.indexOf(carrot) != -1 || [paren, cparen].indexOf(carrot) != -1) carrot = '#' + Math.random().toString();
input = input.replaceAll('\\^', carrot);
var dollar = '#' + Math.random().toString();
while (input.indexOf(dollar) != -1 || [paren, cparen, carrot].indexOf(dollar) != -1) dollar = '#' + Math.random().toString();
input = input.replaceAll('\\$', dollar);
var open = [];
return input.split('
').map(function(val, i, arr) {
if (i % 2) return '' + html(val.replaceAll([backslash, graveaccent, asterisk, underscore, dash, plus, dot, hash, gt, paren, cparen, carrot, dollar], ['\\\\', '\\`', '\\*', '\\_', '\\-', '\\+', '\\.', '\\#', '\\>', '\\(', '\\)', '\\^'])) + '';
var parsed = val.split('*').map(function(val, i, arr) {
var parsed = val.split('_').map(function(val, i, arr) {
var parsed = val.split('---').map(function(val, i, arr) {
var parsed = val.split('+++').map(function(val, i, arr) {

Solution

Contrary to your claims to not be crazy, I am not convinced. Using random characters is simply crazy. If I were you I would change the charCode in the string to charCodes with a value under 32 (control characters) that are not 9, 10 or 13. Since you need 14 replacement chars I would go for

  • 1 (Start Of Header)



  • 2 (Start Of Text)



  • 3 (End Of Text)



  • 4 (End Of Transmission)



  • 5 (Enquiry)



  • 6 (Acknowledgement)



  • 14 (Shift out) -> skipping bell, backspace, tab, line feed, tab, form feed and return



  • 15 (Shift in)



  • 16 (Data Link Escape)



  • 17 (Device Control 1)



  • 18 (Device Control 2)



  • 19 (Device Control 3)



  • 20 (Device Control 4)



  • 21 Negative Acknowledgment



If you need more, see here: http://en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart

Other than that:

  • Split your code up further in well named functions



  • Use comments for your regexes!!



  • Use well named constants, for example here class="blk"

Context

StackExchange Code Review Q#69116, answer score: 2

Revisions (0)

No revisions yet.