Skip to content

Commit

Permalink
124: Added media parser from markdown files
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed Dec 22, 2024
1 parent 84f1f27 commit 12472f9
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public sealed class FilePath : ValueObject
{
public string Path { get; }
public string FileName { get; }

private string Extension { get; }

private FilePath(string path, string extension, string fileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace ASSISTENTE.Infrastructure.MarkDownParser.Contracts;
public interface IMarkDownParser
{
public Result<FileContent> Parse(FilePath filePath);
public Result<List<MediaUrl>> GetMediaUrls(FilePath filePath);
}
46 changes: 46 additions & 0 deletions API/ASSISTENTE.Infrastructure.MarkDownParser/Contracts/MediaUrl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using CSharpFunctionalExtensions;
using SOFTURE.Results;

namespace ASSISTENTE.Infrastructure.MarkDownParser.Contracts;

public sealed class MediaUrl : ValueObject
{
private static readonly List<string> _supportedExtensions =
[
"png",
"jpg",
"mp3"
];

public string Url { get; }
public string Extension { get; }

private MediaUrl(string url, string extension)
{
Url = url;
Extension = extension;
}

public static Result<MediaUrl> Create(string url)
{
if (string.IsNullOrEmpty(url))
return Result.Failure<MediaUrl>(CommonErrors.EmptyParameter.Build());

var extension = url.Split('.').Last();

return _supportedExtensions.Contains(extension)
? new MediaUrl(url, extension)
: Result.Failure<MediaUrl>(MediaUrlErrors.InvalidFileExtension.Build());
}

protected override IEnumerable<IComparable> GetEqualityComponents()
{
yield return Url;
}
}

public static class MediaUrlErrors
{
public static readonly Error InvalidFileExtension = new(
"MediaUrl.InvalidFileExtension", "Invalid file extension - only '.png, .jpg, .mp3' file types are allowed");
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ internal static class MarkDownParserErrors

public static readonly Error OnlyHeadersNotAllowed = new(
"MarkDownParser.OnlyHeadersNotAllowed", "Notes with only headers are not allowed");

public static readonly Error UnsupportedMedia = new(
"MarkDownParser.UnsupportedMedia", "Media type is not supported");
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ public static string GetContent(this LeafBlock leaf)
return string.Join(" ", contentsList);
}

public static string GetUrls(this LeafBlock leaf)
public static List<string> GetUrls(this LeafBlock leaf)
{
if (leaf.Inline == null) return string.Empty;
if (leaf.Inline == null) return [];

var urls = leaf.Inline
.Descendants()
.OfType<LinkInline>()
.Select(x => x.Url)
.Where(url => url != string.Empty)
.ToList();

return string.Join(" ", urls);
return urls!;
}

public static string GetCodeLine(this LeafBlock leaf)
Expand Down Expand Up @@ -91,8 +92,8 @@ public static string GetListContent(this ListBlock block)

var line = $"{counter}. {content}";

if (!string.IsNullOrEmpty(urls))
line += $" | Links associated with this point: {urls}";
if (urls.Count != 0)
line += $" | Links associated with this point: {string.Join(" ", urls)}";

listContent.Add(line);
}
Expand Down
26 changes: 25 additions & 1 deletion API/ASSISTENTE.Infrastructure.MarkDownParser/MarkDownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ public Result<FileContent> Parse(FilePath filePath)
return FileContent.Create(filePath.FileName, elements);
}

public Result<List<MediaUrl>> GetMediaUrls(FilePath filePath)
{
var content = File.ReadAllText(filePath.Path);
var parsedMarkdown = Markdown.Parse(content);

var mediaUrls = parsedMarkdown
.Descendants<ParagraphBlock>()
.Select(paragraphBlock => paragraphBlock.GetUrls())
.Where(mediaUrl => mediaUrl.Count != 0)
.SelectMany(mediaUrl => mediaUrl)
.Select(MediaUrl.Create)
.ToList();

if (mediaUrls.Count == 0)
return Result.Failure<List<MediaUrl>>(MarkDownParserErrors.EmptyContent.Build());

if (mediaUrls.Any(x => x.IsFailure))
return Result.Failure<List<MediaUrl>>(MarkDownParserErrors.UnsupportedMedia.Build());

return mediaUrls
.Select(x => x.Value)
.ToList();
}

private static Result<ElementBase> GetElement(IMarkdownObject block)
{
switch (block)
Expand Down Expand Up @@ -88,7 +112,7 @@ private static Result<ElementBase> GetElement(IMarkdownObject block)
{
var paragraphContent = paragraph.GetContent();
var paragraphUrls = paragraph.GetUrls();

var paragraphElement = new Paragraph(paragraphContent, paragraphUrls) as ElementBase;

return Result.Success(paragraphElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace ASSISTENTE.Infrastructure.MarkDownParser.Models;

internal sealed record Paragraph(string Content, string Urls) : ElementBase(Content)
internal sealed record Paragraph(string Content, List<string> Urls) : ElementBase(Content)
{
public string Urls { get; } = Urls;
public List<string> Urls { get; } = Urls;

public override string GetContent()
{
var content = $"Paragraph content: {Content}";

if (!string.IsNullOrEmpty(Urls))
content += $" | Urls connected with this paragraph: {Urls}";
if (Urls.Count != 0)
content += $" | Urls connected with this paragraph: {string.Join(" ", Urls)}";

return content;
}
Expand Down
Loading

0 comments on commit 12472f9

Please sign in to comment.