|
| 1 | +// Simple test for tag extraction functionality |
| 2 | +function extractHashtagsFromContent(content) { |
| 3 | + // Match hashtags that are standalone or at the end of lines |
| 4 | + // This regex looks for # followed by word characters, allowing for hyphens, underscores, and spaces in multi-word hashtags |
| 5 | + const hashtagRegex = /#([a-zA-Z][a-zA-Z0-9\s_-]*?)(?=\s|$|[^a-zA-Z0-9\s_-])/g; |
| 6 | + |
| 7 | + const hashtags = []; |
| 8 | + let match; |
| 9 | + |
| 10 | + while ((match = hashtagRegex.exec(content)) !== null) { |
| 11 | + const tag = match[1].trim().toLowerCase().replace(/[\s_-]+/g, '-'); // Normalize spaces and underscores to hyphens |
| 12 | + if (tag.length > 0 && tag.length <= 25) { // Medium tag limit |
| 13 | + hashtags.push(tag); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + return [...new Set(hashtags)]; // Remove duplicates |
| 18 | +} |
| 19 | + |
| 20 | +function processTagsForMedium(explicitTags = [], content) { |
| 21 | + // Extract hashtags from content |
| 22 | + const extractedTags = extractHashtagsFromContent(content); |
| 23 | + |
| 24 | + // Combine explicit tags with extracted tags |
| 25 | + const allTags = [...explicitTags, ...extractedTags]; |
| 26 | + |
| 27 | + // Normalize tags: lowercase, trim, remove duplicates |
| 28 | + const normalizedTags = allTags |
| 29 | + .map(tag => tag.toLowerCase().trim()) |
| 30 | + .filter(tag => tag.length > 0 && tag.length <= 25) |
| 31 | + .filter((tag, index, arr) => arr.indexOf(tag) === index); // Remove duplicates |
| 32 | + |
| 33 | + // Medium allows maximum 5 tags |
| 34 | + const finalTags = normalizedTags.slice(0, 5); |
| 35 | + |
| 36 | + if (normalizedTags.length > 5) { |
| 37 | + console.warn(`⚠️ Medium allows maximum 5 tags. Using first 5: ${finalTags.join(', ')}`); |
| 38 | + } |
| 39 | + |
| 40 | + return finalTags; |
| 41 | +} |
| 42 | + |
| 43 | +// Test the functionality |
| 44 | +const content = '# This is a test post about #javascript and #web-development. Also covers #react and #typescript.'; |
| 45 | +const explicitTags = ['programming', 'coding']; |
| 46 | + |
| 47 | +console.log('Content:', content); |
| 48 | +console.log('Explicit tags:', explicitTags); |
| 49 | +console.log('Extracted hashtags:', extractHashtagsFromContent(content)); |
| 50 | +console.log('Processed tags:', processTagsForMedium(explicitTags, content)); |
| 51 | + |
| 52 | +// Test edge cases |
| 53 | +console.log('\n--- Edge Cases ---'); |
| 54 | +console.log('No hashtags:', processTagsForMedium(['tech'], 'Regular content without hashtags')); |
| 55 | +console.log('Only hashtags:', processTagsForMedium([], '#javascript #react #vue #angular #svelte #nextjs')); |
| 56 | +console.log('Mixed case:', processTagsForMedium(['Tech'], '#JavaScript #react')); |
| 57 | +console.log('Long tags:', processTagsForMedium([], '#this-is-a-very-long-tag-that-exceeds-the-twenty-five-character-limit')); |
0 commit comments