11using Core . Data ;
22using Core . Helpers ;
33using Core . Services ;
4+ using Markdig ;
45using Microsoft . AspNetCore . Authorization ;
56using Microsoft . AspNetCore . Http ;
67using 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 {
0 commit comments