Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Last active August 29, 2015 14:21
Show Gist options
  • Save aloisdg/0d9c84b8d9990fc001fd to your computer and use it in GitHub Desktop.
Save aloisdg/0d9c84b8d9990fc001fd to your computer and use it in GitHub Desktop.
Quick snippet to convert a plain url to markdown
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
public class Program
{
public static void Main()
{
var s = @"https://dotnetfiddle.net/pMMcEN";
foreach (var item in s.Split(new []{'\r', '\n'}))
{
Console.WriteLine(ToMarkdown(item));
}
// output :
// - [Home | .NET Fiddle](https://dotnetfiddle.net/pMMcEN)
}
public static string ToMarkdown(string url)
{
const string titleRegex = @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>";
if (url.StartsWith("http"))
{
var source = new WebClient().DownloadString(url);
var encodeTitle = Regex
.Match(source, titleRegex, RegexOptions.IgnoreCase)
.Groups["Title"].Value;
var decodeTitle = Encoding.UTF8.GetString(
Encoding.Default.GetBytes(
WebUtility.HtmlDecode(encodeTitle)));
return String.Format("- [{0}]({1})", decodeTitle, url);
}
else
return url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment