absolute links in wiziwig content

Nando's Avatar

Nando

05 May, 2016 08:40 PM

We have a few pages in the CMS that are used for emails and/or inside other software so unless the user inputs absolute links, most links are not absolute and therefore broken. I tackled that issue today :)

I did some crazy stuff but it works...

#set ($content = $_SerializerTool.serialize($data.getChild("content"), true))
#set ($content2 = $content.replaceAll('<', 'ltsign123').replaceAll('>', 'gtsign123').replaceAll(',', 'comma123').replaceAll('href="', 'linkstart123'))
#set ($linksets = $content2.split("ltsign123a "))
#set ($linked = [])  
#foreach ($linkset in $linksets) 
#if($linkset.contains("[.]") || $linkset.contains(";") || $linkset.contains(".html") || $linkset.contains(".pdf") || $linkset.contains(".doc"))
#set ($addls = $linked.add($linkset))
#else
#set ($linkset1 = $linkset.replaceFirst('"','.html"'))
#set ($addls = $linked.add($linkset1))
#end
#end
#set ($linked1 = $linked.toString())
#set ($linked2 = $linked1.replaceAll(',','').replace("[","").replace("]",""))
#set ($linked3 = $linked2.replaceAll('site://Secure Site/','https://www.nova.edu/'))
#set ($linked4 = $linked3.replaceAll('ltsign123','<').replaceAll('gtsign123','>').replaceAll('comma123',',').replaceAll('linkstart123','<a href="').replaceAll('site://$currentPageSiteName/',$cpath))
$linked4

$spath is set in an import that checks the current site name and sets the url...

the if contains line messes me up, if I don't specify .html, .doc, .pdf SOMETIMES it replaces on lines that shouldn't... weird

when you get a chance can you let me know what you think? any recommendations to clean up the code.

thanks in advance!

  1. 1 Posted by Nando on 09 May, 2016 01:24 PM

    Nando's Avatar

    there was an issue with the code above, this code works but we a specific issue I can't seem to resolve:

    #set ($content = $_SerializerTool.serialize($data.getChild("content"), true))
    #set ($content2 = $content.replaceAll('<', 'ltsign123').replaceAll('>', 'gtsign123').replaceAll(',', 'comma123').replaceAll('href="', 'linkstart123'))
    #set ($linksets = $content2.split("ltsign123a "))
    #set ($linked = [])  
    #foreach ($linkset in $linksets) 
    #if($linkset.contains("[.]") || $linkset.contains(";") || $linkset.contains(".html") || $linkset.contains(".pdf") || $linkset.contains(".doc"))
    #set ($addls = $linked.add($linkset))
    #else
    #set ($linkset1 = $linkset.replaceFirst('"','.html"'))
    #set ($addls = $linked.add($linkset1))
    #end
    #end
    ##${linked.size()}
    #set ($linked1 = $linked.toString())
    #set ($linked2 = $linked1.replaceAll(',','').replace("[","").replace("]","").replaceAll('site://','').replaceAll('Secure Site', $cpath))
    #set ($linked3 = $linked2.replaceAll('edu//', 'edu/').replaceAll('ltsign123','<').replaceAll('gtsign123','>').replaceAll('comma123',',').replaceAll('linkstart123','<a href="'))##
    $linked3
    

    when setting $linked2 I'd like to use $currentPageSiteName instead of manually setting the site name but every time I use $currentPageSiteName nothing gets replaced...

    thoughts?

  2. 2 Posted by Ryan Griffith on 09 May, 2016 02:22 PM

    Ryan Griffith's Avatar

    Hi Nando,

    Curious, but I'm not seeing $cpath being initialized anywhere. If you adjust that does using $currentPageSiteName work?

    Thanks!

  3. 3 Posted by Nando on 09 May, 2016 02:26 PM

    Nando's Avatar

    $cpath is initialized in a import above this code (not shown - sorry)

    it populates fine and so does $currentPageSiteName - it just doesn't see to find the end result of $currentPageSiteName to replace...

  4. 4 Posted by Ryan Griffith on 09 May, 2016 02:56 PM

    Ryan Griffith's Avatar

    Got it, thanks Nando.

    I believe I may have ran into a similar issue in the past where I was not able to use a variable within the first argument in the replaceAll(). Perhaps try something like:

    #set ($linked2 = $linked1.replaceAll(',','').replace("[","").replace("]","").replaceAll('site://','').replaceAll("${currentPageSiteName}", $cpath))
    

    Let me know if this adjustment helps at all.

    Thanks!

  5. 5 Posted by Nando on 09 May, 2016 03:03 PM

    Nando's Avatar

    no go :(

    An error occurred while rendering asset preview: Invocation of method 'replaceAll' in  class java.lang.String threw exception java.util.regex.PatternSyntaxException: Illegal repetition near index 0
    ${currentPageSiteName}
    ^ at templateValidation[line 20, column 103]
    

    I also tried:

    replaceAll(${currentPageSiteName}, $cpath) 


    An error occurred while rendering asset preview: Invocation of method 'replaceAll' in class java.lang.String threw exception java.lang.NullPointerException at templateValidation[line 20, column 103]
  6. 6 Posted by Ryan Griffith on 09 May, 2016 03:28 PM

    Ryan Griffith's Avatar

    What about replaceAll($currentPageSiteName, $cpath)?

  7. 7 Posted by Nando on 09 May, 2016 03:31 PM

    Nando's Avatar

    nope :(

    An error occurred while rendering asset preview: Invocation of method 'replaceAll' in class java.lang.String threw exception java.lang.NullPointerException at templateValidation[line 20, column 103]

  8. 8 Posted by Ryan Griffith on 09 May, 2016 05:38 PM

    Ryan Griffith's Avatar

    Thank you for trying, Nando.

    I believe the issue here is that $currentPageSiteName is a variable that is registered at run time as opposed to something like a static string. So when the replaceAll is called the variable is null, which explains the NullPointerException.

    Let me keep playing with this a bit to see if there's something else we can do.

    Please let me know if you have any questions.

    Thanks!

  9. 9 Posted by Ryan Griffith on 09 May, 2016 06:03 PM

    Ryan Griffith's Avatar

    Hi Nando,

    I believe I was able to come up with a possible solution using the String Tool. I've created a macro which basically does a "string before" and "string after" replacement. Here is the code:

    #macro (replaceCurrentPageSiteName $string, $what, $with)
    $_StringTool.substringBefore($string, $what)$with$_StringTool.substringAfter($string, $what)
    #end
    

    Using your Format as an example, you'd call this macro like:

    #set ($linked2 = $linked1.replaceAll(',','').replace("[","").replace("]","").replaceAll('site://',''))
    #set ($linked2 = "#replaceCurrentPageSiteName($linked2, $currentPageSiteName, $clink)")
    ## Do things with $linked2
    

    Please let me know if you have any questions.

    Thanks!

  10. 10 Posted by Nando on 09 May, 2016 06:20 PM

    Nando's Avatar

    NICE!! that worked great!

    few questions:

    when setting variables, using the same name is ok? just overwritten?

    the if contains line still messes me up, if I don't specify .html, .doc, .pdf SOMETIMES it replaces on lines that shouldn't... thoughts?

    in general, what are your thoughts on this for outputting absolute links when publishing but still using system links inside?

    thanks again!!!

  11. 11 Posted by Ryan Griffith on 09 May, 2016 07:36 PM

    Ryan Griffith's Avatar

    Thank you for following up, Nando. I am glad to hear the proposed solution did the trick.

    when setting variables, using the same name is ok? just overwritten?

    Correct, the variable will be overwritten.

    the if contains line still messes me up, if I don't specify .html, .doc, .pdf SOMETIMES it replaces on lines that shouldn't... thoughts?

    I'm having a tough time following the code without some context. Can you explain this piece a bit more?

    in general, what are your thoughts on this for outputting absolute links when publishing but still using system links inside?

    Generally, clients will append a static domain or use the Site name (which represents the URL) when building out the links, so this is not too far fetched. Using a combination of [system-view:external] and [system-view:internal] to generate different links for preview and publish would be recommended so you can still link to pages within Cascade.

    Please let me know if you have any questions.

    Thanks!

  12. 12 Posted by Nando on 09 May, 2016 08:28 PM

    Nando's Avatar

    Gotcha.

    actually, your previous fix gave me an idea, is there way for me to check if a variable contains a specific character BEFORE a specific character?

    for example, I would like to check if the variable has a . or a ; before the first "

  13. 13 Posted by Nando on 09 May, 2016 08:57 PM

    Nando's Avatar

    trying something like:

    #foreach ($linkset in $linksets) 
    #set($quotechecker = $_StringTool.substringBefore($linkset,'"'))
    #if ($quotechecker.contains("[.]") || $quotechecker.contains(";"))
    

    but it gets stuck because its breaking elements I guess...

  14. 14 Posted by Ryan Griffith on 09 May, 2016 09:01 PM

    Ryan Griffith's Avatar

    Hm, are you seeing any errors, or is it just not getting into that conditional? Curious, but do you need [.], or can it just be .? The former looks more like you're trying to perform a regular expression match, which contains() does not do.

    Please let me know if you have any questions.

    Thanks!

  15. 15 Posted by Nando on 11 May, 2016 01:12 PM

    Nando's Avatar

    You were right about [.] :)

    The big issue I was having here is when I would split the one value into multiple values I would get a "element must be terminated by the matching end-tag" error. Which I get bc the split values were a mix of content missing start/end elements BUT it would've all worked out in the end... no?

    to get around that, I replaced all <, >, a href, etc so that I can split them without issue... this is my latest code:

    #import('site://Nova/_cascade/formats/bootstrap/default/css-js/default canonical')
    ##
    #macro (replaceCurrentPageSiteName $string, $what, $with)
    $_StringTool.substringBefore($string, $what)$with$_StringTool.substringAfter($string, $what)
    #end
    #macro (absolute $links)
    #set ($content = $content.replaceAll('<','ltsign123').replaceAll('>','gtsign123').replaceAll(',','comma123').replaceAll('href="','linkstart123').replaceAll('style="','style123'))
    #set ($content = $content.replaceAll('"','quotes123'))
    #set ($linksets = $content.split("ltsign123a "))
    #set ($linked = [])  
    #foreach ($linkset in $linksets) 
    #set($quotechecker = $_StringTool.substringBefore($linkset,'quotes123'))
    ##$quotechecker
    #if ($quotechecker.contains(".") || $quotechecker.contains(";"))
    ##if($linkset.contains("[.]") || $linkset.contains(";") || $linkset.contains(".html") || $linkset.contains(".pdf") || $linkset.contains(".doc"))
    #set ($addls = $linked.add($linkset))
    #else
    #set ($linkset = $linkset.replaceFirst('quotes123','.htmlquotes123'))
    #set ($addls = $linked.add($linkset))
    #end
    #end
    ##${linked.size()}
    #set ($linked = $linked.toString())
    #set ($linked = $linked.replaceAll(",","").replace("[","").replace("]",""))
    #set ($site = "site://" + $currentPageSiteName)
    #if ($linked.contains("site://"))
    #set ($linked = "#replaceCurrentPageSiteName($linked, $site, $cpath)")
    #end
    #set ($linked = $linked.replaceAll('edu//', 'edu/').replaceAll('ltsign123','<').replaceAll('gtsign123','>').replaceAll('comma123',',').replaceAll('linkstart123','<a href="'))
    #set ($linked = $linked.replaceAll('style123','style="').replaceAll('quotes123','"'))##
    $linked
    #end
    
    ##
    #if ($data.getChild("content").value != "" || $data.getChild("content").getChildren())
    #set ($content = $_SerializerTool.serialize($data.getChild("content"), true))
    #absolute($links)
    #end
    
  16. Nando closed this discussion on 16 May, 2016 12:40 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM