function pickRandom(arr) { return arr[Math.floor(Math.random() * arr.length)]; } function escapeHtml(text) { if (!text) return ''; const map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return String(text).replace(/[&<>"']/g, m => map[m]); } export function dungeonTemplate(data) { const bodyFonts = [ "'Lora', serif", "'Merriweather', serif", "'Libre Baskerville', serif", "'Source Serif 4', serif" ]; const headingFonts = [ "'New Rocker', system-ui", "'UnifrakturCook', cursive", "'IM Fell DW Pica', serif", "'Cinzel', serif", "'Cormorant Garamond', serif", "'Playfair Display', serif" ]; const quoteFonts = [ "'Playfair Display', serif", "'Libre Baskerville', serif", "'Merriweather', serif" ]; const bodyFont = pickRandom(bodyFonts); const headingFont = pickRandom(headingFonts); const quoteFont = pickRandom(quoteFonts); // Check if we have a map image to include const hasMap = data.map && typeof data.map === 'string' && data.map.startsWith('data:image/'); return ` ${data.title}

${escapeHtml(data.title)}

${data.flavor ? `

${escapeHtml(data.flavor)}

` : ''}
${data.hooksRumors && data.hooksRumors.length > 0 ? `

Hooks & Rumors

    ${data.hooksRumors.map(hook => `
  • ${escapeHtml(hook)}
  • `).join('')}
` : ''} ${data.randomEvents && data.randomEvents.length > 0 ? `

Random Events (d6)

${data.randomEvents.map((event, index) => ` `).join('')}
${index + 1} ${escapeHtml(event)}
` : ''} ${data.rooms && data.rooms.length > 0 ? `

Locations

${data.rooms.map(room => { let desc = room.description || ''; // Truncate to 2 sentences max const sentences = desc.match(/[^.!?]+[.!?]+/g) || [desc]; if (sentences.length > 2) { desc = sentences.slice(0, 2).join(' ').trim(); } // Also limit by character count (~150 chars) if (desc.length > 150) { desc = desc.substring(0, 147).trim(); const lastPeriod = desc.lastIndexOf('.'); if (lastPeriod > 100) { desc = desc.substring(0, lastPeriod + 1); } else { desc += '...'; } } return `

${escapeHtml(room.name)}

${escapeHtml(desc)}

`; }).join('')}
` : ''}
${data.encounters && data.encounters.length > 0 ? `

Encounters (d6)

${data.encounters.map((encounter, index) => { // Truncate details to 4 sentences max to prevent overflow let details = encounter.details || ''; // Remove encounter name if it appears at start if (details.toLowerCase().startsWith(encounter.name.toLowerCase())) { details = details.substring(encounter.name.length).replace(/^:\s*/, '').trim(); } // Remove location prefix if present (format: "Location Name: description") // Handle multiple colons - strip the first one that looks like a location const locationMatch = details.match(/^([^:]+):\s*(.+)$/); if (locationMatch) { const potentialLocation = locationMatch[1].trim(); // If it looks like a location name (capitalized, not too long), remove it if (potentialLocation.length > 3 && potentialLocation.length < 50 && /^[A-Z]/.test(potentialLocation)) { details = locationMatch[2].trim(); } } // Split into sentences and keep only first 4 const sentences = details.match(/[^.!?]+[.!?]+/g) || [details]; if (sentences.length > 4) { details = sentences.slice(0, 4).join(' ').trim(); } // Also limit by character count as fallback (max ~350 chars) if (details.length > 350) { details = details.substring(0, 347).trim(); // Try to end at a sentence boundary const lastPeriod = details.lastIndexOf('.'); if (lastPeriod > 280) { details = details.substring(0, lastPeriod + 1); } else { details += '...'; } } return ` `; }).join('')}
${index + 1} ${escapeHtml(encounter.name)} ${escapeHtml(details)}
` : ''}
${data.treasure && data.treasure.length > 0 ? `

Treasure

${data.treasure.map(item => { if (typeof item === 'object' && item.name && item.description) { return `
${escapeHtml(item.name)} — ${escapeHtml(item.description)}
`; } else if (typeof item === 'string') { // Handle string format "Name — Description" const parts = item.split(/[—–-]/); if (parts.length >= 2) { return `
${escapeHtml(parts[0].trim())} — ${escapeHtml(parts.slice(1).join(' ').trim())}
`; } return `
${escapeHtml(item)}
`; } return ''; }).filter(Boolean).join('')}
` : ''} ${data.npcs && data.npcs.length > 0 ? `

NPCs

${data.npcs.map(npc => { if (typeof npc === 'object' && npc.name && npc.trait) { return `
${escapeHtml(npc.name)}: ${escapeHtml(npc.trait)}
`; } else if (typeof npc === 'string') { // Handle string format "Name: Description" const parts = npc.split(/:/); if (parts.length >= 2) { return `
${escapeHtml(parts[0].trim())}: ${escapeHtml(parts.slice(1).join(':').trim())}
`; } return `
${escapeHtml(npc)}
`; } return ''; }).filter(Boolean).join('')}
` : ''} ${data.plotResolutions && data.plotResolutions.length > 0 ? `

Plot Resolutions

${data.plotResolutions.map(resolution => { // Truncate to 2 sentences max to prevent overflow let text = resolution || ''; // Split into sentences and keep only first 2 const sentences = text.match(/[^.!?]+[.!?]+/g) || [text]; if (sentences.length > 2) { text = sentences.slice(0, 2).join(' ').trim(); } // Also limit by character count as fallback (max ~150 chars) if (text.length > 150) { text = text.substring(0, 147).trim(); // Try to end at a sentence boundary const lastPeriod = text.lastIndexOf('.'); if (lastPeriod > 100) { text = text.substring(0, lastPeriod + 1); } else { text += '...'; } } return `
${escapeHtml(text)}
`; }).join('')}
` : ''}
${hasMap ? `
Dungeon Map
` : ''} `; }