Markdown 转换器 – 用 C# 转换 MD

Markdown 是一种轻量级标记语言,用于在纯文本中显示格式。MD 文件使用由约翰-格鲁伯(John Gruber)提出和开发的 Markdown 语言。它们包括决定文本格式的内联文本字符,如缩进、表格格式、字体和标题。此外,MD 文件还可以转换为 HTML、PDF 或图像,以利用其他格式完成特定任务。

Aspose.HTML 的主要亮点是转换功能。Aspose.Html.Converters 命名空间实现了对转换方法的轻松访问。它提供了多种常用格式的转换,如 Markdown 到 HTML、Markdown 到 DOCX 或 Markdown 到 PDF。

本节将介绍支持的 Markdown 转换列表,以及如何使用 ConvertMarkdown() 方法执行这些转换。所有这些方法都允许将 Markdown 转换为 HTML。从 Markdown 到其他格式的转换需要经过 Markdown 到 HTML 的转换阶段。

请看下面的 C# 示例,该示例显示了在指定 ImageSaveOptions 时 Markdown 到 JPG 的转换:

 1// Convert Markdown to JPG in C# with custom settings
 2
 3// Prepare a path to a source Markdown file
 4string sourcePath = Path.Combine(DataDir, "nature.md");
 5
 6// Prepare a path to save the converted file
 7string savePath = Path.Combine(OutputDir, "nature-options.jpg");
 8
 9// Convert Markdown to HTML
10using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
11
12// Initialize ImageSaveOptions 
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
14{
15    UseAntialiasing = true,
16    HorizontalResolution = 200,
17    VerticalResolution = 200,
18    BackgroundColor = System.Drawing.Color.AliceBlue
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 950), new Margin(30, 20, 10, 10));
21
22// Convert HTML to JPG
23Converter.ConvertHTML(document, options, savePath);

在示例中, ConvertMarkdown(sourcePath) 方法获取 Markdown 文件的源路径,并生成一个 HTMLDocument 文件。ImageSaveOptions() 构造函数创建了一个新的 ImageSaveOptions 对象,该对象具有 JPG ImageFormat 和一组属性。然后, ConvertHTML() 方法接收 HTMLDocument、ImageSaveOptions 和输出路径,完成 Markdown 到 JPG 的转换。

Aspose.HTML 提供免费的在线 Markdown 转换器 将 Markdown 文件转换为各种流行格式。您可以轻松地将 Markdown 转换为 HTMLMarkdown 转换为 PDFMarkdown 转换为 JPGMHTML 转换为 DOCX。只需选择文件,选择要转换的格式,就大功告成了。它速度快,而且完全免费!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.