3 Replies Latest reply on Nov 10, 2010 12:36 PM by richardh6

    [JS] Find hyperlinks in a block of text

    richardh6 Level 1

      Is there any way I can easily, given a block of text, find which hyperlinks have their SourceTexts inside that block of text, or overlapping it?

       

      The only way I could think of was to loop through all the hyperlinks in the document and then check to see if any character in their SourceText was === to any character in the block of text we're searching.  That seems a little wasteful.

        • 1. Re: [JS] Find hyperlinks in a block of text
          richardh6 Level 1

          This works, but it's slow:

           

          // Returns an array of Hyperlinks whose SourceTexts are 
          // contained within (or overlapping with) textObj
          function checkHyperlinks (textObj, doc) {
            var foundLinks = new Array();
            var overlap;
            for (var linkIndex=doc.hyperlinks.length-1; linkIndex >= 0; linkIndex--) {
              overlap = false;
              var link = doc.hyperlinks[linkIndex];
              var linkText = link.source.sourceText;
              for (c=0; c < linkText.length; c++) {
                for (d=0; d < textObj.length; d++) {
                  if (linkText.characters[c] == textObj.characters[d]) {
                    overlap = true;
                    break;
                  }
                }
                if (overlap) break;
              }
              if (overlap) {
                foundLinks.push (link);
              }
            }
            return foundLinks;
          }
          
          • 2. Re: [JS] Find hyperlinks in a block of text
            Harbs. Level 6

            I'd do it like this:

             

            Step 1: check the story of each hyperlink source. If the story is not the one you are checking, throw it out.

             

            Step 2 Check the index of the sources left. If the index is greater than the start of your text and less than the end, it's in your range.

             

            This should be very fast...

             

            Harbs

            • 3. Re: [JS] Find hyperlinks in a block of text
              richardh6 Level 1

              Thanks.  That ought to do it.