diff --git a/YaeBlog.Core/Abstractions/IEssayContentService.cs b/YaeBlog.Core/Abstractions/IEssayContentService.cs new file mode 100644 index 0000000..3fbbb42 --- /dev/null +++ b/YaeBlog.Core/Abstractions/IEssayContentService.cs @@ -0,0 +1,13 @@ +using System.Diagnostics.CodeAnalysis; +using YaeBlog.Core.Models; + +namespace YaeBlog.Core.Abstractions; + +public interface IEssayContentService +{ + public IReadOnlyDictionary Essays { get; } + + public IReadOnlyDictionary> Tags { get; } + + public bool SearchByUrlEncodedTag(string tag,[NotNullWhen(true)] out List? result); +} diff --git a/YaeBlog.Core/Extensions/WebApplicationBuilderExtensions.cs b/YaeBlog.Core/Extensions/WebApplicationBuilderExtensions.cs index 6ed6680..b4a6cd1 100644 --- a/YaeBlog.Core/Extensions/WebApplicationBuilderExtensions.cs +++ b/YaeBlog.Core/Extensions/WebApplicationBuilderExtensions.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using YaeBlog.Core.Abstractions; using YaeBlog.Core.Models; using YaeBlog.Core.Processors; using YaeBlog.Core.Services; @@ -20,6 +21,8 @@ public static class WebApplicationBuilderExtensions builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(provider => + provider.GetRequiredService()); builder.Services.AddTransient(); builder.Services.AddTransient(provider => provider.GetRequiredService>().Value); diff --git a/YaeBlog.Core/Models/EssayTag.cs b/YaeBlog.Core/Models/EssayTag.cs new file mode 100644 index 0000000..6528943 --- /dev/null +++ b/YaeBlog.Core/Models/EssayTag.cs @@ -0,0 +1,16 @@ +using System.Text.Encodings.Web; + +namespace YaeBlog.Core.Models; + +public class EssayTag(string tagName) : IEquatable +{ + public string TagName { get; } = tagName; + + public string UrlEncodedTagName { get; } = UrlEncoder.Default.Encode(tagName); + + public bool Equals(EssayTag? other) => other is not null && TagName == other.TagName; + + public override bool Equals(object? obj) => obj is EssayTag other && Equals(other); + + public override int GetHashCode() => TagName.GetHashCode(); +} diff --git a/YaeBlog.Core/Services/EssayContentService.cs b/YaeBlog.Core/Services/EssayContentService.cs index 2ddc98c..aa800f2 100644 --- a/YaeBlog.Core/Services/EssayContentService.cs +++ b/YaeBlog.Core/Services/EssayContentService.cs @@ -1,57 +1,48 @@ using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; +using YaeBlog.Core.Abstractions; using YaeBlog.Core.Models; namespace YaeBlog.Core.Services; -public class EssayContentService +public class EssayContentService : IEssayContentService { private readonly ConcurrentDictionary _essays = new(); - private readonly Dictionary> _tags = []; + private readonly Dictionary> _tags = []; - public bool TryGet(string key, out BlogEssay? essay) - => _essays.TryGetValue(key, out essay); + public bool TryAdd(BlogEssay essay) => _essays.TryAdd(essay.FileName, essay); - public bool TryAdd(string key, BlogEssay essay) => _essays.TryAdd(key, essay); + public IReadOnlyDictionary Essays => _essays; - public IDictionary Essays => _essays; + public IReadOnlyDictionary> Tags => _tags; public void RefreshTags() { - foreach (BlogEssay essay in _essays.Values) - { - foreach (string tag in essay.Tags) - { - if (_tags.TryGetValue(tag, out var list)) - { - list.Add(essay); - } - else - { - _tags[tag] = [essay]; - } - } - } + _tags.Clear(); - foreach (KeyValuePair> pair in _tags) - { - pair.Value.Sort(); - } + foreach (BlogEssay essay in _essays.Values) + { + foreach (EssayTag essayTag in essay.Tags.Select(tag => new EssayTag(tag))) + { + if (_tags.TryGetValue(essayTag, out List? essays)) + { + essays.Add(essay); + } + else + { + _tags.Add(essayTag, [essay]); + } + } + } } - public IEnumerable> Tags => from item in _tags - orderby item.Value.Count descending - select KeyValuePair.Create(item.Key, item.Value.Count); - - public int TagCount => _tags.Count; - - public IEnumerable GetTag(string tag) + public bool SearchByUrlEncodedTag(string tag, [NotNullWhen(true)] out List? result) { - if (_tags.TryGetValue(tag, out var list)) - { - return list; - } + result = (from item in _tags + where item.Key.UrlEncodedTagName == tag + select item.Value).FirstOrDefault(); - throw new KeyNotFoundException("Selected tag not found."); + return result is not null; } } diff --git a/YaeBlog.Core/Services/RendererService.cs b/YaeBlog.Core/Services/RendererService.cs index a4359ea..0e82fdb 100644 --- a/YaeBlog.Core/Services/RendererService.cs +++ b/YaeBlog.Core/Services/RendererService.cs @@ -125,7 +125,7 @@ public class RendererService(ILogger logger, essay = await processor.ProcessAsync(essay); } - if (!essayContentService.TryAdd(essay.FileName, essay)) + if (!essayContentService.TryAdd(essay)) { throw new BlogFileException( $"There are two essays with the same name: '{essay.FileName}'."); diff --git a/YaeBlog/Components/BlogInformationCard.razor b/YaeBlog/Components/BlogInformationCard.razor index 80dbf69..6c85775 100644 --- a/YaeBlog/Components/BlogInformationCard.razor +++ b/YaeBlog/Components/BlogInformationCard.razor @@ -1,7 +1,7 @@ +@using YaeBlog.Core.Abstractions @using YaeBlog.Core.Models -@using YaeBlog.Core.Services -@inject EssayContentService EssayContentInstance +@inject IEssayContentService Contents @inject BlogOptions Options @@ -36,7 +36,7 @@ diff --git a/YaeBlog/Pages/About.razor b/YaeBlog/Pages/About.razor index 22c02fe..38f3a36 100644 --- a/YaeBlog/Pages/About.razor +++ b/YaeBlog/Pages/About.razor @@ -1,5 +1,9 @@ @page "/blog/about" + + 关于 + +
diff --git a/YaeBlog/Pages/Archives.razor b/YaeBlog/Pages/Archives.razor index fee7717..fcf87e9 100644 --- a/YaeBlog/Pages/Archives.razor +++ b/YaeBlog/Pages/Archives.razor @@ -1,8 +1,12 @@ @page "/blog/archives" +@using YaeBlog.Core.Abstractions @using YaeBlog.Core.Models -@using YaeBlog.Core.Services -@inject EssayContentService EssayContentInstance +@inject IEssayContentService Contents + + + 归档 +
@@ -64,9 +68,8 @@ { base.OnInitialized(); - _essays.AddRange(from essay in EssayContentInstance.Essays + _essays.AddRange(from essay in Contents.Essays orderby essay.Value.PublishTime descending group essay by new DateTime(essay.Value.PublishTime.Year, 1, 1)); } - } diff --git a/YaeBlog/Pages/BlogIndex.razor b/YaeBlog/Pages/BlogIndex.razor index 78db229..9242a4b 100644 --- a/YaeBlog/Pages/BlogIndex.razor +++ b/YaeBlog/Pages/BlogIndex.razor @@ -1,8 +1,8 @@ @page "/blog" +@using YaeBlog.Core.Abstractions @using YaeBlog.Core.Models -@using YaeBlog.Core.Services -@inject EssayContentService EssayContentInstance +@inject IEssayContentService Contents @inject NavigationManager NavigationInstance @@ -99,15 +99,15 @@ protected override void OnInitialized() { _page = Page ?? 1; - _pageCount = EssayContentInstance.Essays.Count / EssaysPerPage + 1; + _pageCount = Contents.Essays.Count / EssaysPerPage + 1; - if (EssaysPerPage * _page > EssayContentInstance.Essays.Count + EssaysPerPage) + if (EssaysPerPage * _page > Contents.Essays.Count + EssaysPerPage) { NavigationInstance.NavigateTo("/NotFount"); return; } - _essays.AddRange(EssayContentInstance.Essays + _essays.AddRange(Contents.Essays .OrderByDescending(p => p.Value.PublishTime) .Skip((_page - 1) * EssaysPerPage) .Take(EssaysPerPage)); diff --git a/YaeBlog/Pages/Essays.razor b/YaeBlog/Pages/Essays.razor index 24fb49e..44c23a0 100644 --- a/YaeBlog/Pages/Essays.razor +++ b/YaeBlog/Pages/Essays.razor @@ -1,9 +1,9 @@ @page "/blog/essays/{BlogKey}" +@using YaeBlog.Core.Abstractions @using YaeBlog.Core.Models -@using YaeBlog.Core.Services +@inject IEssayContentService Contents @inject NavigationManager NavigationInstance -@inject EssayContentService EssayContentInstance @(_essay!.Title) @@ -52,7 +52,7 @@ return; } - if (!EssayContentInstance.TryGet(BlogKey, out _essay)) + if (!Contents.Essays.TryGetValue(BlogKey, out _essay)) { NavigationInstance.NavigateTo("/NotFound"); } diff --git a/YaeBlog/Pages/NotFound.razor b/YaeBlog/Pages/NotFound.razor index cd87b0d..6440b85 100644 --- a/YaeBlog/Pages/NotFound.razor +++ b/YaeBlog/Pages/NotFound.razor @@ -1,5 +1,9 @@ @page "/NotFound" + + 啊~ 页面走丢啦~ + +

NotFound!

diff --git a/YaeBlog/Pages/Tags.razor b/YaeBlog/Pages/Tags.razor index 6319668..c7b75bc 100644 --- a/YaeBlog/Pages/Tags.razor +++ b/YaeBlog/Pages/Tags.razor @@ -1,8 +1,14 @@ @page "/blog/tags/" +@using System.Text.Encodings.Web +@using YaeBlog.Core.Abstractions @using YaeBlog.Core.Models -@using YaeBlog.Core.Services -@inject EssayContentService EssayContentInstance +@inject IEssayContentService Contents +@inject NavigationManager NavigationInstance + + + @(TagName ?? "标签") +