Can I get the url of images from in RichText editor (WYSIWYG) or just the first image?
I'm using open graph meta tags to specify the image, here a topImage .. but not all pages have a topImage and here I would like to use a image from the bodyText if one exists.
You could try to use an XSLT extension. Here is some code which get's all the images from the RTE and adds an absolute path to them:
/// <summary>
/// Add an absolute path to all the img tags in the html of an e-mail.
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public string AddImgAbsolutePath(string html)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var uri = new Uri(HttpContext.Request.Url.AbsoluteUri);
var domainUrl = string.Format("{0}://{1}", uri.Scheme, uri.Authority);
if (doc.DocumentNode.SelectNodes("//img[@src]") != null)
{
foreach (HtmlNode img in doc.DocumentNode.SelectNodes("//img[@src]"))
{
HtmlAttribute att = img.Attributes["src"];
if (att.Value.StartsWith("/"))
{
att.Value = domainUrl + att.Value;
}
}
}
return doc.DocumentNode.InnerHtml;
}
With this code you can also get all images or the first one. It's using Html Agility Pack.
Get image url from RichText editor
Hi..
Can I get the url of images from in RichText editor (WYSIWYG) or just the first image?
I'm using open graph meta tags to specify the image, here a topImage .. but not all pages have a topImage and here I would like to use a image from the bodyText if one exists.
/Bjarne
As far as I'm concerned it's rather difficult, since it's all wrapped in some CDATA, and therefore you cannot traverse it through your XSLT :-(
If it's really a necessity, you should create an extension method for that purpose.
I was thinking I perhaps could use Chriztians WYSIWYG helper to do this: https://gist.github.com/greystate/1171897
But if it can't be done or is too complicated it might not be worth the time spent on it..
You could try to use an XSLT extension. Here is some code which get's all the images from the RTE and adds an absolute path to them:
With this code you can also get all images or the first one. It's using Html Agility Pack.
Jeroen
Hi Bjarne - you could totally use the WYSIWYG helper for that - but Jeroen's extension would be equally good - depends as always :-)
Once you've experienced the power of processing RichText content with XSLT you're going to want to do it in many more cases !
/Chriztian
Hi Jeroen and Chriztian..
Thanks for the suggestions..
@Chriztian how can I use WYSIWYG helper to get e.g. first image to a variable?
I have been looking at these examples: http://pimpmyxslt.com/articles/wysiwyg/ .. where it replaces the html markup, with something more clean markup..
/Bjarne
Hi Bjarne,
You could do something like this to grab the src attribute of the first <img> element in the bodyText property:
/Chriztian
Hi Chriztian
Thanks, that was what I was looking for :)
I think that is the easiest way to solve this..
/Bjarne
is working on a reply...