← back to Dear Bubbe Nextjs

archived/social-posters/twitter-clean-formatter.js

70 lines

// Clean Twitter formatter - no ellipsis, complete sentences only
function formatCleanTweet(userMessage, bubbeResponse) {
  const hashtags = ['#BubbeAI', '#Bubbe', '#JewishGrandma', '#NoFilter'];
  const day = new Date().getDay();
  const dayTags = {
    0: '#SundayFunday',
    1: '#MondayMotivation',
    2: '#TuesdayThoughts', 
    3: '#WednesdayWisdom',
    4: '#ThursdayThoughts',
    5: '#FridayFeeling',
    6: '#SaturdayVibes'
  };
  
  // Add day hashtag
  hashtags.push(dayTags[day]);
  
  // Clean Bubbe's response - get first complete sentence or two
  let cleanResponse = bubbeResponse
    .replace(/\s+/g, ' ')
    .trim();
  
  // Find natural sentence breaks
  const sentences = cleanResponse.match(/[^.!?]+[.!?]+/g) || [cleanResponse];
  
  // Build tweet with complete sentences only
  const hashtagStr = hashtags.join(' ');
  const footer = 'Visit Bubbe.AI';
  const overhead = hashtagStr.length + footer.length + 2; // spaces
  const maxContent = 280 - overhead;
  
  let tweet = '';
  for (const sentence of sentences) {
    const trimmed = sentence.trim();
    if (tweet.length + trimmed.length + 1 <= maxContent) {
      tweet += (tweet ? ' ' : '') + trimmed;
    } else {
      break; // Stop at last complete sentence that fits
    }
  }
  
  // If no complete sentence fits, take first sentence and trim it
  if (!tweet && sentences.length > 0) {
    let firstSentence = sentences[0].trim();
    if (firstSentence.length > maxContent) {
      // Find last complete word before limit
      const words = firstSentence.split(' ');
      let built = '';
      for (const word of words) {
        if (built.length + word.length + 1 <= maxContent - 1) {
          built += (built ? ' ' : '') + word;
        } else {
          break;
        }
      }
      tweet = built + '!'; // Add exclamation to complete it
    } else {
      tweet = firstSentence;
    }
  }
  
  // Ensure it ends with punctuation
  if (tweet && !tweet.match(/[.!?]$/)) {
    tweet += '!';
  }
  
  return `${tweet} ${hashtagStr} ${footer}`;
}

module.exports = { formatCleanTweet };