views:

882

answers:

3

Currently i'm parsing bbcode server side but i'd like to show a preview just like this site does.

If I process the bbcode serverside using ajax it's a bit laggy, so i thought doing it client side, to just show the preview.

Do you guys know any bbcode parser written in javascript?

+1  A: 

Let me Google that for you.

"bbcode parser javascript" returns the bbcodejs project on Google Code, and a function you can copy-paste from this blog post.

They do have some key limitations: The former only seems to support simple find-and-replace, whereas the latter seems to have pre-set BBcode built in, so you'd probably have to hack it a bit if you chose that solution.

Your best options are probably to roll your own solution (possibly basing your work off one of the two links here), or just use AJAX and move on. That's probably the best way to ensure that previews are accurate, and previewing doesn't have to be real-time on every keypress, anyway; a delay before even sending the request is acceptable. (Nobody wants to waste that much bandwidth.)

Matchu
Yup, I saw both links before posting, but i was looking for something a bit more popular, just like the library i'm using now (http://bbcode.codeplex.com/).So, probably I will just port it to JS.Seems that there is not much
Drevak
A: 

Google gave me the following bbCode JavaScript editor, including explanations and everything.

Abel
Only gave the tutorial a skim, but it seems to just be Javascript functions to produce BBcode, not a preview mechanism.
Matchu
+1  A: 

It is a bit late, and the question has certainly been answered. However if you are still open to suggestions, and have not yet spent your time converting the indicated parser from C# to JavaScript, I have written a parser (originally in PHP) which I converted myself[2] to JavaScript. It is available at my website under the 3-clause BSD license. The parser seems to be reasonably fast, but I haven't performed any analysis on its speed.

It may not be as flexible in some ways as other possible projects out there, but it does allow defining your own codes ("bb-code" or not, with quite a few properties), and is also all contained within the one file. This is not a simple find-and-replace parser, and is not based on regex. Due to there currently being no option to disable escaping of "content", the output of one parser cannot be used as input to another (to perform more than one pass on different types of codes, such as :smile:-type codes and regular bb-code) as the output of the first parser would be escaped by the second.

If this is of any interest to you, it might save you from having to convert that other library. Technically, I'm a relative "unknown", but that's the great things about JS/OSS: you can check out the source to see what I've done.

[2] As a result, there are a few remaining "compatibility functions", but I rewrote things which had native equivalents available.

Art McBain