Skip to content

Commit 376e3d8

Browse files
authored
Merge pull request #295 from fullreboot/SearchFix
Search, Canonical Pages URL, Metadata fixes
2 parents f48786f + 4d548ac commit 376e3d8

File tree

8 files changed

+132
-96
lines changed

8 files changed

+132
-96
lines changed

src/Blogifier.Admin/Pages/Account/Register.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Blogifier.Shared;
1+
using Blogifier.Shared;
22
using System.Net.Http.Json;
33
using System.Threading.Tasks;
44

@@ -16,7 +16,7 @@ public async Task RegisterUser()
1616
if (result.IsSuccessStatusCode)
1717
{
1818
showError = false;
19-
_navigationManager.NavigateTo("admin/", true);
19+
_navigationManager.NavigateTo("admin", true);
2020
}
2121
else
2222
{

src/Blogifier.Admin/Pages/Pages/Editor.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</svg>
3434
<span class="ms-2 d-none d-lg-inline">@_localizer["delete"]</span>
3535
</button>
36-
<a href="posts/@Post.Slug" class="btn btn-link text-white" target="_blank">
36+
<a href="/@Post.Slug" class="btn btn-link text-white" target="_blank">
3737
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-up-right" viewBox="0 0 16 16">
3838
<path fill-rule="evenodd" d="M14 2.5a.5.5 0 0 0-.5-.5h-6a.5.5 0 0 0 0 1h4.793L2.146 13.146a.5.5 0 0 0 .708.708L13 3.707V8.5a.5.5 0 0 0 1 0v-6z" />
3939
</svg>

src/Blogifier/Blog.db

0 Bytes
Binary file not shown.

src/Blogifier/Controllers/HomeController.cs

Lines changed: 118 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -37,73 +37,57 @@ public HomeController(IBlogProvider blogProvider,
3737
_compositeViewEngine = compositeViewEngine;
3838
}
3939

40-
public async Task<IActionResult> Index(string term, int page = 1)
40+
public async Task<IActionResult> Index(int page = 1)
4141
{
42-
var model = new ListModel { PostListType = PostListType.Blog };
43-
try
44-
{
45-
model.Blog = await _blogProvider.GetBlogItem();
46-
}
47-
catch
48-
{
49-
return Redirect("~/admin");
50-
}
5142

52-
model.Pager = new Pager(page, model.Blog.ItemsPerPage);
43+
var model = await getBlogPosts(pager: page);
5344

54-
if (string.IsNullOrEmpty(term))
55-
{
56-
if (model.Blog.IncludeFeatured)
57-
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "FP");
58-
else
59-
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "P");
60-
}
61-
else
62-
{
63-
model.PostListType = PostListType.Search;
64-
model.Blog.Title = term;
65-
model.Blog.Description = "";
66-
model.Posts = await _postProvider.Search(model.Pager, term, 0, "FP");
67-
}
45+
//If no blogs are setup redirect to first time registration
46+
if(model == null){
47+
return Redirect("~/admin/register");
48+
}
49+
50+
return View($"~/Views/Themes/{model.Blog.Theme}/Index.cshtml", model);
51+
}
52+
53+
[HttpGet("/{slug}")]
54+
public async Task<IActionResult> Index(string slug)
55+
{
56+
if (!string.IsNullOrEmpty(slug))
57+
{
58+
return await getSingleBlogPost(slug);
59+
}
60+
return Redirect("~/");
61+
}
6862

69-
if (model.Pager.ShowOlder) model.Pager.LinkToOlder = $"?page={model.Pager.Older}";
70-
if (model.Pager.ShowNewer) model.Pager.LinkToNewer = $"?page={model.Pager.Newer}";
63+
[HttpGet("/admin")]
64+
public async Task<IActionResult> Admin()
65+
{
66+
return File("~/index.html", "text/html");
67+
}
7168

69+
[HttpPost]
70+
public async Task<IActionResult> Search(string term, int page = 1)
71+
{
72+
7273
if (!string.IsNullOrEmpty(term))
7374
{
75+
var model = await getBlogPosts(term, page);
7476
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Search.cshtml";
7577
if (IsViewExists(viewPath))
7678
return View(viewPath, model);
79+
else
80+
return Redirect("~/home");
7781
}
82+
else{
83+
return Redirect("~/home");
84+
}
85+
}
7886

79-
return View($"~/Views/Themes/{model.Blog.Theme}/Index.cshtml", model);
80-
}
81-
82-
[HttpPost]
83-
public IActionResult Search(string term)
84-
{
85-
return Redirect($"/home?term={term}");
86-
}
87-
88-
[HttpGet("categories/{category}")]
87+
[HttpGet("categories/{category}")]
8988
public async Task<IActionResult> Categories(string category, int page = 1)
9089
{
91-
var model = new ListModel { PostListType = PostListType.Category };
92-
try
93-
{
94-
model.Blog = await _blogProvider.GetBlogItem();
95-
}
96-
catch
97-
{
98-
return Redirect("~/admin");
99-
}
100-
101-
model.Pager = new Pager(page, model.Blog.ItemsPerPage);
102-
model.Posts = await _postProvider.GetList(model.Pager, 0, category, "PF");
103-
104-
if (model.Pager.ShowOlder) model.Pager.LinkToOlder = $"?page={model.Pager.Older}";
105-
if (model.Pager.ShowNewer) model.Pager.LinkToNewer = $"?page={model.Pager.Newer}";
106-
90+
var model = await getBlogPosts("", page, category);
10791
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Category.cshtml";
10892

10993
if (IsViewExists(viewPath))
@@ -112,41 +96,11 @@ public async Task<IActionResult> Categories(string category, int page = 1)
11296
return View($"~/Views/Themes/{model.Blog.Theme}/Index.cshtml", model);
11397
}
11498

115-
[HttpGet("posts/{slug}")]
116-
public async Task<IActionResult> Single(string slug)
117-
{
118-
try
119-
{
120-
ViewBag.Slug = slug;
121-
PostModel model = await _postProvider.GetPostModel(slug);
122-
123-
// If unpublished and unauthorised redirect to error / 404.
124-
if (model.Post.Published == DateTime.MinValue && !User.Identity.IsAuthenticated)
125-
{
126-
return Redirect("~/error");
127-
}
128-
129-
model.Blog = await _blogProvider.GetBlogItem();
130-
model.Post.Description = model.Post.Description.MdToHtml();
131-
model.Post.Content = model.Post.Content.MdToHtml();
132-
133-
if(!model.Post.Author.Avatar.StartsWith("data:"))
134-
model.Post.Author.Avatar = Url.Content($"~/{model.Post.Author.Avatar}");
135-
136-
if(model.Post.PostType == PostType.Page)
137-
{
138-
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Page.cshtml";
139-
if (IsViewExists(viewPath))
140-
return View(viewPath, model);
141-
}
142-
143-
return View($"~/Views/Themes/{model.Blog.Theme}/Post.cshtml", model);
144-
}
145-
catch
146-
{
147-
return Redirect("~/error");
148-
}
149-
}
99+
[HttpGet("posts/{slug}")]
100+
public async Task<IActionResult> Single(string slug)
101+
{
102+
return await getSingleBlogPost(slug);
103+
}
150104

151105
[HttpGet("error")]
152106
public async Task<IActionResult> Error()
@@ -226,5 +180,81 @@ private bool IsViewExists(string viewPath)
226180
var result = _compositeViewEngine.GetView("", viewPath, false);
227181
return result.Success;
228182
}
183+
184+
185+
public async Task<IActionResult> getSingleBlogPost(string slug){
186+
try
187+
{
188+
ViewBag.Slug = slug;
189+
PostModel model = await _postProvider.GetPostModel(slug);
190+
191+
// If unpublished and unauthorised redirect to error / 404.
192+
if (model.Post.Published == DateTime.MinValue && !User.Identity.IsAuthenticated)
193+
{
194+
return Redirect("~/error");
195+
}
196+
197+
model.Blog = await _blogProvider.GetBlogItem();
198+
model.Post.Description = model.Post.Description.MdToHtml();
199+
model.Post.Content = model.Post.Content.MdToHtml();
200+
201+
if (!model.Post.Author.Avatar.StartsWith("data:"))
202+
model.Post.Author.Avatar = Url.Content($"~/{model.Post.Author.Avatar}");
203+
204+
if (model.Post.PostType == PostType.Page)
205+
{
206+
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Page.cshtml";
207+
if (IsViewExists(viewPath))
208+
return View(viewPath, model);
209+
}
210+
211+
return View($"~/Views/Themes/{model.Blog.Theme}/Post.cshtml", model);
212+
}
213+
catch
214+
{
215+
return Redirect("~/error");
216+
}
217+
}
218+
public async Task<ListModel> getBlogPosts(string term ="", int pager = 1, string category = "", string slug = ""){
219+
220+
var model = new ListModel{};
221+
222+
try
223+
{
224+
model.Blog = await _blogProvider.GetBlogItem();
225+
}
226+
catch
227+
{
228+
return null;
229+
}
230+
231+
model.Pager = new Pager(pager, model.Blog.ItemsPerPage);
232+
233+
if(!string.IsNullOrEmpty(category))
234+
{
235+
model.PostListType = PostListType.Category;
236+
model.Posts = await _postProvider.GetList(model.Pager, 0, category, "PF");
237+
}
238+
else if (string.IsNullOrEmpty(term))
239+
{
240+
model.PostListType = PostListType.Blog;
241+
if (model.Blog.IncludeFeatured)
242+
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "FP");
243+
else
244+
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "P");
245+
}
246+
else
247+
{
248+
model.PostListType = PostListType.Search;
249+
model.Blog.Title = term;
250+
model.Blog.Description = "";
251+
model.Posts = await _postProvider.Search(model.Pager, term, 0, "FP");
252+
}
253+
254+
if (model.Pager.ShowOlder) model.Pager.LinkToOlder = $"?page={model.Pager.Older}";
255+
if (model.Pager.ShowNewer) model.Pager.LinkToNewer = $"?page={model.Pager.Newer}";
256+
257+
return model;
258+
}
229259
}
230260
}

src/Blogifier/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void ConfigureServices(IServiceCollection services)
4444

4545
services.AddBlogProviders();
4646

47+
4748
services.AddControllersWithViews();
4849
services.AddRazorPages();
4950

src/Blogifier/Views/Shared/HeaderScript.cshtml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@using Blogifier.Core
1+
@using Blogifier.Core
22
@{
33
var request = Url.ActionContext.HttpContext.Request;
44
var absoluteUrl = $"{request.Scheme}://{request.Host.ToUriComponent()}{request.PathBase.ToUriComponent()}";
@@ -9,7 +9,12 @@
99
<title>@postModel.Post.Title &amp; @postModel.Blog.Title</title>
1010
<meta name="author" content="@postModel.Post.Author.DisplayName">
1111
<meta name="description" content="@postModel.Post.Description.StripHtml()">
12-
<link rel="canonical" href="@absoluteUrl/posts/@postModel.Post.Slug" />
12+
if(postModel.Post.PostType == PostType.Page){
13+
<link rel="canonical" href="@absoluteUrl/@postModel.Post.Slug" />
14+
}
15+
else{
16+
<link rel="canonical" href="@absoluteUrl/posts/@postModel.Post.Slug" />
17+
}
1318
}
1419
else
1520
{
@@ -19,4 +24,4 @@
1924
}
2025
<link rel="alternate" type="application/rss+xml" title="@Model.Blog.Title" href="@absoluteUrl/feed/rss" />
2126
}
22-
@Html.Raw(Model.Blog.HeaderScript)
27+
@Html.Raw(Model.Blog.HeaderScript)

src/Blogifier/Views/Themes/standard/components/metadata.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
PostModel postModel = (PostModel)Model;
66
var postTitle = postModel.Post.Title;
77
var postDesc = postModel.Post.Description.StripHtml();
8-
var postUrl = absoluteUrl + "/posts/" + postModel.Post.Slug;
8+
var postUrl = postModel.Post.PostType == PostType.Post ? absoluteUrl + "/posts/" + postModel.Post.Slug : absoluteUrl + "/" + postModel.Post.Slug;
99
var postCover = absoluteUrl + "/" + postModel.Post.Cover;
1010
var postPublished = postModel.Post.Published.ToString("s");
1111
var postAuthor = postModel.Post.Author.DisplayName;

src/Blogifier/Views/Themes/standard/layouts/_main.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
PostModel postModel = (PostModel)Model;
1616
pageTitle = postModel.Post.Title + " | " + siteTitle;
1717
pageDesc = postModel.Post.Description.StripHtml();
18-
pageCanonical = absoluteUrl + "/posts/" + postModel.Post.Slug;
18+
pageCanonical = postModel.Post.PostType == PostType.Post ? absoluteUrl + "/posts/" + postModel.Post.Slug : absoluteUrl + "/" + postModel.Post.Slug;
1919
}
2020
}
2121

0 commit comments

Comments
 (0)