refacto FrontmatterParser
This commit is contained in:
parent
7b3a236bcc
commit
ef0f5ddcac
3 changed files with 10 additions and 9 deletions
|
|
@ -6,7 +6,6 @@ namespace Yavsc.Controllers.Generic
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Models;
|
using Models;
|
||||||
using Yavsc.Helpers;
|
|
||||||
using Yavsc.Server.Helpers;
|
using Yavsc.Server.Helpers;
|
||||||
using Yavsc.Services;
|
using Yavsc.Services;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ namespace Yavsc.Server.Services;
|
||||||
|
|
||||||
public static class FrontmatterParser
|
public static class FrontmatterParser
|
||||||
{
|
{
|
||||||
public const string YamlSeparator = "---";
|
public const string YamlHeader = "---\n";
|
||||||
|
public const string YamlSeparator = "\n---";
|
||||||
|
|
||||||
private static readonly IDeserializer _deserializer =
|
private static readonly IDeserializer _deserializer =
|
||||||
new DeserializerBuilder()
|
new DeserializerBuilder()
|
||||||
|
|
@ -17,14 +18,15 @@ public static class FrontmatterParser
|
||||||
public static FrontmatterOffreResult Parse(string markdown)
|
public static FrontmatterOffreResult Parse(string markdown)
|
||||||
{
|
{
|
||||||
// Le contenu doit commencer par ----
|
// Le contenu doit commencer par ----
|
||||||
if (!markdown.TrimStart().StartsWith(YamlSeparator))
|
if (!markdown.TrimStart().StartsWith(YamlHeader, StringComparison.Ordinal))
|
||||||
return new FrontmatterOffreResult { Corps = markdown };
|
return new FrontmatterOffreResult { Corps = markdown };
|
||||||
var parts = markdown.TrimStart()[3..].Split(new[] { "\n"+YamlSeparator }, 2,
|
|
||||||
|
var parts = markdown.Substring(YamlHeader.Length).Trim().Split(new[] { YamlSeparator }, 2,
|
||||||
StringSplitOptions.None);
|
StringSplitOptions.None);
|
||||||
if (parts.Length < 2)
|
if (parts.Length < 2)
|
||||||
return new FrontmatterOffreResult { Corps = markdown };
|
return new FrontmatterOffreResult { Corps = markdown };
|
||||||
var yamlBlock = parts[0].Trim();
|
var yamlBlock = parts[0];
|
||||||
var corps = parts[1].Trim();
|
var corps = parts[1];
|
||||||
var meta = _deserializer.Deserialize<FrontmatterOffreResult>(yamlBlock);
|
var meta = _deserializer.Deserialize<FrontmatterOffreResult>(yamlBlock);
|
||||||
meta.Corps = corps;
|
meta.Corps = corps;
|
||||||
return meta;
|
return meta;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue