0

I want to create my own markdown system for my platform.

So, to allow users to make their text bold, they can wrap text in double asterisks.

Here is how I do this:

<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);


});

Here is a working fiddle.

Now, to my question. Whenever users add a > at the beginning of a paragraph, like this:

> Hello world, this can be a very lengthy paragraph.

Then I want to wrap that text into <blockquote>.

How can I do this?

Boaz
  • 19,892
  • 8
  • 62
  • 70
Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
  • Most of the content in your post is not relevant to your question. You should remove it. In finding a solution, consider using a similar logic to the one you've included but don't include a solution for another issue in your question. – Boaz Aug 09 '14 at 16:20

1 Answers1

1

hey i have updated your jsfiddle..

code:-

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

    if (markdownable.indexOf("&gt;") == 0) {
        markdownable = markdownable.replace("&gt;", "<blockquote>");
        markdownable += "</blockquote>";
    }
    return markdownable;
}

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

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

    $(this).html(content);


});

working jsfiddle example:-

http://jsfiddle.net/dwxmjkb3/2/

new Code as per request:-

function markdown(markdownableOrg) {
    var bold = /\*\*(\S(.*?\S)?)\*\*/gm;
    var dataArray = markdownableOrg.split("\n");
    var data = [];
    for (var i = 0; i < dataArray.length; i++) {
        var markdownable = dataArray[i];
        markdownable = markdownable.replace(bold, '<span style="font-weight:bold">$1</span>');
        if (markdownable.indexOf("&gt;") == 0) {
            markdownable = markdownable.replace("&gt;", "<blockquote>");
            markdownable += "</blockquote>";
        }
        data.push(markdownable)
    }

    return data.join("\n");
}

now above given method splits the data(each line) and checks for > and replace it with blockquote.

updated jsfiddle :-http://jsfiddle.net/dwxmjkb3/6/

thanks

Devendra Soni
  • 1,924
  • 12
  • 16
  • I just viewed your fiddle but couldn't see a working solution to turn text that start with > into
    . Can you please edit the HTML part and show a working fiddle? EDIT: See this fiddle: http://jsfiddle.net/dwxmjkb3/4/
    – Henrik Petterson Aug 09 '14 at 16:33
  • Oh in this case split the line and then pass the lines one by one into this function and merge the result, this might help you or I will provide you a jsfiddle for this thanks – Devendra Soni Aug 09 '14 at 16:52
  • Can you please provide me with a jsfiddle for this? – Henrik Petterson Aug 09 '14 at 17:12
  • hey i have updated the jsfiddle and updated the answer for it please check and accept the answer if it solved your purpose ...:) – Devendra Soni Aug 10 '14 at 08:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59039/discussion-between-devendra-soni-and-henrik-petterson). – Devendra Soni Aug 10 '14 at 09:22
  • I noticed one last issue, I am not sure if you have the time to help me. If the paragraph starts with a

    tag, then the > solution doesn't come in effect. Is there any way to make it work if the data has a

    tag at the beginning?

    – Henrik Petterson Aug 10 '14 at 10:16