|
1 | 1 | import { TERRITORY_GRACE_DAYS, TERRITORY_PERIOD_COST } from './constants'
|
2 | 2 | import { datePivot, diffDays } from './time'
|
| 3 | +import removeMd from 'remove-markdown' |
3 | 4 |
|
4 | 5 | export function nextBilling (relativeTo, billingType) {
|
5 | 6 | if (!relativeTo || billingType === 'ONCE') return null
|
@@ -28,3 +29,103 @@ export function nextBillingWithGrace (sub) {
|
28 | 29 | if (!sub) return null
|
29 | 30 | return datePivot(new Date(sub.billPaidUntil), { days: TERRITORY_GRACE_DAYS })
|
30 | 31 | }
|
| 32 | + |
| 33 | +function cleanMarkdownText (text) { |
| 34 | + if (!text) return '' |
| 35 | + |
| 36 | + let cleaned = removeMd(text) |
| 37 | + cleaned = cleaned |
| 38 | + .replace(/\s+/g, ' ') // Replace multiple spaces with single space |
| 39 | + .replace(/\[\[([^\]]+)\]\]\([^)]+\)/g, '$1') // Remove nested markdown links, keep inner text |
| 40 | + .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1') // Remove regular markdown links, keep text |
| 41 | + .replace(/!\[([^\]]*)\]\([^)]+\)/g, '') // Remove images completely |
| 42 | + .replace(/`([^`]+)`/g, '$1') // Remove code formatting |
| 43 | + .replace(/\*\*([^*]+)\*\*/g, '$1') // Remove bold formatting |
| 44 | + .replace(/\*([^*]+)\*/g, '$1') // Remove italic formatting |
| 45 | + .trim() |
| 46 | + |
| 47 | + return cleaned |
| 48 | +} |
| 49 | + |
| 50 | +function truncateAtSentenceBoundary (text, maxLength = 160) { |
| 51 | + if (!text || text.length <= maxLength) return text |
| 52 | + const sentences = text |
| 53 | + .split(/[.!?]+/) |
| 54 | + .map(s => s.trim()) |
| 55 | + .filter(Boolean) |
| 56 | + |
| 57 | + let result = '' |
| 58 | + let currentLength = 0 |
| 59 | + |
| 60 | + for (const sentence of sentences) { |
| 61 | + const separator = result ? '. ' : '' |
| 62 | + const newLength = currentLength + separator.length + sentence.length |
| 63 | + |
| 64 | + if (newLength <= maxLength - 3) { |
| 65 | + result += separator + sentence |
| 66 | + currentLength = newLength |
| 67 | + } else { |
| 68 | + break |
| 69 | + } |
| 70 | + } |
| 71 | + if (result) { |
| 72 | + return result + '...' |
| 73 | + } |
| 74 | + // Fallback to simple truncation |
| 75 | + return text.substring(0, maxLength - 3) + '...' |
| 76 | +} |
| 77 | + |
| 78 | +export function processTerritoryDescription (territory) { |
| 79 | + if (!territory) return null |
| 80 | + |
| 81 | + if (territory.desc) { |
| 82 | + const processedDesc = cleanMarkdownText(territory.desc) |
| 83 | + |
| 84 | + if (processedDesc.length >= 30 && processedDesc.length <= 160) { |
| 85 | + return processedDesc |
| 86 | + } |
| 87 | + if (processedDesc.length > 160) { |
| 88 | + return truncateAtSentenceBoundary(processedDesc, 160) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Generate a contextual description if the original is too short or unsuitable |
| 93 | + return generateTerritoryDescription(territory) |
| 94 | +} |
| 95 | + |
| 96 | +export function generateTerritoryDescription (territory) { |
| 97 | + const parts = [] |
| 98 | + if (territory.desc) { |
| 99 | + const processedDesc = cleanMarkdownText(territory.desc) |
| 100 | + if (processedDesc) { |
| 101 | + parts.push(processedDesc) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (territory.postTypes && territory.postTypes.length > 0) { |
| 106 | + const postTypeLabels = territory.postTypes.map(type => { |
| 107 | + switch (type) { |
| 108 | + case 'LINK': return 'links' |
| 109 | + case 'DISCUSSION': return 'discussions' |
| 110 | + case 'BOUNTY': return 'bounties' |
| 111 | + case 'POLL': return 'polls' |
| 112 | + default: return type.toLowerCase() |
| 113 | + } |
| 114 | + }) |
| 115 | + parts.push(postTypeLabels.join(', ')) |
| 116 | + } |
| 117 | + |
| 118 | + if (territory.user?.name) { |
| 119 | + parts.push(`created by @${territory.user.name}`) |
| 120 | + } |
| 121 | + |
| 122 | + let desc = parts.length > 1 |
| 123 | + ? `Stacker.news community: ${parts.join('. ')}` |
| 124 | + : `Stacker.news community for ${parts.join(', ')}` |
| 125 | + |
| 126 | + if (desc.length > 160) { |
| 127 | + desc = truncateAtSentenceBoundary(desc, 160) |
| 128 | + } |
| 129 | + |
| 130 | + return desc |
| 131 | +} |
0 commit comments