packages/devtools-reps/src/reps/string.js
function getLinkifiedElements(text, cropLimit, openLink, isInContentPage) {
  const halfLimit = Math.ceil((cropLimit - ELLIPSIS.length) / 2);
  const startCropIndex = cropLimit ? halfLimit : null;
  const endCropIndex = cropLimit ? text.length - halfLimit : null;

  const items = [];
  let currentIndex = 0;
  let contentStart;
  while (true) {
    const url = urlRegex.exec(text);
    // Pick the regexp with the earlier content; index will always be zero.
    if (!url) {
      break;
    }
    contentStart = url.index + url[1].length;
    if (contentStart > 0) {
      const nonUrlText = text.substring(0, contentStart);
      items.push(
        getCroppedString(nonUrlText, currentIndex, startCropIndex, endCropIndex)
      );
    }

    // There are some final characters for a URL that are much more likely
    // to have been part of the enclosing text rather than the end of the
    // URL.
    let useUrl = url[2];
    const uneat = uneatLastUrlCharsRegex.exec(useUrl);
    if (uneat) {
      useUrl = useUrl.substring(0, uneat.index);
    }

    currentIndex = currentIndex + contentStart;
    const linkText = getCroppedString(
      useUrl,
      currentIndex,
      startCropIndex,
      endCropIndex
    );

    if (linkText) {
      items.push(
        a(
          {
            className: "url",
            title: useUrl,
            draggable: false,
            // Because we don't want the link to be open in the current
            // panel's frame, we only render the href attribute if `openLink`
            // exists (so we can preventDefault) or if the reps will be
            // displayed in content page (e.g. in the JSONViewer).
            href: openLink || isInContentPage ? useUrl : null,
            onClick: openLink
              ? e => {
                  e.preventDefault();
                  openLink(useUrl, e);
                }
              : null
          },
          linkText
        )
      );
    }

    currentIndex = currentIndex + useUrl.length;
    text = text.substring(url.index + url[1].length + useUrl.length);
  }

  // Clean up any non-URL text at the end of the source string,
  // i.e. not handled in the loop.
  if (text.length > 0) {
    if (currentIndex < endCropIndex) {
      text = getCroppedString(text, currentIndex, startCropIndex, endCropIndex);
    }
    items.push(text);
  }

  return items;
}
function getLinkifiedElements(text, cropLimit, openLink, isInContentPage) {
  const halfLimit = Math.ceil((cropLimit - ELLIPSIS.length) / 2);
  const startCropIndex = cropLimit ? halfLimit : null;
  const endCropIndex = cropLimit ? text.length - halfLimit : null;

  const items = [];
  let currentIndex = 0;
  let contentStart;
  while (true) {
    const url = urlRegex.exec(text);
    // Pick the regexp with the earlier content; index will always be zero.
    if (!url) {
      break;
    }
    contentStart = url.index + url[1].length;
    if (contentStart > 0) {
      const nonUrlText = text.substring(0, contentStart);
      items.push(
        getCroppedString(nonUrlText, currentIndex, startCropIndex, endCropIndex)
      );
    }

    // There are some final characters for a URL that are much more likely
    // to have been part of the enclosing text rather than the end of the
    // URL.
    let useUrl = url[2];
    const uneat = uneatLastUrlCharsRegex.exec(useUrl);
    if (uneat) {
      useUrl = useUrl.substring(0, uneat.index);
    }

    currentIndex = currentIndex + contentStart;
    const linkText = getCroppedString(
      useUrl,
      currentIndex,
      startCropIndex,
      endCropIndex
    );

    if (linkText) {
      items.push(
        a(
          {
            className: "url",
            title: useUrl,
            draggable: false,
            // Because we don't want the link to be open in the current
            // panel's frame, we only render the href attribute if `openLink`
            // exists (so we can preventDefault) or if the reps will be
            // displayed in content page (e.g. in the JSONViewer).
            href: openLink || isInContentPage ? useUrl : null,
            onClick: openLink
              ? e => {
                  e.preventDefault();
                  openLink(useUrl, e);
                }
              : null
          },
          linkText
        )
      );
    }

    currentIndex = currentIndex + useUrl.length;
    text = text.substring(url.index + url[1].length + useUrl.length);
  }

  // Clean up any non-URL text at the end of the source string,
  // i.e. not handled in the loop.
  if (text.length > 0) {
    if (currentIndex < endCropIndex) {
      text = getCroppedString(text, currentIndex, startCropIndex, endCropIndex);
    }
    items.push(text);
  }

  return items;
}
function getLinkifiedElements ( text , cropLimit , openLink , isInContentPage ) { const halfLimit = Math . ceil ( ( cropLimit - ELLIPSIS . length ) / 2 ) ; const startCropIndex = cropLimit ? halfLimit : null ; const endCropIndex = cropLimit ? text . length - halfLimit : null ; const items = [ ] ; let currentIndex = 0 ; let contentStart ; while ( true ) { const url = urlRegex . exec ( text ) ; // Pick the regexp with the earlier content; index will always be zero. if ( ! url ) { break ; } contentStart = url . index + url [ 1 ] . length ; if ( contentStart > 0 ) { const nonUrlText = text . substring ( 0 , contentStart ) ; items . push ( getCroppedString ( nonUrlText , currentIndex , startCropIndex , endCropIndex ) ) ; } // There are some final characters for a URL that are much more likely // to have been part of the enclosing text rather than the end of the // URL. let useUrl = url [ 2 ] ; const uneat = uneatLastUrlCharsRegex . exec ( useUrl ) ; if ( uneat ) { useUrl = useUrl . substring ( 0 , uneat . index ) ; } currentIndex = currentIndex + contentStart ; const linkText = getCroppedString ( useUrl , currentIndex , startCropIndex , endCropIndex ) ; if ( linkText ) { items . push ( a ( { className : "url" , title : useUrl , draggable : false , // Because we don't want the link to be open in the current // panel's frame, we only render the href attribute if `openLink` // exists (so we can preventDefault) or if the reps will be // displayed in content page (e.g. in the JSONViewer). href : openLink || isInContentPage ? useUrl : null , onClick : openLink ? e => { e . preventDefault ( ) ; openLink ( useUrl , e ) ; } : null } , linkText ) ) ; } currentIndex = currentIndex + useUrl . length ; text = text . substring ( url . index + url [ 1 ] . length + useUrl . length ) ; } // Clean up any non-URL text at the end of the source string, // i.e. not handled in the loop. if ( text . length > 0 ) { if ( currentIndex < endCropIndex ) { text = getCroppedString ( text , currentIndex , startCropIndex , endCropIndex ) ; } items . push ( text ) ; } return items ; }
Get an array of the elements representing the string, cropped if needed,
with actual links.

@param {String} text: The actual string to linkify.
@param {Integer | null} cropLimit
@param {Function} openLink: Function handling the link opening.
@param {Boolean} isInContentPage: pass true if the reps is rendered in
the content page (e.g. in JSONViewer).
@returns {Array<String|ReactElement>}
Get an array of the elements representing the string cropped if needed with actual links .