如何将 RTF 字符串转换为 Markdown 字符串(并返回)(C# .NET Core 或 JS) [英] How do I convert an RTF string to a Markdown string (and back) (C# .NET Core, or JS)

查看:43
本文介绍了如何将 RTF 字符串转换为 Markdown 字符串(并返回)(C# .NET Core 或 JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C# 或 JS 中将 RTF 字符串转换为 Markdown 字符串(并返回),理想情况下不包装 exe?

How do I convert an RTF string to a Markdown string (and back) either in C# or JS, ideally without wrapping an exe?

我有一个使用 .NET 的 RichTextBox 控件的旧产品.使用它的表单以 Microsoft 专有的RTF 格式保存其输出.这是它可以生成的输出的一个小示例:

I have a legacy product that uses .NET's RichTextBox control. Forms that use it save their output in Microsoft's proprietary RTF format. Here is a small example of the output it can generate:

{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 GenericSansSerif;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch Some content here }\li0\ri0\sa0\sb0\fi0\ql\par}
}
}

我的 C# .NET Core Web App 需要能够使用这个存储的 RTF 来在网页上显示富文本编辑器",有更新值的能力,并保存为仍然可以使用的格式由旧产品.

My C# .NET Core Web App needs to be able to use this stored RTF to display a "Rich Text Editor" on a web page, have the ability to update the value, and save in a format that can still be used by the legacy product.

不幸的是,我无法找到可以使用 RTF 作为输入的现有/现代 Web 组件.大多数似乎使用降价或自定义 JSON 格式.

Unfortunately, I am having trouble finding existing/modern web components that can use RTF as input. Most appear to use markdown or a custom JSON format.

  1. 使用以下任一方法将现有的 RTF 转换为 Markdown:
    • 服务器端,使用 C#
    • 客户端,使用JS

到目前为止,我已经尝试过:

  • 按照此 CodeProject 文章创建自定义 RTF -> HTML 转换器:编写您自己的 RTF 转换器
    • 我可以让它在 .NET Framework 项目中工作,但不能在 .NET Core 中工作
      • 在 .NET Core 项目中引发空引用错误
      • 只支持一小部分 RTF,创建一个完整的 HTML 文档而不是一个字符串/子集,打破我的具体例子

      注意:我尝试过的东西来自 RTF -> Html,因为我找不到任何适用于 RTF -> Markdown 的内容.我的希望是,如果必须的话,我可以这样做:作为最后的手段,RTF -> HTML -> Markdown(和相反).

      Note: The things I've tried are from RTF -> Html because I couldn't find anything for RTF -> Markdown specifically. My hope was that I could, if I had to, do: RTF -> HTML -> Markdown (and in reverse) as a last resort.

      推荐答案

      很抱歉您在使用 RtfPipe 时出现空引用错误 和 .Net 核心.这些错误的解决方案现已记录在项目中,包括包含 NuGet 包 System.Text.Encoding.CodePages 和注册代码页提供程序.

      Sorry for the null reference errors you had with RtfPipe and .Net Core. A resolution to these errors is now documented on the project and involves including the NuGet package System.Text.Encoding.CodePages and registering the code page provider.

      #if NETCORE
        // Add a reference to the NuGet package System.Text.Encoding.CodePages for .Net core only
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
      #endif
      var html = Rtf.ToHtml(rtf);
      

      由于 HTML 在技术上是 Markdown,所以你可以停在这里.否则,您也可以使用我的 BracketPipe 库将 HTML 转换为 Markdown.代码看起来像这样.

      Since HTML is technically Markdown, you can stop here. Otherwise, you can convert the HTML to Markdown as well using my BracketPipe library. The code would look something like.

      using BracketPipe;
      using RtfPipe;
      
      private string RtfToMarkdown(string source)
      {
        using (var w = new System.IO.StringWriter())
        using (var md = new MarkdownWriter(w))
        {
          Rtf.ToHtml(source, md);
          md.Flush();
          return w.ToString();
        }
      }
      

      Markdig 是一个很好的从 Markdown 到 HTML 的库.但是,对于从 HTML 到 RTF,我没有任何好的建议.

      Markdig is a good library for getting from Markdown to HTML. However, I don't have any good suggestions for getting from HTML to RTF.

      免责声明:我是 RtfPipeBracketPipe 开源项目

      Disclaimer: I am the author of the RtfPipe and BracketPipe open source projects

      这篇关于如何将 RTF 字符串转换为 Markdown 字符串(并返回)(C# .NET Core 或 JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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