使用JavaScript或jQuery自定义降价格式化文本 [英] Formatting text using custom markdown with Javascript or jQuery

查看:247
本文介绍了使用JavaScript或jQuery自定义降价格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个网站,用户提交基本的新闻报道。我想为他们的提交添加一些格式化功能,非常类似于这里使用的降价功能。



例如,用户可以添加:

  **这应该是大胆的** 



文本显示为粗体。或者用户在段落的开头添加>

 > ;这可能是一个很长的段落

段落被包装在< blockquote> ;



请注意,我不希望它被服务器端处理。



如何使用Javascript或jQuery来做到这一点?



编辑:



我发现了一种在asterick中使用以下粗体的方法:

 < div class = content>以下是粗体:**我是粗体**< / div> 

jQuery:

  function markdown(markdownable){

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

返回markdownable;

$ b $('。content')。each(function(){

var markdownable = $(this).html(),$ b $ (内容);


});

小提琴



然而,我仍然不知道如何制作一个以> 包装成< blockquote>

解决方案

看看这个插件 markitUp!



markItUp!是一个基于jQuery库构建的JavaScript插件。它允许你把任何textarea变成一个标记编辑器。 Html,Textile,Wiki Syntax,Markdown,BBcode甚至是你自己的标记系统都可以很容易的实现。

希望这会对你有所帮助。

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>.

解决方案

Please have a look at this pluggin markitUp!

markItUp! is a JavaScript plugin built on the jQuery library. It allows you to turn any textarea into a markup editor. Html, Textile, Wiki Syntax, Markdown, BBcode or even your own Markup system can be easily implemented.

Hope this will help you.

这篇关于使用JavaScript或jQuery自定义降价格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆