-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9952ef6
commit 83eb1bc
Showing
15 changed files
with
274 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace TerribleDev.Blog.Web.Models | ||
{ | ||
public class PostViewModel | ||
{ | ||
public IPost Post { get; set; } | ||
public bool IsAmp { get; set; } = false; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Hosting; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace TerribleDev.Blog.Web.Taghelpers | ||
{ | ||
[HtmlTargetElement("inline-script")] | ||
public class InlineScriptTagHelper : TagHelper | ||
{ | ||
[HtmlAttributeName("src")] | ||
public string Src { get; set; } | ||
|
||
private IWebHostEnvironment HostingEnvironment { get; } | ||
private IMemoryCache Cache { get; } | ||
|
||
|
||
|
||
public InlineScriptTagHelper(IWebHostEnvironment hostingEnvironment, IMemoryCache cache) | ||
{ | ||
HostingEnvironment = hostingEnvironment; | ||
Cache = cache; | ||
} | ||
|
||
|
||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) | ||
{ | ||
var paths = Src.Split(','); | ||
|
||
// Get the value from the cache, or compute the value and add it to the cache | ||
var fileContent = await Cache.GetOrCreateAsync("InlineScriptTagHelper-" + paths, async entry => | ||
{ | ||
var fileProvider = HostingEnvironment.WebRootFileProvider; | ||
var result = paths.Select(async path => { | ||
if(HostingEnvironment.IsDevelopment()) | ||
{ | ||
var changeToken = fileProvider.Watch(path); | ||
entry.AddExpirationToken(changeToken); | ||
} | ||
entry.SetPriority(CacheItemPriority.NeverRemove); | ||
var file = fileProvider.GetFileInfo(path); | ||
if (file == null || !file.Exists) | ||
return null; | ||
return await ReadFileContent(file); | ||
}); | ||
var allFinished = await Task.WhenAll(result); | ||
return string.Join("\n", allFinished); | ||
}); | ||
|
||
if (fileContent == null) | ||
{ | ||
output.SuppressOutput(); | ||
return; | ||
} | ||
|
||
output.TagName = "script"; | ||
output.Attributes.RemoveAll("href"); | ||
output.Content.AppendHtml(fileContent); | ||
} | ||
|
||
private static async Task<string> ReadFileContent(IFileInfo file) | ||
{ | ||
using (var stream = file.CreateReadStream()) | ||
using (var textReader = new StreamReader(stream)) | ||
{ | ||
return await textReader.ReadToEndAsync(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,43 @@ | ||
@inject BlogConfiguration config | ||
@model IPost | ||
@model PostViewModel | ||
@{ | ||
ViewData["Title"] = @Model.Title; | ||
ViewData["Title"] = Model.Post.Title; | ||
ViewData["amp"] = Model.IsAmp; | ||
} | ||
|
||
<cache vary-by-route="postUrl"> | ||
@Html.DisplayForModel() | ||
@Html.DisplayFor(m => m.Post, "Post") | ||
</cache> | ||
|
||
@section Head { | ||
<meta name="description" content="@Model.Content.Value.SummaryPlainShort" /> | ||
<meta name="description" content="@Model.Post.Content.Value.SummaryPlainShort" /> | ||
<meta property="og:type" content="blog"> | ||
<meta property="og:title" content="@Model.Title"> | ||
<meta property="og:url" content="@Model.CanonicalUrl"> | ||
<meta property="og:title" content="@Model.Post.Title"> | ||
<meta property="og:url" content="@Model.Post.CanonicalUrl"> | ||
<meta property="og:site_name" content="@config.Title"> | ||
<meta property="og:description" content="@Model.Content.Value.SummaryPlainShort"> | ||
<meta property="og:updated_time" content="@Model.PublishDate.ToString("O")"> | ||
<meta property="og:description" content="@Model.Post.Content.Value.SummaryPlainShort"> | ||
<meta property="og:updated_time" content="@Model.Post.PublishDate.ToString("O")"> | ||
<meta name="twitter:card" content="summary"> | ||
<meta name="twitter:title" content="@Model.Title"> | ||
<meta name="twitter:description" content="@Model.Content.Value.SummaryPlainShort"> | ||
<meta name="twitter:title" content="@Model.Post.Title"> | ||
<meta name="twitter:description" content="@Model.Post.Content.Value.SummaryPlainShort"> | ||
<meta name="twitter:site" content="@@TerribleDev"> | ||
<meta name="twitter:creator" content="@@TerribleDev"> | ||
<link rel="canonical" href="@Model.CanonicalUrl" /> | ||
@foreach(var image in Model.Content.Value.Images.Take(6)) | ||
<link rel="canonical" href="@Model.Post.CanonicalUrl" /> | ||
<link rel="amphtml" href="@Model.Post.AMPUrl"> | ||
@foreach(var image in Model.Post.Content.Value.Images.Take(6)) | ||
{ | ||
<meta property="og:image" content="@image"> | ||
} | ||
@if(Model.Content.Value.Images.Count > 0) | ||
@if(Model.Post.Content.Value.Images.Count > 0) | ||
{ | ||
<meta name="twitter:image" content="@(Model.Content.Value.Images[0])"> | ||
<meta name="twitter:image" content="@(Model.Post.Content.Value.Images[0])"> | ||
} | ||
<meta property="og:image" content="https://www.gravatar.com/avatar/333e3cea32cd17ff2007d131df336061?s=640" /> | ||
<script type="application/ld+json"> | ||
@Html.Raw(Model.Content.Value.JsonLDString) | ||
@Html.Raw(Model.Post.Content.Value.JsonLDString) | ||
</script> | ||
<script type="application/ld+json"> | ||
@Html.Raw(Model.Content.Value.JsonLDBreadcrumbString) | ||
@Html.Raw(Model.Post.Content.Value.JsonLDBreadcrumbString) | ||
</script> | ||
} | ||
|
13 changes: 11 additions & 2 deletions
13
src/TerribleDev.Blog.Web/Views/Shared/DisplayTemplates/Post.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,36 @@ | ||
|
||
@{ | ||
Layout = null; | ||
var amp = ViewData["amp"] as bool? ?? false; | ||
} | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag() { dataLayer.push(arguments); } | ||
gtag('js', new Date()); | ||
gtag('config', 'UA-48128396-1'); | ||
document.addEventListener('DOMContentLoaded', function () { | ||
var script = document.createElement('script'); | ||
script.src = 'https://www.googletagmanager.com/gtag/js?id=UA-48128396-1'; | ||
script.async = true | ||
document.body.appendChild(script); | ||
}); | ||
</script> | ||
|
||
@if(amp) | ||
{ | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag() { dataLayer.push(arguments); } | ||
gtag('js', new Date()); | ||
gtag('config', 'UA-48128396-1'); | ||
document.addEventListener('DOMContentLoaded', function () { | ||
var script = document.createElement('script'); | ||
script.src = 'https://www.googletagmanager.com/gtag/js?id=UA-48128396-1'; | ||
script.async = true | ||
document.body.appendChild(script); | ||
}); | ||
</script> | ||
} | ||
else | ||
{ | ||
<amp-analytics type="gtag" data-credentials="include"> | ||
<script type="application/json"> | ||
{ | ||
"vars" : { | ||
"gtag_id": "UA-48128396-1", | ||
"config" : { | ||
"UA-48128396-1: { "groups": "default" } | ||
} | ||
} | ||
} | ||
</script> | ||
</amp-analytics> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.