Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jun 17, 2013 @ 14:57
    Bjarne Fyrstenborg
    0

    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.

    <xsl:variable name="url" select="concat('http://',umbraco.library:RequestServerVariables('HTTP_HOST'))" />
    <xsl:variable name="topImg" select="$currentPage/topImage" />
    
    <xsl:if test="normalize-space($topImg)">
      <meta property="og:image" content="{$url}{umbraco.library:GetMedia($topImg, 0)/umbracoFile}" />
    </xsl:if>
    

    /Bjarne

  • Mads Jørgensen 74 posts 226 karma points
    Jun 17, 2013 @ 15:08
    Mads Jørgensen
    0

    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.

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jun 17, 2013 @ 15:31
    Bjarne Fyrstenborg
    1

    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..

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 17, 2013 @ 16:03
    Jeroen Breuer
    2

    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.

    Jeroen

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 17, 2013 @ 16:22
    Chriztian Steinmeier
    0

    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 

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jun 17, 2013 @ 19:05
    Bjarne Fyrstenborg
    0

    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

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 17, 2013 @ 19:33
    Chriztian Steinmeier
    100

    Hi Bjarne,

    You could do something like this to grab the src attribute of the first <img> element in the bodyText property:

    <xsl:variable name="firstImagePath" select="ucom:ParseXhtml($currentPage/bodyText)//img[1]/@src" />

    /Chriztian

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jun 17, 2013 @ 20:43
    Bjarne Fyrstenborg
    0

    Hi Chriztian

    Thanks, that was what I was looking for :)

    I think that is the easiest way to solve this..

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft