patternjavascriptMinor
A character-by-character inline markdown parser
Viewed 0 times
inlineparsermarkdowncharacter
Problem
``
'``': 'samp',
'*': 'em',
'**': 'strong',
'_': 'i',
'–––': 's',
'+++': 'ins',
'---': 'del',
'[c]': 'cite',
'[m]': 'mark',
'[u]': 'u',
'[v]': 'var',
'::': 'kbd',
'"': 'q'
},
stags = {
sup: {
start: '^(',
end: ')^'
},
sub: {
start: 'v(',
end: ')v'
},
small: {
start: '[sm]',
end: '[/sm]'
}
};
outer: for (var i = 0; i 0; l--) {
if (tags[input.sub
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(['&', '$1');
return input
.replaceAll('\u0001', '^')
.replace(/\[(.+?)\|(.+?)\]/g, '$1')
.replaceAll('\u0002', '')
.replace(/\[\[(\d+)\\]/g, '[$1]')
.replace(/!\[([^\]]+)]\((https?:\/\/[^\s("\\]+\.[^\s"\\]+)\)/g, '')
.replace(/^(https?:\/\/([^\s("\\]+\.[^\s"\\]+\.(svg|png|tiff|jpg|jpeg)(\?[^\s"\\\/]*)?))/g, '')
.replace(/\[([^\]]+)]\((https?:\/\/[^\s("\\]+\.[^\s"\\]+)\)/g, '$1'.link('$2'))
.replace(/([^;["\\])(https?:\/\/([^\s("\\]+\.[^\s"\\]+\.(svg|png|tiff|jpg|jpeg)(\?[^\s"\\\/]*)?))/g, '$1')
.replace(/([^;["\\])(https?:\/\/([^\s("\\]+\.[^\s"\\]+))/g, '$1' + '$3'.link('$2'))
.replace(/^(https?:\/\/([^\s("\\]+\.[^\s"\\]+))/g, '$2'.link('$1'));
}
function inlineMarkdown(input) {
var output = '',
span = '',
current = [],
tags = {
'': 'code','``': 'samp',
'*': 'em',
'**': 'strong',
'_': 'i',
'–––': 's',
'+++': 'ins',
'---': 'del',
'[c]': 'cite',
'[m]': 'mark',
'[u]': 'u',
'[v]': 'var',
'::': 'kbd',
'"': 'q'
},
stags = {
sup: {
start: '^(',
end: ')^'
},
sub: {
start: 'v(',
end: ')v'
},
small: {
start: '[sm]',
end: '[/sm]'
}
};
outer: for (var i = 0; i 0; l--) {
if (tags[input.sub
Solution
My first comment would be to add some blank lines between those functions of yours. You should also add some comments describing the purpose of the functions as well. My preferred style of documentation is usually JSDoc, but you can always find a different style as well.
Secondly, even though you say that you're fine with patching Javascript's builtin object prototypes, it's still a really bad idea. See this Stackoverflow question for more details on that.
From looking at your code, I've also noticed that you don't use braces in many places, like here:
Generally it's good to use braces, even if you're only running one line of code. A famous example of this ending up very badly is the Apple SSL bug. The bug looked something like this:
So, in short, use braces.
Other than that, your code looks really nice!
Secondly, even though you say that you're fine with patching Javascript's builtin object prototypes, it's still a really bad idea. See this Stackoverflow question for more details on that.
From looking at your code, I've also noticed that you don't use braces in many places, like here:
if (typeof find == 'string') return this.split(find).join(replace);Generally it's good to use braces, even if you're only running one line of code. A famous example of this ending up very badly is the Apple SSL bug. The bug looked something like this:
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail; /* MISTAKE! THIS LINE SHOULD NOT BE HERE */So, in short, use braces.
Other than that, your code looks really nice!
Code Snippets
if (typeof find == 'string') return this.split(find).join(replace);if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail; /* MISTAKE! THIS LINE SHOULD NOT BE HERE */Context
StackExchange Code Review Q#87316, answer score: 2
Revisions (0)
No revisions yet.