Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

Formatting text using custom markdown with Javascript or jQuery

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

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

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...