Skip to content

Instantly share code, notes, and snippets.

@poornimanayar
Last active February 3, 2022 15:40
Show Gist options
  • Save poornimanayar/f6784cdcfeedfea792ec2f640df375be to your computer and use it in GitHub Desktop.
Save poornimanayar/f6784cdcfeedfea792ec2f640df375be to your computer and use it in GitHub Desktop.
Embed Provider for Gist
using System.Collections.Generic;
using System.Dynamic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Media.EmbedProviders;
using Umbraco.Cms.Core.Serialization;
namespace Umbraco.TechCommunityDayDemo.EmbedProvider
{
public class GistEmbedProvider : OEmbedProviderBase
{
private readonly HttpClient _httpClient;
public GistEmbedProvider(IJsonSerializer jsonSerializer, HttpClient httpClient) : base(jsonSerializer)
{
_httpClient = httpClient;
}
public override string ApiEndpoint => "https://api.github.com/";
public override string[] UrlSchemeRegex => new string[]
{
@"gists/*"
};
public override Dictionary<string, string> RequestParams=> new Dictionary<string, string>();
public override string GetMarkup(string url, int maxWidth = 0, int maxHeight = 0)
{
var markup = "<div class=\"gist-markup\">{0}<div>";
//gists/f6784cdcfeedfea792ec2f640df375be
using (_httpClient)
{
_httpClient.DefaultRequestHeaders.Add("user-agent", "umbraco");
var gistResponse = _httpClient.GetFromJsonAsync<GistResponse>(ApiEndpoint+url).Result;
var gistResponseMarkUp = new StringBuilder();
if(gistResponse != null && gistResponse.Files!= null && gistResponse.Files.Keys.Count > 0)
{
foreach (var file in gistResponse.Files)
{
gistResponseMarkUp.Append($"<div class=\"gist-markup-heading\">{file.Key}</div>");
gistResponseMarkUp.Append($"<div class=\"gist-markup-content\">{file.Value.Content}</div>");
}
}
markup = string.Format(markup, gistResponseMarkUp.ToString());
}
return markup;
}
}
}
using System;
using System.Collections.Generic;
namespace Umbraco.TechCommunityDayDemo.EmbedProvider
{
public class GistResponse
{
public Dictionary<string, GistFile> Files { get; set; }
public Owner Owner { get; set; }
}
public class GistFile
{
public string Filename { get; set; }
public string Type { get; set; }
public string Language { get; set; }
public Uri RawUrl { get; set; }
public long Size { get; set; }
public bool Truncated { get; set; }
public string Content { get; set; }
}
public class Owner
{
public string Login { get; set; }
public long Id { get; set; }
public string NodeId { get; set; }
public Uri AvatarUrl { get; set; }
public string GravatarId { get; set; }
public Uri Url { get; set; }
public Uri HtmlUrl { get; set; }
public Uri FollowersUrl { get; set; }
public string FollowingUrl { get; set; }
public string GistsUrl { get; set; }
public string StarredUrl { get; set; }
public Uri SubscriptionsUrl { get; set; }
public Uri OrganizationsUrl { get; set; }
public Uri ReposUrl { get; set; }
public string EventsUrl { get; set; }
public Uri ReceivedEventsUrl { get; set; }
public string Type { get; set; }
public bool SiteAdmin { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment