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

发布于 2024-10-21 22:05:55 字数 829 浏览 2 评论 0 原文

我找不到任何真正的来源。我正在 ASP.NET MVC 3 中构建一个网站,并希望利用 Stack Overflow 使用的 Markdown 编辑器。有人有好的教程吗?

最新的markdown在哪里下载?它是用什么语言写的?我应该从哪里开始将其集成到 MVC 3 项目中?即使在我完成了所有的搜索和阅读之后,我仍然很困惑。

我发现了这个网站。但这似乎太古老了,而且我似乎必须学习一些关于 CGI 和 Perl 的知识,而我完全没有经验。如果有 JavaScript/jQuery 版本那就太好了。

更新

我注意到这个问题得到了相当多的关注,所以我决定用一些有用的参考资料来更新它。我设法让 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. Does anybody have a good tutorial?

Where do you download the latest markdown? What language is it written in? Where would I start in integrating this into an MVC 3 project? 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. A JavaScript/jQuery version would be splendid.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

燃情 2024-10-28 22:05:55

Stackoverflow 向全世界开源了他们的 Markdown 版本。它名为 MarkdownSharp,是用 C# 编写的。

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

如果您正在寻找如何实现 JavaScript 编辑器,这里有一个现有的问题:
将 Markitup 文本编辑器集成到 ASP.NET MVC 项目

Stackoverflow open sourced their version of Markdown to the world. Its called MarkdownSharp and is written in C#.

Somebody wrote a HtmlHelper here:
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

不知在何时 2024-10-28 22:05:55

您可能正在寻找 MarkdownSharp

Markdown 处理器的开源 C# 实现,如 Stack Overflow 上的特色。

要将其集成到 MVC 应用程序中:

  1. 在直到或公共控制器中,添加以下操作方法

    public ActionResult FormatMarkdown(string markdownText)
    {
        var md = new MarkdownSharp.Markdown();
        字符串 html = md.Transform(markdownText);
        返回 Json(html, JsonRequestBehavior.AllowGet);
    }
    
  2. 在直到或公共控制器中,在客户端视图中 :

    @Html.TextArea("mdText", new { rows = 12, cols = 60 })
    
  3. 和客户端 JS:

    <前><代码>$(函数 () {
    var mdText = $("#mdText");
    var mdFormatted = $("#mdFormatted");
    函数 setFormatted(数据) {
    mdFormatted.html(数据);
    };
    mdText.toObservable(“按键”)
    .油门(200)
    .订阅(函数() {
    $.getJSON("@VirtualPathUtility.ToAbsolute("~/Util/FormatMarkdown/")", {
    markdownText: mdText.val()
    }, setFormatted);
    })

  4. 下载 RxJs(来自 MSDN)并包含以下两个js文件

    ;    
    ;  
    

You are probably looking for MarkdownSharp

Open source C# implementation of Markdown processor, as featured on Stack Overflow.

To integrate it into an MVC app:

  1. In a until or common controller, add the following action method

    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:

    $(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

    <script src="@Url.Content("~/Scripts/rx.js")" type="text/javascript"></script>    
    <script src="@Url.Content("~/Scripts/rx.jquery.js")" type="text/javascript"></script>  
    
最初的梦 2024-10-28 22:05:55

我知道这个问题已经很老了,但我偶然发现了另一个解决方案markdowndeep,它对MVC非常友好

它可以通过nuget安装PM>安装包 MarkdownDeep.Full

C# 中的 Markdown

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

编辑器

1.将提供的js、css、png和htm文件复制到您的服务器。根据您将这些文件放置在服务器上的位置,您可能需要更新 css 文件中的图像 URL。

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

注意:MarkdownDeepLib.min.js 是 MarkdownDeep.js、MarkdownDeepEditor.js 和 MarkdownDeepEditorUI.js 的打包缩小版本。为了进行调试,您可以引用这三个文件。

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>

注意:关联的 div 都是可选的,如果缺少,插件将创建它们。但是...如果这样做,您可能会在加载过程中遇到页面跳转的情况。即:建议明确包含它们。

4.调用 MarkdownDeep jQuery 插件将文本区域转换为 MarkdownEditor

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

虽然我真的很喜欢他们的产品我不隶属于 markdowndeep 的制作者。我只是觉得他们做了一个好产品

I know this question is old but I stumbled upon another solution markdowndeep which is very friendly with MVC

It can be installed through nuget PM> Install-Package MarkdownDeep.Full

Markdown in 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. Depending where you place these files on your server, you might need to update the image urls in the css file.

2.Update your page to reference jQuery, the MarkdownDeep library and the MarkdownDeep css file (again, you might need to change the paths).

<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. For debugging, you can reference these three files instead.

3.Insert the Markdown editor into your page like this:

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

$("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. I just thought they made a good product

懒的傷心 2024-10-28 22:05:55

这个问题已经很老了,但我只是在这里留下一个答案,以便将来的读者可以从中受益。

我已经使用了 MarkdownSharp v1.13,它不会清理你的html 输出。例如,如果您

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

在输入字段中键入:,MarkdownSharp 的输出将包含相同的脚本。因此,它会使您的网站面临 XSS 漏洞。

请阅读 Stackoverflow 的关于 PageDown 的文章

应该注意的是,就用户输入而言,Markdown 并不安全。几乎任何东西在 Markdown 中都是有效的,特别是像 这样的东西。此 PageDown 存储库包含 Stack Exchange 用于清理用户输入的两个插件;请参阅下面的 Markdown.Sanitizer.js 描述。

因此,从其他角度来看,也许 Markdown 一开始就不应该清理你的输入,而 MarkdownSharp 的实现只是符合这些原则。我应该提到 Stackoverflow 确实在其服务器端使用 MarkdownSharp。

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. For example, if you type:

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

Into your input field, the output from MarkdownSharp contains the same script. Thus it exposes your website to XSS vulnerability.

Read this from Stackoverflow's article on PageDown:

It should be noted that Markdown is not safe as far as user-entered input goes. Pretty much anything is valid in Markdown, in particular something like <script>doEvil();</script>. This PageDown repository includes the two plugins that Stack Exchange uses to sanitize the user's input; see the description of Markdown.Sanitizer.js below.

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. I should mention that Stackoverflow does uses MarkdownSharp on their server side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文