Web-Development-with-Blazor.../Chapter11/MyBlog/Components/Pages/Post.razor
2023-02-17 15:28:17 +01:00

57 lines
1.7 KiB
Plaintext

@page "/post/{BlogPostId}"
@inject IBlogApi _api
@inject NavigationManager _navman
@using Components.Interfaces
@inject IBlogNotificationService notificationService
@implements IDisposable
@using Markdig
@if (BlogPost != null)
{
<PageTitle>@BlogPost.Title</PageTitle>
<HeadContent>
<meta property="og:title" content="@BlogPost.Title" />
<meta property="og:description" content="@(new string(BlogPost.Text.Take(100).ToArray()))" />
<meta property="og:image" content="@($"{_navman.BaseUri}/pathtoanimage.png")" />
<meta property="og:url" content="@_navman.Uri" />
<meta name="twitter:card" content="@(new string(BlogPost.Text.Take(100).ToArray()))" />
</HeadContent>
<h2>@BlogPost.Title</h2>
@((MarkupString)Markdig.Markdown.ToHtml(BlogPost.Text, pipeline))
}
@code {
[Parameter]
public string BlogPostId { get; set; } = default!;
public BlogPost? BlogPost { get; set; }
protected async override Task OnParametersSetAsync()
{
BlogPost = await _api.GetBlogPostAsync(BlogPostId);
await base.OnParametersSetAsync();
}
MarkdownPipeline pipeline;
protected override Task OnInitializedAsync()
{
notificationService.BlogPostChanged += PostChanged;
pipeline = new MarkdownPipelineBuilder()
.UseEmojiAndSmiley()
.Build();
return base.OnInitializedAsync();
}
private async void PostChanged(BlogPost post)
{
if (BlogPost?.Id == post.Id)
{
BlogPost = post;
await InvokeAsync(() => this.StateHasChanged());
}
}
void IDisposable.Dispose()
{
notificationService.BlogPostChanged -= PostChanged;
}
}