且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 RTF 字符串转换为 Markdown 字符串(并返回)(C# .NET Core 或 JS)

更新时间:2022-11-09 09:53:58

很抱歉您在使用 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