public String processAttachmentsReferences(
      long userId, KBArticle kbArticle, ZipReader zipReader, Map<String, FileEntry> fileEntriesMap)
      throws PortalException {

    Set<Integer> indexes = new TreeSet<>();

    int index = 0;

    while ((index = _html.indexOf("<img", index)) > -1) {
      indexes.add(index);

      index += 4;
    }

    if (indexes.isEmpty()) {
      return _html;
    }

    StringBundler sb = new StringBundler();

    int previousIndex = 0;

    for (int curIndex : indexes) {
      if (curIndex < 0) {
        break;
      }

      if (curIndex > previousIndex) {

        // Append text from previous position up to image tag

        String text = _html.substring(previousIndex, curIndex);

        sb.append(text);
      }

      int pos = _html.indexOf("/>", curIndex);

      if (pos < 0) {
        if (_log.isDebugEnabled()) {
          _log.debug("Expected close tag for image " + _html.substring(curIndex));
        }

        sb.append(_html.substring(curIndex));

        previousIndex = curIndex;

        break;
      }

      String text = _html.substring(curIndex, pos);

      String imageFileName = KBArticleImporterUtil.extractImageFileName(text);

      FileEntry imageFileEntry =
          KBArticleImporterUtil.addImageFileEntry(
              imageFileName, userId, kbArticle, zipReader, fileEntriesMap);

      if (imageFileEntry == null) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to find image source " + text);
        }

        sb.append("<img alt=\"missing image\" src=\"\" ");
      } else {
        String imageSrc = StringPool.BLANK;

        try {
          imageSrc =
              DLUtil.getPreviewURL(
                  imageFileEntry, imageFileEntry.getFileVersion(), null, StringPool.BLANK);
        } catch (PortalException pe) {
          if (_log.isWarnEnabled()) {
            _log.warn(
                "Unable to obtain image URL from file entry " + imageFileEntry.getFileEntryId());
          }
        }

        sb.append("<img alt=\"");
        sb.append(HtmlUtil.escapeAttribute(imageFileEntry.getTitle()));
        sb.append("\" src=\"");
        sb.append(imageSrc);
        sb.append("\" ");
      }

      previousIndex = pos;
    }

    if (previousIndex < _html.length()) {
      sb.append(_html.substring(previousIndex));
    }

    return sb.toString();
  }