Skip to content

Commit 44618c3

Browse files
committed
Post APIs to have html/markdown switch
1 parent 11a5a2c commit 44618c3

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

src/App/Pages/Admin/Posts/Edit.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
},
127127
methods: {
128128
load: function () {
129-
axios.get(getUrl('api/posts/' + this.id))
129+
axios.get(getUrl('api/posts/' + this.id + '?format=markdown'))
130130
.then(response => {
131131
this.post = response.data;
132132
simplemde = getEditor(this.post);

src/Core/Api/PostsController.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Core.Data;
22
using Core.Helpers;
33
using Core.Services;
4+
using Markdig;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Mvc;
@@ -28,13 +29,15 @@ public PostsController(IDataService data)
2829
/// <param name="author">Author</param>
2930
/// <param name="include">Posts to include: all by default; F - featured, D - drafts, P - published</param>
3031
/// <param name="page">Page number</param>
32+
/// <param name="format">Otput format: html or markdown; default = html;</param>
3133
/// <returns>Model with list of posts and pager</returns>
3234
[HttpGet("search/{term}")]
3335
public async Task<ActionResult<PageListModel>> Search(
3436
string term,
3537
[FromQuery]string author = "",
3638
[FromQuery]string include = "",
37-
[FromQuery]int page = 1)
39+
[FromQuery]int page = 1,
40+
[FromQuery]string format = "html")
3841
{
3942
try
4043
{
@@ -45,6 +48,15 @@ public async Task<ActionResult<PageListModel>> Search(
4548

4649
results = await _data.BlogPosts.Search(pager, term, authorId, include, !User.Identity.IsAuthenticated);
4750

51+
if(format.ToUpper() == "HTML")
52+
{
53+
foreach (var p in results)
54+
{
55+
p.Description = Markdown.ToHtml(p.Description);
56+
p.Content = Markdown.ToHtml(p.Content);
57+
}
58+
}
59+
4860
return Ok(new PageListModel { Posts = results, Pager = pager });
4961
}
5062
catch (Exception)
@@ -60,13 +72,15 @@ public async Task<ActionResult<PageListModel>> Search(
6072
/// <param name="category">Post category</param>
6173
/// <param name="include">Posts to include: all by default; F - featured, D - drafts, P - published</param>
6274
/// <param name="page">Page number</param>
75+
/// <param name="format">Otput format: html or markdown; default = html;</param>
6376
/// <returns>Model with list of posts and pager</returns>
6477
[HttpGet]
6578
public async Task<ActionResult<PageListModel>> Get(
6679
[FromQuery]string author = "",
6780
[FromQuery]string category = "",
6881
[FromQuery]string include = "",
69-
[FromQuery]int page = 1)
82+
[FromQuery]int page = 1,
83+
[FromQuery]string format = "html")
7084
{
7185
try
7286
{
@@ -77,6 +91,15 @@ public async Task<ActionResult<PageListModel>> Get(
7791

7892
results = await _data.BlogPosts.GetList(pager, authorId, category, include, !User.Identity.IsAuthenticated);
7993

94+
if (format.ToUpper() == "HTML")
95+
{
96+
foreach (var p in results)
97+
{
98+
p.Description = Markdown.ToHtml(p.Description);
99+
p.Content = Markdown.ToHtml(p.Content);
100+
}
101+
}
102+
80103
return Ok(new PageListModel { Posts = results, Pager = pager });
81104
}
82105
catch (Exception)
@@ -89,13 +112,20 @@ public async Task<ActionResult<PageListModel>> Get(
89112
/// Get single post by ID
90113
/// </summary>
91114
/// <param name="id">Post ID</param>
115+
/// <param name="format">Otput format: html or markdown; default = html;</param>
92116
/// <returns>Post item</returns>
93117
[HttpGet("{id}")]
94-
public async Task<PostItem> GetPost(int id)
118+
public async Task<PostItem> GetPost(int id, [FromQuery]string format = "html")
95119
{
96120
if (id > 0)
97121
{
98-
return await _data.BlogPosts.GetItem(p => p.Id == id, !User.Identity.IsAuthenticated);
122+
var post = await _data.BlogPosts.GetItem(p => p.Id == id, !User.Identity.IsAuthenticated);
123+
if (format.ToUpper() == "HTML")
124+
{
125+
post.Description = Markdown.ToHtml(post.Description);
126+
post.Content = Markdown.ToHtml(post.Content);
127+
}
128+
return post;
99129
}
100130
else
101131
{

src/Core/CoreAPI.xml

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)