Skip to content

Commit 83eb1bc

Browse files
committed
amp
1 parent 9952ef6 commit 83eb1bc

File tree

15 files changed

+274
-80
lines changed

15 files changed

+274
-80
lines changed

src/TerribleDev.Blog.Web/Controllers/HomeController.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,19 @@ public IActionResult Post(string postUrl)
6666
this.StatusCode(404);
6767
return View(nameof(FourOhFour));
6868
}
69-
return View(model: currentPost);
69+
return View("Post", model: new PostViewModel() { Post = currentPost, IsAmp = false });
70+
}
71+
[Route("{postUrl}/amp")]
72+
[OutputCache(Duration = 31536000, VaryByParam = "postUrl")]
73+
[ResponseCache(Duration = 900)]
74+
public IActionResult PostAmp(string postUrl)
75+
{
76+
if(!postCache.UrlToPost.TryGetValue(postUrl, out var currentPost))
77+
{
78+
this.StatusCode(404);
79+
return View(nameof(FourOhFour));
80+
}
81+
return View("Post", model: new PostViewModel() { Post = currentPost, IsAmp = true });
7082
}
7183
[Route("/Error")]
7284
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

src/TerribleDev.Blog.Web/Controllers/SeoController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void SiteMap()
6565
Urls = postCache.PostsAsLists.Select(a => new SiteMapItem() { LastModified = DateTime.UtcNow, Location = a.CanonicalUrl }).ToList()
6666
};
6767
sitemap.Urls.AddRange(sitewideLinks);
68+
sitemap.Urls.AddRange(postCache.PostsAsLists.Select(a => new SiteMapItem() { LastModified = DateTime.UtcNow, Location = a.AMPUrl }).ToList());
6869
ser.Serialize(this.Response.Body, sitemap);
6970
}
7071
}

src/TerribleDev.Blog.Web/Factories/BlogFactory.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Diagnostics;
1414
using System.Collections.Concurrent;
1515
using Schema.NET;
16+
using System.Text.RegularExpressions;
1617

1718
namespace TerribleDev.Blog.Web
1819
{
@@ -69,6 +70,7 @@ public IPost ParsePost(string postText, string fileName, string domain)
6970
var postSettings = ParseYaml(ymlRaw);
7071
var resolvedUrl = !string.IsNullOrWhiteSpace(postSettings.permalink) ? postSettings.permalink : fileName.Split('.')[0].Replace(' ', '-').WithoutSpecialCharacters();
7172
var canonicalUrl = $"https://blog.terrible.dev/{resolvedUrl}/";
73+
var ampUrl = $"https://blog.terrible.dev/{resolvedUrl}/amp/";
7274
return new Post()
7375
{
7476
PublishDate = postSettings.date.ToUniversalTime(),
@@ -77,6 +79,7 @@ public IPost ParsePost(string postText, string fileName, string domain)
7779
Title = postSettings.title,
7880
RelativeUrl = $"/{resolvedUrl}/",
7981
CanonicalUrl = canonicalUrl,
82+
AMPUrl = ampUrl,
8083
UrlWithoutPath = resolvedUrl,
8184
Content = new Lazy<IPostContent>(() =>
8285
{
@@ -107,8 +110,12 @@ public IPost ParsePost(string postText, string fileName, string domain)
107110
},
108111
},
109112
};
113+
// regex remove picture and source tags
114+
var regex = new Regex(@"<source[^>]*>|</source>|<picture[^>]*>|</picture>", RegexOptions.IgnoreCase);
115+
var ampContent = regex.Replace(postContent, "");
110116
return new PostContent()
111117
{
118+
AmpContent = new HtmlString(ampContent),
112119
Content = new HtmlString(postContent),
113120
Images = postImages,
114121
ContentPlain = postContentPlain,

src/TerribleDev.Blog.Web/Models/IPost.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace TerribleDev.Blog.Web.Models
99
{
1010
public interface IPost
1111
{
12+
string AMPUrl { get; set; }
1213
string CanonicalUrl { get; set; }
1314
string UrlWithoutPath { get; set; }
1415
string RelativeUrl { get; set; }

src/TerribleDev.Blog.Web/Models/IPostContent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace TerribleDev.Blog.Web.Models
77
{
88
public interface IPostContent
99
{
10+
public HtmlString AmpContent { get; set; }
1011
HtmlString Content { get; set; }
1112
HtmlString Summary { get; set; }
1213
string ContentPlain { get; set; }

src/TerribleDev.Blog.Web/Models/Post.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace TerribleDev.Blog.Web.Models
99
[DebuggerDisplay("{Title}")]
1010
public class Post : IPost
1111
{
12+
public string AMPUrl { get; set; }
1213
public string CanonicalUrl { get; set; }
1314
public string UrlWithoutPath { get; set; }
1415
public string RelativeUrl { get; set; }

src/TerribleDev.Blog.Web/Models/PostContent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace TerribleDev.Blog.Web.Models
88

99
public class PostContent : IPostContent
1010
{
11+
public HtmlString AmpContent { get; set; }
1112
public HtmlString Content { get; set; }
1213
public HtmlString Summary { get; set; }
1314
public string ContentPlain { get; set; }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace TerribleDev.Blog.Web.Models
2+
{
3+
public class PostViewModel
4+
{
5+
public IPost Post { get; set; }
6+
public bool IsAmp { get; set; } = false;
7+
8+
}
9+
}

src/TerribleDev.Blog.Web/Taghelpers/InlineCss.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using Microsoft.Extensions.Caching.Memory;
55
using Microsoft.Extensions.FileProviders;
66
using Microsoft.Extensions.Hosting;
7+
using System.Collections.Generic;
78
using System.IO;
9+
using System.Linq;
810
using System.Threading.Tasks;
911

1012
namespace TerribleDev.Blog.Web.Taghelpers
@@ -29,25 +31,29 @@ public InlineStyleTagHelper(IWebHostEnvironment hostingEnvironment, IMemoryCache
2931

3032
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
3133
{
32-
var path = Href;
34+
var paths = Href.Split(',');
3335

3436
// Get the value from the cache, or compute the value and add it to the cache
35-
var fileContent = await Cache.GetOrCreateAsync("InlineStyleTagHelper-" + path, async entry =>
37+
var fileContent = await Cache.GetOrCreateAsync("InlineStyleTagHelper-" + paths, async entry =>
3638
{
3739
var fileProvider = HostingEnvironment.WebRootFileProvider;
38-
if(HostingEnvironment.IsDevelopment())
39-
{
40-
var changeToken = fileProvider.Watch(path);
41-
entry.AddExpirationToken(changeToken);
42-
}
40+
var result = paths.Select(async path => {
41+
if(HostingEnvironment.IsDevelopment())
42+
{
43+
var changeToken = fileProvider.Watch(path);
44+
entry.AddExpirationToken(changeToken);
45+
}
4346

44-
entry.SetPriority(CacheItemPriority.NeverRemove);
47+
entry.SetPriority(CacheItemPriority.NeverRemove);
4548

46-
var file = fileProvider.GetFileInfo(path);
47-
if (file == null || !file.Exists)
48-
return null;
49+
var file = fileProvider.GetFileInfo(path);
50+
if (file == null || !file.Exists)
51+
return null;
4952

50-
return await ReadFileContent(file);
53+
return await ReadFileContent(file);
54+
});
55+
var allFinished = await Task.WhenAll(result);
56+
return string.Join("\n", allFinished);
5157
});
5258

5359
if (fileContent == null)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Razor.TagHelpers;
4+
using Microsoft.Extensions.Caching.Memory;
5+
using Microsoft.Extensions.FileProviders;
6+
using Microsoft.Extensions.Hosting;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
namespace TerribleDev.Blog.Web.Taghelpers
13+
{
14+
[HtmlTargetElement("inline-script")]
15+
public class InlineScriptTagHelper : TagHelper
16+
{
17+
[HtmlAttributeName("src")]
18+
public string Src { get; set; }
19+
20+
private IWebHostEnvironment HostingEnvironment { get; }
21+
private IMemoryCache Cache { get; }
22+
23+
24+
25+
public InlineScriptTagHelper(IWebHostEnvironment hostingEnvironment, IMemoryCache cache)
26+
{
27+
HostingEnvironment = hostingEnvironment;
28+
Cache = cache;
29+
}
30+
31+
32+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
33+
{
34+
var paths = Src.Split(',');
35+
36+
// Get the value from the cache, or compute the value and add it to the cache
37+
var fileContent = await Cache.GetOrCreateAsync("InlineScriptTagHelper-" + paths, async entry =>
38+
{
39+
var fileProvider = HostingEnvironment.WebRootFileProvider;
40+
var result = paths.Select(async path => {
41+
if(HostingEnvironment.IsDevelopment())
42+
{
43+
var changeToken = fileProvider.Watch(path);
44+
entry.AddExpirationToken(changeToken);
45+
}
46+
47+
entry.SetPriority(CacheItemPriority.NeverRemove);
48+
49+
var file = fileProvider.GetFileInfo(path);
50+
if (file == null || !file.Exists)
51+
return null;
52+
53+
return await ReadFileContent(file);
54+
});
55+
var allFinished = await Task.WhenAll(result);
56+
return string.Join("\n", allFinished);
57+
});
58+
59+
if (fileContent == null)
60+
{
61+
output.SuppressOutput();
62+
return;
63+
}
64+
65+
output.TagName = "script";
66+
output.Attributes.RemoveAll("href");
67+
output.Content.AppendHtml(fileContent);
68+
}
69+
70+
private static async Task<string> ReadFileContent(IFileInfo file)
71+
{
72+
using (var stream = file.CreateReadStream())
73+
using (var textReader = new StreamReader(stream))
74+
{
75+
return await textReader.ReadToEndAsync();
76+
}
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)