Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I run a site where users submit basic news articles. I want to add some formatting features for their submissions, quite similar to the markdown feature used here.

For example, user can add:

**This should be bold**

And the text appears bold. Or user adds > at the beginning of the paragraph:

> This could be a lengthy paragraph

And the paragraph gets wrapped in <blockquote>.

Note that I do not want it to be processed server-side.

How can I do this using Javascript or jQuery?

EDIT:

I found a way to make text within astericks bold using the following:

<div class="content">The following will be bold: **I am bold**</div>

jQuery:

function markdown(markdownable) {

var bold = /**(S(.*?S)?)**/gm;
    markdownable = markdownable.replace( bold, '<span style="font-weight:bold">$1</span>' );

    return markdownable;
}

$('.content').each(function() {

    var markdownable = $(this).html(),
        content = markdown(markdownable);

    $(this).html(content);


});

Fiddle.

However, I still have no clue on how to make a paragraph starting with > wrapped into <blockquote>.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

The Markdown solution that Reddit uses is freely available, lightweight, well documented and does everything you asked for and more:

http://daringfireball.net/projects/markdown/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...