Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search for posts in sub-directories #12

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
Expand Down
5 changes: 2 additions & 3 deletions BlazorStaticWebsite/Components/Blog/PostsList.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@using System.Globalization
@using Markdig
@using BlazorStatic
@using BlazorStatic.Services
@inject BlogService<FrontMatter> BlogService
Expand All @@ -25,7 +24,7 @@
<div class="space-y-6">
<div>
<h2 class="text-2xl font-bold leading-8 tracking-tight">
<a class="text-gray-100" href="@BlogService.Options.BlogPageUrl/@(post.FileNameNoExtension)">@post.FrontMatter.Title</a>
<a class="text-gray-100" href="@BlogService.Options.BlogPageUrl/@(post.Url)">@post.FrontMatter.Title</a>
</h2>
<div class="flex flex-wrap">
@foreach (var tag in post.FrontMatter.Tags)
Expand All @@ -39,7 +38,7 @@
</div>
</div>
<div class="text-base font-medium leading-6">
<a class="text-primary-500 hover:text-primary-400" aria-label='Read "@(post.FrontMatter.Lead)"' href="@BlogService.Options.BlogPageUrl/@(post.FileNameNoExtension)">Read more →</a>
<a class="text-primary-500 hover:text-primary-400" aria-label='Read "@(post.FrontMatter.Lead)"' href="@BlogService.Options.BlogPageUrl/@(post.Url)">Read more →</a>
</div>
</div>
</div>
Expand Down
13 changes: 6 additions & 7 deletions BlazorStaticWebsite/Components/Pages/Blog.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@page "/blog"
@page "/blog/{fileName}"
@using Microsoft.Extensions.Options
@page "/blog/{*postUrl}"
@using System.Globalization
@using BlazorStatic
@using BlazorStatic.Services
Expand All @@ -9,7 +8,7 @@


@* No filename -> show latest posts *@
@if (string.IsNullOrWhiteSpace(FileName))
@if (string.IsNullOrWhiteSpace(PostUrl))
{
<div class="divide-y divide-gray-700">
<div class="space-y-2 pb-8 pt-6 md:space-y-5">
Expand Down Expand Up @@ -88,7 +87,7 @@
@((MarkupString)post.HtmlContent)
</div>
<div class="pb-6 pt-6 text-sm text-gray-300">
<a target="_blank" rel="noopener noreferrer" href="@(WebsiteKeys.BlogPostStorageAddress+FileName+".md")">View on GitHub</a>
<a target="_blank" rel="noopener noreferrer" href="@(WebsiteKeys.BlogPostStorageAddress+PostUrl+".md")">View on GitHub</a>
</div>
</div>
<footer>
Expand All @@ -113,13 +112,13 @@


@code{
[Parameter] public string? FileName { get; set; }
[Parameter] public string? PostUrl { get; set; }
Post<FrontMatter>? post;

protected override void OnInitialized()
{
if (string.IsNullOrWhiteSpace(FileName)) return;
post = BlogService.BlogPosts.FirstOrDefault(x => x.FileNameNoExtension == FileName);
if (string.IsNullOrWhiteSpace(PostUrl)) return;
post = BlogService.BlogPosts.FirstOrDefault(x => x.Url == PostUrl);

}
}
Binary file not shown.
16 changes: 16 additions & 0 deletions BlazorStaticWebsite/Content/Blog/subfolder/post-in-subfolder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Test post for subfolder
lead: Just testing if post in subfolder works
published: 2024-05-29
tags: [test]
authors:
- name: "Jan Tesař"
gitHubUserName: "tesar-tech"
twitterUserName: "tesar-tech"
---

Yes, it **works**!

And also an image from subfolder:

![Test image](media/subfolder-for-media/image-in-subfolder.webp)
2 changes: 1 addition & 1 deletion src/BlazorStatic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<Description>Static site generator for Blazor on .NET8.</Description>
<PackageId>BlazorStatic</PackageId>
<!--Set EnvironmentName using dotnet build -c Release -p:EnvironmentName=Development for local package build-->
<Version Condition="'$(EnvironmentName)' != 'Development'">1.0.0-beta.5</Version>
<Version Condition="'$(EnvironmentName)' != 'Development'">1.0.0-beta.6</Version>
<Version Condition="'$(EnvironmentName)' == 'Development'">1.0.0-dev.$([System.DateTime]::Now.ToString("yyMMddHHmmss"))</Version>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/tesar-tech/BlazorStatic/</RepositoryUrl>
Expand Down
7 changes: 4 additions & 3 deletions src/Dtos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ public class Post<TFrontMatter>
/// </summary>
public required TFrontMatter FrontMatter { get; set; }
/// <summary>
/// Name of the blog post file without extension.
/// Used as url param "blog/{FileNameNoExtension}".
/// The url where the blog post will be generated.
/// Processed from the file path (Content/Blog/subfolder/post-in-subfolder.md => blog/subfolder/post-in-subfolder).
/// Used as url param "blog/{Url}".
/// </summary>
public required string FileNameNoExtension { get; set; }
public required string Url { get; set; }
/// <summary>
/// HTML content of the blog post. Parsed from md. Without front matter part.
/// </summary>
Expand Down
26 changes: 21 additions & 5 deletions src/Services/BlogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class BlogService<TFrontMatter>(BlogOptions<TFrontMatter> options,
/// </summary>
public async Task ParseAndAddBlogPosts()
{
string absContentPath;//gets initialized in GetPostsPath
var files = GetPostsPath();

foreach (string file in files)
Expand All @@ -48,12 +49,12 @@ public async Task ParseAndAddBlogPosts()
Post<TFrontMatter> post = new()
{
FrontMatter = frontMatter,
FileNameNoExtension = Path.GetFileNameWithoutExtension(file),
Url = GetRelativePathWithFilename(file),
HtmlContent = htmlContent
};
options.Posts.Add(post);

blazorStaticService.Options.PagesToGenerate.Add(new($"{options.BlogPageUrl}/{post.FileNameNoExtension}", Path.Combine("blog", $"{post.FileNameNoExtension}.html")));
blazorStaticService.Options.PagesToGenerate.Add(new($"{options.BlogPageUrl}/{post.Url}", Path.Combine("blog", $"{post.Url}.html")));
}

//copy media folder to output
Expand All @@ -70,10 +71,25 @@ public async Task ParseAndAddBlogPosts()
}
}
options.AfterBlogParsedAndAddedAction?.Invoke();

string[] GetPostsPath(){
return;

string[] GetPostsPath(){//retrieves blog post from bin folder, where the app is running
EnumerationOptions enumerationOptions = new()
{
IgnoreInaccessible = true,
RecurseSubdirectories = true,
};
string execFolder = Directory.GetParent((Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).Location)!.FullName;//! is ok, null only in empty path or root
return Directory.GetFiles(Path.Combine(execFolder, options.ContentPath), options.PostFilePattern);
absContentPath = Path.Combine(execFolder, options.ContentPath);
return Directory.GetFiles(absContentPath, options.PostFilePattern, enumerationOptions);
}

//ex: file= "C:\Users\user\source\repos\MyBlog\Content\Blog\en\somePost.md"
//returns "en/somePost"
string GetRelativePathWithFilename(string file)
{
string relativePathWithFileName = Path.GetRelativePath(absContentPath, file);
return Path.Combine(Path.GetDirectoryName(relativePathWithFileName)!, Path.GetFileNameWithoutExtension(relativePathWithFileName)).Replace("\\","/");
}
}

Expand Down