Fastest Way To Format a Plain Text Using Javascript
I have a huge plain text document, about 700kb which is very big for plain texts and I need to format it on cloud converting it to HTML, but the only things that I need to replace, format to HTML so it can be displayed by the browser, are bold and italic. For bold at the plain text they are like this:
Not on bold... **bold text here** not bold here
And italic like this:
Not italic... *italic text* no italic
Just like StackOverflow does for their formatting, but 开发者_如何学Cthe problem is that I need to make it a lot faster, since the text is so big... One of my ideas was to add a page slide, so I the script just need to format some part of the text, not it all, then after the user changes the page the script would be called again, but the problem is how I can make the code for this all?
Is
function boldAndItalicize(text) {
return text.replace(/&/g, '&').replace(/</g, '<')
.replace(/(\*+)([^*]{1,1024})(\*+)/g,
function (whole, open, content, close) {
if (open.length === close.length) {
switch (open.length) {
case 1: return '<i>' + content + '</i>';
case 2: return '<b>' + content + '</b>';
}
}
return whole;
});
}
too slow?
If you want to break it up, you do still need to scan to the break point, to make sure you don't split inside an italicized or bolded section.
You may try to divide it in many divs and try to find a way to apply the formatting to each when they become visible (i.e. the user scrolls and they enter the content area).
I have no idea on how to do it, but you may find something similar already done in Jquery and grab some code.
Something like this for example: http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/
精彩评论