Created
July 11, 2012 19:50
-
-
Save sniffdk/3092833 to your computer and use it in GitHub Desktop.
Umbraco - TinyMce helpers for parsing localmedia to real links
This file contains hidden or 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
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