.NET 中的标记文件 slug

问题描述 投票:0回答:1

我现在正在使用

Markdig
nuget 包来读取我的标记文件并将其转换为 HTML

 string content = File.ReadAllText(mdFilepath);

 string? result = Markdown.ToHtml(content, pipeline);

但现在我在每个博客的开头添加了以下几行:

---
title: 'This is the title of the blog'
description: 'Dummy description'
pubDate: 'Feb 11 2023'
coverImage: './blogs/covers/blog1.png'
---

This is blog, lorem ipsum ......
More content....

此外,

File.ReadAllText
方法无法读取 --- 标签中的这些行。

我如何创建/使用某种类型的slug或转换器/阅读器,我应该在不同的模型中获得上述详细信息。

.net dotnetnuke markup readme markdig
1个回答
0
投票

读取该块(内部是 YAML)。

  1. 在构建解析器管道时通过调用

    UseYamlFrontMatter()
    将 YAML front Matter 支持添加到 Markdig 的解析器。

  2. 当您获得来自

    MarkdownParser.Parse(markdownContent, markdownPipeline)
    的 AST 后,您可以找到第一个
    YamlFrontMatterBlock

    if (ast.FirstOrDefault(blk => blk is YamlFrontMatterBlock) is YamlFrontMatterBlock yb) {
       // yb is the front matter
    }
    
  3. 您需要一个 YAML 解析器(我之前使用过 YamlDotNet):

    var yanl = yb.Lines.ToString();
    var information = yamlDeserialiser.Deserialize<MarkdownFrontMatter>(yaml);
    

其中

MarkdownFrontMatter
是具有要提取的属性集的类型(使用可为空的字符串属性以避免属性没有值时出现问题)。

© www.soinside.com 2019 - 2024. All rights reserved.