Front matters
This commit is contained in:
parent
514549c5f9
commit
4034c39905
6 changed files with 215 additions and 12 deletions
32
src/Yavsc.Server/Services/FrontmatterParser.cs
Normal file
32
src/Yavsc.Server/Services/FrontmatterParser.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
using Yavsc.Server.Models.Billing;
|
||||
|
||||
namespace Yavsc.Server.Services;
|
||||
|
||||
public static class FrontmatterParser
|
||||
{
|
||||
public const string YamlSeparator = "---";
|
||||
|
||||
private static readonly IDeserializer _deserializer =
|
||||
new DeserializerBuilder()
|
||||
.WithNamingConvention(CamelCaseNamingConvention.Instance)
|
||||
.IgnoreUnmatchedProperties()
|
||||
.Build();
|
||||
|
||||
public static FrontmatterOffreResult Parse(string markdown)
|
||||
{
|
||||
// Le contenu doit commencer par ----
|
||||
if (!markdown.TrimStart().StartsWith(YamlSeparator))
|
||||
return new FrontmatterOffreResult { Corps = markdown };
|
||||
var parts = markdown.TrimStart()[3..].Split(new[] { "\n"+YamlSeparator }, 2,
|
||||
StringSplitOptions.None);
|
||||
if (parts.Length < 2)
|
||||
return new FrontmatterOffreResult { Corps = markdown };
|
||||
var yamlBlock = parts[0].Trim();
|
||||
var corps = parts[1].Trim();
|
||||
var meta = _deserializer.Deserialize<FrontmatterOffreResult>(yamlBlock);
|
||||
meta.Corps = corps;
|
||||
return meta;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue