Skip to content

Instantly share code, notes, and snippets.

@sniffdk
Created July 11, 2012 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sniffdk/3092833 to your computer and use it in GitHub Desktop.
Save sniffdk/3092833 to your computer and use it in GitHub Desktop.
Umbraco - TinyMce helpers for parsing localmedia to real links
public static HtmlString ParseTemplates(string html)
{
if (string.IsNullOrWhiteSpace(html)) return new HtmlString(html);
var doc = new HtmlDocument();
doc.LoadHtml(html);
var root = doc.DocumentNode;
if (root != null)
{
var replace = false;
var links = root.SelectNodes("//a");
if (links != null)
{
foreach (var link in links)
{
var href = HttpUtility.HtmlDecode(link.GetAttributeValue("href", "") ?? "");
var matchLocalMedia = StaticRegexes.LocalMediaRegex.Match(href);
if (matchLocalMedia.Success)
{
int id;
if (int.TryParse(matchLocalMedia.Groups[1].Value, out id))
{
var node = LuceneNode.Load(id);
if (node != null)
{
var media = new MediaItem(node);
link.SetAttributeValue("href", media.Path);
replace = true;
}
}
}
}
}
if (replace)
{
html = root.OuterHtml;
}
}
return new HtmlString(html);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment