简体   繁体   English

如何使用 ASP.NET MVC 3 和 Stack Overflow 的 Markdown

[英]How to use ASP.NET MVC 3 and Stack Overflow's Markdown

I couldn't find any real sources for this.我找不到任何真正的来源。 I'm building a site in ASP.NET MVC 3 and would like to take advantage of the Markdown editor that Stack Overflow uses.我正在 ASP.NET MVC 3 中构建一个站点,并希望利用 Stack Overflow 使用的 Markdown 编辑器。 Does anybody have a good tutorial?有人有好的教程吗?

Where do you download the latest markdown?哪里下载最新的markdown? What language is it written in?它是用什么语言编写的? Where would I start in integrating this into an MVC 3 project?我从哪里开始将它集成到 MVC 3 项目中? Even after all the searching and reading I've done I'm still pretty confused.即使在我完成了所有的搜索和阅读之后,我仍然很困惑。

I came across this site .我遇到了这个网站 But this seems outlandishly old and it would seem I would have to learn a little something about CGI and Perl which I have absolutely no experience with.但这似乎太古怪了,似乎我必须学习一些关于 CGI 和 Perl 的知识,而我完全没有这方面的经验。 A JavaScript/jQuery version would be splendid. JavaScript/jQuery 版本会很棒。

Update更新

I noticed this question is getting a fair amount of views so I decided to update it with some helpful references.我注意到这个问题得到了大量的意见,所以我决定用一些有用的参考来更新它。 I managed to get a Markdown editor working nicely on my website, and I wrote a few blogs about it.我设法让 Markdown 编辑器在我的网站上运行良好,我写了一些关于它的博客。

Stackoverflow open sourced their version of Markdown to the world. Stackoverflow 向全世界开源了他们的 Markdown 版本。 Its called MarkdownSharp and is written in C#.它称为MarkdownSharp ,是用 C# 编写的。

Somebody wrote a HtmlHelper here: http://blog.dantup.com/2011/03/an-asp-net-mvc-htmlhelper-extension-method-for-markdown-using-markdownsharp有人在这里写了一个 HtmlHelper: http : //blog.dantup.com/2011/03/an-asp-net-mvc-htmlhelper-extension-method-for-markdown-using-markdownsharp

If you are looking for how to implement a javascript editor there is an existing question: Integrate Markitup text editor to ASP.NET MVC project如果您正在寻找如何实现 javascript 编辑器,则存在一个现有问题: 将 Markitup 文本编辑器集成到 ASP.NET MVC 项目

You are probably looking for MarkdownSharp您可能正在寻找MarkdownSharp

Open source C# implementation of Markdown processor, as featured on Stack Overflow. Markdown 处理器的开源 C# 实现,如 Stack Overflow 上的特色。

To integrate it into an MVC app:要将其集成到 MVC 应用程序中:

  1. In a until or common controller, add the following action method在一个until 或 common 控制器中,添加以下操作方法

    public ActionResult FormatMarkdown(string markdownText) { var md = new MarkdownSharp.Markdown(); string html = md.Transform(markdownText); return Json(html, JsonRequestBehavior.AllowGet); }
  2. in your client side view:在您的客户端视图中:

     @Html.TextArea("mdText", new { rows = 12, cols = 60 }) <div id="mdFormatted"></div>
  3. and client side JS:和客户端JS:

     $(function () { var mdText = $("#mdText"); var mdFormatted = $("#mdFormatted"); function setFormatted(data) { mdFormatted.html(data); }; mdText.toObservable("keypress") .Throttle(200) .Subscribe(function () { $.getJSON("@VirtualPathUtility.ToAbsolute("~/Util/FormatMarkdown/")", { markdownText: mdText.val() }, setFormatted); })
  4. Download RxJs (from MSDN ) and include the following two js files下载 RxJs(来自MSDN )并包含以下两个 js 文件

    <script src="@Url.Content("~/Scripts/rx.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/rx.jquery.js")" type="text/javascript"></script>

I know this question is old but I stumbled upon another solution markdowndeep which is very friendly with MVC我知道这个问题很老,但我偶然发现了另一个对 MVC 非常友好的解决方案markdowndeep

It can be installed through nuget PM> Install-Package MarkdownDeep.Full可以通过nuget PM> Install-Package MarkdownDeep.Full 安装

Markdown in C# C#中的降价

// Create an instance of Markdown
var md = new MarkdownDeep.Markdown();
// Set options
md.ExtraMode = true;
md.SafeMode = false;
string output = md.Transform(input);

Editor编辑

1.Copy the supplied js, css, png and htm files to your server. 1. 将提供的 js、css、png 和 htm 文件复制到您的服务器。 Depending where you place these files on your server, you might need to update the image urls in the css file.根据您在服务器上放置这些文件的位置,您可能需要更新 css 文件中的图像 URL。

2.Update your page to reference jQuery, the MarkdownDeep library and the MarkdownDeep css file (again, you might need to change the paths). 2.更新您的页面以引用 jQuery、MarkdownDeep 库和 MarkdownDeep css 文件(同样,您可能需要更改路径)。

<link rel="stylesheet" href="mdd_styles.css" 
<script type="text/javascript" src="jQuery-1.4.2.min.js">
<script type="text/javascript" src="MarkdownDeepLib.min.js">

NB: MarkdownDeepLib.min.js is a packaged, minified version of MarkdownDeep.js, MarkdownDeepEditor.js and MarkdownDeepEditorUI.js.注意:MarkdownDeepLib.min.js 是 MarkdownDeep.js、MarkdownDeepEditor.js 和 MarkdownDeepEditorUI.js 的打包缩小版本。 For debugging, you can reference these three files instead.为了调试,您可以参考这三个文件。

3.Insert the Markdown editor into your page like this: 3.将 Markdown 编辑器插入您的页面,如下所示:

<div class="mdd_toolbar"></div>
<textarea cols=50 rows=10 class="mdd_editor"></textarea>
<div class="mdd_resizer"></div>
<div class="mdd_preview"></div>

Note: the associated divs are all optional and if missing, the plugin will create them.注意:关联的 div 都是可选的,如果缺少,插件将创建它们。 However... you might experience the page jumping around during load if you do this.但是...如果您这样做,您可能会在加载过程中遇到页面跳动。 ie: it's recommended to explicitly include them.即:建议明确包含它们。

4.Called the MarkdownDeep jQuery plugin to convert the textarea to a MarkdownEditor 4.调用MarkdownDeep jQuery插件将textarea转为MarkdownEditor

$("textarea.mdd_editor").MarkdownDeep({ 
    help_location: "/Content/mdd_help.html",
    disableTabHandling:true
 });

Although I really like their product I am not affiliated with the makers of markdowndeep .虽然我真的很喜欢他们的产品,但我不隶属于 markdowndeep 的制造商 I just thought they made a good product我只是觉得他们做了一个很好的产品

This question is old, but I'm just leaving an answer here so that future readers can benefit from it.这个问题很老了,但我只是在这里留下一个答案,以便未来的读者可以从中受益。

I have used MarkdownSharp v1.13, It does NOT sanitize your html output.我已经使用MarkdownSharp V1.13,它不会净化你的HTML输出。 For example, if you type:例如,如果您键入:

<script type="text/javascript">alert("Hacked");</script>

Into your input field, the output from MarkdownSharp contains the same script.在您的输入字段中,MarkdownSharp 的输出包含相同的脚本。 Thus it exposes your website to XSS vulnerability.因此,它会将您的网站暴露于 XSS 漏洞。

Read this from Stackoverflow's article on PageDown:阅读Stackoverflow在 PageDown 上的文章

It should be noted that Markdown is not safe as far as user-entered input goes.应该注意的是,就用户输入的内容而言,Markdown 并不安全。 Pretty much anything is valid in Markdown, in particular something like <script>doEvil();</script> .在 Markdown 中几乎任何东西都是有效的,特别是像<script>doEvil();</script> This PageDown repository includes the two plugins that Stack Exchange uses to sanitize the user's input;这个 PageDown 存储库包括 Stack Exchange 用来清理用户输入的两个插件; see the description of Markdown.Sanitizer.js below.请参阅下面的 Markdown.Sanitizer.js 描述。

So, from other point of view, maybe Markdown was not supposed to sanitize your input in the first place and MarkdownSharp implementation of it just conformed with those principles.因此,从其他角度来看,也许 Markdown 一开始就不应该清理您的输入,而它的 MarkdownSharp 实现只是符合这些原则。 I should mention that Stackoverflow does uses MarkdownSharp on their server side.我应该提到 Stackoverflow 确实在他们的服务器端使用了 MarkdownSharp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM