diff --git a/force-app/main/default/classes/ConnectApiHelper.cls b/force-app/main/default/classes/ConnectApiHelper.cls index 81eb5d9..c2f94a7 100644 --- a/force-app/main/default/classes/ConnectApiHelper.cls +++ b/force-app/main/default/classes/ConnectApiHelper.cls @@ -56,7 +56,8 @@ global class ConnectApiHelper { 'p' => ConnectApi.MarkupType.Paragraph, 's' => ConnectApi.MarkupType.Strikethrough, 'u' => ConnectApi.MarkupType.Underline, - 'ul' => ConnectApi.MarkupType.UnorderedList + 'ul' => ConnectApi.MarkupType.UnorderedList, + 'a href=' => ConnectApi.MarkupType.Hyperlink }; /** @@ -144,14 +145,16 @@ global class ConnectApiHelper { List messageSegmentInputs = new List(); Integer strPos = 0; // The pattern for matching mentions, markup begin/end tags, and inline images. - // The first group matches a 15 or 18 character ID surrounded by {}: + // The first and second group matches a 15 or 18 character ID surrounded by {}: // (\\{[a-zA-Z0-9]{15}\\}|\\{[a-zA-Z0-9]{18}\\}) - // The second/third groups match beginning/ending HTML tags: (<[a-zA-Z]*>)|() - // The fourth group matches a 15 or 18 character content document ID preceded by "img:", + // The thrid group match hyperlink tags with the url and alt text and end tag + // (()|()) + // The fourth/fifth groups match the other beginning/ending HTML tags and text: (<[a-zA-Z]*>)|() + // The sixth group matches a 15 or 18 character content document ID preceded by "img:", // optionally followed by a string (not containing '}'), and surrounded by {}: // (\\{img:(069[a-zA-Z0-9]{12,15})(:[\\s\\S]*?)?\\}) - // The fifth group matches a 15 or 18 character record ID preceded by "record:" ex:{record:01t3E000002GCm9QAG} - Pattern globalPattern = Pattern.compile('(\\{[a-zA-Z0-9]{15}\\}|\\{[a-zA-Z0-9]{18}\\})|(<[a-zA-Z]*>)|()|(\\{img:(069[a-zA-Z0-9]{12,15})(:[\\s\\S]*?)?\\})|(\\{record:([a-zA-Z0-9]){15,18}(:[\\s\\S]*?)?\\})'); + // The seventh group matches a 15 or 18 character record ID preceded by "record:" ex:{record:01t3E000002GCm9QAG} + Pattern globalPattern = Pattern.compile('(\\{[a-zA-Z0-9]{15}\\}|\\{[a-zA-Z0-9]{18}\\})|(<[aA]\\s+[hH][rR][eE][fF]=")(.*?)">(.*?)|(<[a-zA-Z]*>)|()|(\\{img:(069[a-zA-Z0-9]{12,15})(:[\\s\\S]*?)?\\})|(\\{record:([a-zA-Z0-9]){15,18}(:[\\s\\S]*?)?\\})'); Matcher globalMatcher = globalPattern.matcher(inputText); @@ -197,12 +200,24 @@ global class ConnectApiHelper { // Add a segment for any accumulated text (which includes unsupported HTML tags). addTextSegment(messageSegmentInputs, textSegment); - ConnectApi.MarkupBeginSegmentInput markupBeginSegmentInput = makeMarkupBeginSegmentInput(tag); + ConnectApi.MarkupBeginSegmentInput markupBeginSegmentInput = makeMarkupBeginSegmentInput(tag, null, null); messageSegmentInputs.add(markupBeginSegmentInput); strPos = globalMatcher.end(); + } else if (tag.toLowerCase().contains('href=')){ + // Add a segment for any accumulated text (which includes unsupported HTML tags). + addTextSegment(messageSegmentInputs, textSegment); + //hyperlink tag - get the URL. Assumes format of altText + String url = matchingText.substring(matchingText.indexOf('"') + 1, matchingText.indexOf('>') - 1); + String htmlTag = matchingText.substring(1, matchingText.indexOf('"')); + String altText = matchingText.substring(matchingText.indexOf('>') + 1, matchingText.indexOf('. String tag = matchingText.substring(2, matchingText.indexOf('>')); if (supportedMarkup.containsKey(tag.toLowerCase())) { @@ -258,9 +273,13 @@ global class ConnectApiHelper { * Create a MarkupBeginSegmentInput corresponding to the tag. Checking whether the tag is * supported markup should happen before calling this method. */ - private static ConnectApi.MarkupBeginSegmentInput makeMarkupBeginSegmentInput(String tag) { + private static ConnectApi.MarkupBeginSegmentInput makeMarkupBeginSegmentInput(String tag, String url, String altText) { ConnectApi.MarkupBeginSegmentInput markupBeginSegment = new ConnectApi.MarkupBeginSegmentInput(); markupBeginSegment.markupType = supportedMarkup.get(tag.toLowerCase()); + if(!String.isEmpty(url)){ + markupBeginSegment.url = url; + markupBeginSegment.altText = altText; + } return markupBeginSegment; } diff --git a/force-app/main/default/classes/ConnectApiHelper.cls-meta.xml b/force-app/main/default/classes/ConnectApiHelper.cls-meta.xml index 252fbfd..8e4d11f 100755 --- a/force-app/main/default/classes/ConnectApiHelper.cls-meta.xml +++ b/force-app/main/default/classes/ConnectApiHelper.cls-meta.xml @@ -1,5 +1,5 @@ - 47.0 + 49.0 Active diff --git a/force-app/main/default/classes/ConnectApiHelperTest.cls b/force-app/main/default/classes/ConnectApiHelperTest.cls index 9b0a784..8510a7e 100644 --- a/force-app/main/default/classes/ConnectApiHelperTest.cls +++ b/force-app/main/default/classes/ConnectApiHelperTest.cls @@ -147,6 +147,20 @@ public class ConnectApiHelperTest { System.assertEquals(mentionId2, mentionSegment2.id); } + @IsTest(SeeAllData=true) + static void testHyperlinkPost() { + String mentionId = '005x0000000URNPzzz'; + String mentionId2 = '0F9x00000000D7m'; + String text = 'Testing Hyperlink'; + List segments = ConnectApiHelper.getMessageSegmentInputs(text); + + System.assertEquals(3, segments.size()); + System.assert(segments.get(0) instanceof ConnectApi.MarkupBeginSegmentInput); + System.assert(segments.get(1) instanceof ConnectApi.TextSegmentInput); + System.assert(segments.get(2) instanceof ConnectApi.MarkupEndSegmentInput); + + } + @IsTest(SeeAllData=true) static void testLinkAndHashtagParsing() { // The test string is: #Yolo: http://salesforce.com, {005} {005x0000000URNPzzz} test. @@ -384,7 +398,7 @@ public class ConnectApiHelperTest { // 2 = text2 // 3 = markup end // 4 = text3 - String text1 = 'a,

b
'; + String text1 = 'a,

b
'; String text2 = 'Does this work?'; String text3 = '

'; String text = text1 + '' + text2 + '' + text3; @@ -440,10 +454,10 @@ public class ConnectApiHelperTest { String text1 = 'foo'; String text2 = 'bar'; String text3 = 'baz'; - String text = '' + text1 + '' + text2 + '
  1. ' + text3 + '
'; + String text = '' + text1 + '' + text2 + '
  1. ' + text3 + '
' + '' + text3 + ''; List segments = ConnectApiHelper.getMessageSegmentInputs(text); - System.assertEquals(11, segments.size()); + System.assertEquals(14, segments.size()); System.assert(segments.get(0) instanceof ConnectApi.MarkupBeginSegmentInput); System.assert(segments.get(1) instanceof ConnectApi.TextSegmentInput); System.assert(segments.get(2) instanceof ConnectApi.MarkupEndSegmentInput); @@ -455,6 +469,9 @@ public class ConnectApiHelperTest { System.assert(segments.get(8) instanceof ConnectApi.TextSegmentInput); System.assert(segments.get(9) instanceof ConnectApi.MarkupEndSegmentInput); System.assert(segments.get(10) instanceof ConnectApi.MarkupEndSegmentInput); + System.assert(segments.get(11) instanceof ConnectApi.MarkupBeginSegmentInput); + System.assert(segments.get(12) instanceof ConnectApi.TextSegmentInput); + System.assert(segments.get(13) instanceof ConnectApi.MarkupEndSegmentInput); ConnectApi.MarkupBeginSegmentInput markupBeginSegment = (ConnectApi.MarkupBeginSegmentInput) segments.get(0); System.assertEquals(ConnectApi.MarkupType.Underline, markupBeginSegment.markupType); @@ -488,6 +505,15 @@ public class ConnectApiHelperTest { markupEndSegment = (ConnectApi.MarkupEndSegmentInput) segments.get(10); System.assertEquals(ConnectApi.MarkupType.OrderedList, markupEndSegment.markupType); + + markupBeginSegment = (ConnectApi.MarkupBeginSegmentInput) segments.get(11); + System.assertEquals(ConnectApi.MarkupType.Hyperlink, markupBeginSegment.markupType); + + textSegment = (ConnectApi.TextSegmentInput) segments.get(12); + System.assertEquals(text3, textSegment.text); + + markupEndSegment = (ConnectApi.MarkupEndSegmentInput) segments.get(13); + System.assertEquals(ConnectApi.MarkupType.Hyperlink, markupEndSegment.markupType); } @IsTest(SeeAllData=true) diff --git a/force-app/main/default/classes/ConnectApiHelperTest.cls-meta.xml b/force-app/main/default/classes/ConnectApiHelperTest.cls-meta.xml index 252fbfd..8e4d11f 100755 --- a/force-app/main/default/classes/ConnectApiHelperTest.cls-meta.xml +++ b/force-app/main/default/classes/ConnectApiHelperTest.cls-meta.xml @@ -1,5 +1,5 @@ - 47.0 + 49.0 Active