用 C# 将 HTML 转换为 Markdown
Markdown 是一种使用纯文本格式语法的标记语言。Markdown 经常被用作文档和自述文件的格式,因为它能以一种易读易写的风格进行写作。它因使用简单、学习轻便和支持广泛而深受技术撰稿人的欢迎。它的设计使其可以很容易地转换成多种输出格式,但最初创建时只能转换成 HTML 格式。Aspose.HTML for .NET 库提供从 HTML 到 Markdown 的反向转换。您可以在任何设备上使用任何文本编辑器访问和编辑 Markdown 文件或创建新内容。
本文将介绍如何使用 Converter 类的 ConvertHTML() 方法将 HTML 转换为 MD,以及如何应用 MarkdownSaveOptions。我们的代码示例将帮助您使用 C# 库将 HTML 转换为 Markdown。
在线 HTML 转换器
您可以使用 Aspose.HTML for .NET API 将 HTML 实时转换为 Markdown。首先,从本地硬盘加载 HTML 文件,然后运行示例。在本示例中,默认设置了保存选项。您将立即收到 HTML 转换为 Markdown 的结果,即一个单独的 Markdown 文件。
用几行代码将 HTML 转换为 Markdown
您可以使用 C# 和其他 .NET 编程语言将 HTML 转换为 Markdown 格式。转换器 类的静态方法主要用于将 HTML 代码转换成各种格式的最简单方法。下面的代码片段展示了如何通过几行代码将 HTML 转换为 Markdown 格式!
1// Convert HTML to Markdown in C#
2
3// Prepare HTML code and save it to a file
4string code = "<h1>Header 1</h1>" +
5 "<h2>Header 2</h2>" +
6 "<p>Convert HTML to Markdown</p>";
7File.WriteAllText("convert.html", code);
8
9// Call ConvertHTML() method to convert HTML to Markdown
10Converter.ConvertHTML("convert.html", new MarkdownSaveOptions(), Path.Combine(OutputDir, "convert.md"));
MarkdownSaveOptions
您可以根据自己的需要,通过保存选项来增强 Markdown 创建功能。MarkdownSaveOptions 有许多属性可以让你控制转换过程。最重要的选项是 MarkdownSaveOptions.Features。该选项允许您启用/禁用特定元素的转换。
Property | Description |
---|---|
Default | This property returns a set of options that are compatible with default Markdown documentation. |
Features | A flag set that controls which HTML elements are converted to Markdown. |
Formatter | This property gets or sets the Markdown formatting style. |
Git | This property returns a set of options that are compatible with GitLab Flavored Markdown. |
ResourceHandlingOptions | Gets a ResourceHandlingOptions object which is used for configuration of resources handling. |
要了解有关 MarkdownSaveOptions 的更多信息,请阅读 微调转换器 一文。
使用 MarkdownSaveOptions 将 HTML 转换为 Markdown
要通过指定 MarkdownSaveOptions
将 HTML 转换为 Markdown,应遵循以下几个步骤:
- 使用 HTMLDocument 类的 HTMLDocument() 构造函数之一加载 HTML 文件。
- 创建一个新的 MarkdownSaveOptions 对象。
- 使用转换器类的 ConvertHTML() 方法将 HTML 保存为 Markdown 文件。您需要向 ConvertHTML() 方法传递 HTMLDocument、MarkdownSaveOptions 和输出文件路径,以便将 HTML 转换为 Markdown。
下面的示例显示了如何只处理链接和段落,其他 HTML 元素保持原样:
1// Convert selective HTML tags to Markdown using C#
2
3// Prepare a path for converted file saving
4string savePath = Path.Combine(OutputDir, "options-output.md");
5
6// Prepare HTML code and save it to the file
7string code = "<h1>Header 1</h1>" +
8 "<h2>Header 2</h2>" +
9 "<p>Hello, World!!</p>" +
10 "<a href='aspose.com'>aspose</a>";
11File.WriteAllText(Path.Combine(OutputDir, "options.html"), code);
12
13// Create an instance of SaveOptions and set up the rule:
14// - only <a> and <p> elements will be converted to Markdown
15MarkdownSaveOptions options = new MarkdownSaveOptions();
16options.Features = MarkdownFeatures.Link | MarkdownFeatures.AutomaticParagraph;
17
18// Call the ConvertHTML() method to convert the HTML to Markdown
19Converter.ConvertHTML(Path.Combine(OutputDir, "options.html"), options, savePath);
要将 HTML 转换为 Markdown,可以定义自己的规则集或使用预定义模板。例如,可以使用基于 GitLab Flavored Markdown 语法的模板:
1// Convert HTML to Markdown in C# using Git syntax
2
3// Prepare a path for converted file saving
4string savePath = Path.Combine(OutputDir, "output-git.md");
5
6// Prepare HTML code and save it to the file
7string code = "<h1>Header 1</h1>" +
8 "<h2>Header 2</h2>" +
9 "<p>Hello, World!!</p>";
10File.WriteAllText(Path.Combine(OutputDir, "document.html"), code);
11
12// Call ConvertHTML() method to convert HTML to Markdown
13Converter.ConvertHTML(Path.Combine(OutputDir, "document.html"), MarkdownSaveOptions.Git, savePath);
限制
Markdown 是一种轻量级且易于使用的语法。不过,并非所有 HTML 元素都能转换为 Markdown,因为 Markdown 语法中没有对应的元素。在转换过程中, STYLE、 SCRIPT、 LINK、 EMBED 等元素将被丢弃。
Inline HTML
Markdown 允许您指定纯 HTML 代码,并按原样渲染。允许这种行为的功能称为 Inline HTML。要使用该功能,您应在新行开头放置该功能支持的特定元素之一。或者,您也可以通过在此类元素中添加inline值的markdown属性,将其标记为 Inline HTML。下面是一个演示如何使用该属性的小例子:
1// Convert inline HTML elements to Markdown using C#
2
3// Prepare a path for converted file saving
4string savePath = Path.Combine(OutputDir, "inline-html.md");
5
6// Prepare HTML code and save it to the file
7string code = "text<div markdown='inline'><code>text</code></div>";
8File.WriteAllText(Path.Combine(OutputDir, "inline.html"), code);
9
10// Call ConvertHTML() method to convert HTML to Markdown
11Converter.ConvertHTML(Path.Combine(OutputDir, "inline.html"), new MarkdownSaveOptions(), savePath);
12
13// Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>
如您所见,<div>
元素的内容不会转换为 Markdown 格式,而是由 Markdown 处理器按原样处理。每个 Markdown 处理器支持此功能的元素列表都不同。
最初的 Markdown 规范支持这些标记:blockquote、h1、h2、h3、h4、h5、h6、p、pre、ol、ul、dl、div、ins、del、iframe、fieldset、noscript、form、math。
GitLab Flavored Markdown 用以下标记扩展了该列表:article、footer、nav、ide aside、header、address、hr、dd、figure、figcaption、 abbr、video、audio、output、canvas、section、details、hgroup、summary。
功能嵌套
Markdown 支持很多功能,但并非所有功能都能同时使用。例如,表格元素中的列表元素就无法转换。下表显示了哪些功能可以嵌套。每个特性都是 MarkdownFeatures 枚举的成员。
Parent feature | Features which can be processed inside |
---|---|
Header | Link, Emphasis, Strong, InlineCode, Image, Strikethrough, Video |
Blockquote | Any |
List | AutomaticParagraph, Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough, Video, TaskList, List |
Link | Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough |
AutomaticParagraph | Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough |
Strikethrough | Link, Emphasis, Strong, InlineCode, Image, LineBreak |
Table | Video, Strikethrough, Image, InlineCode, Emphasis, Strong, Link |
Emphasis | Link, InlineCode, Image, LineBreak, Strikethrough, Video |
Strong | Link, InlineCode, Image, LineBreak, Strikethrough, Video |
您可以从 GitHub 下载完整的示例和数据文件。
下载 Aspose.HTML for .NET库,它能让您成功、快速、轻松地将 HTML、MHTML、EPUB、SVG 和 Markdown 文档转换为最流行的格式。
Aspose.HTML 提供一款免费的在线 HTML 到 MD 转换器,可将 HTML 高质量、方便快捷地转换为 Markdown。只需上传、转换您的文件,几秒钟就能得到结果!