function pickRandom(arr) { return arr[Math.floor(Math.random() * arr.length)]; } 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}

${data.title}

${data.flavor ? `

${data.flavor}

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

Hooks & Rumors

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

Random Events (d6)

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

Locations

${data.rooms.map(room => `

${room.name}

${room.description}

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

Encounters (d6)

${data.encounters.map((encounter, index) => { // Truncate details to 2-3 sentences max to prevent overflow let details = encounter.details || ''; // Remove location prefix from details if present (format: "Location Name: details") details = details.replace(/^[^:]+:\s*/, ''); // Split into sentences and keep only first 3 const sentences = details.match(/[^.!?]+[.!?]+/g) || [details]; if (sentences.length > 3) { details = sentences.slice(0, 3).join(' ').trim(); } // Also limit by character count as fallback (max ~250 chars) if (details.length > 250) { details = details.substring(0, 247).trim(); // Try to end at a sentence boundary const lastPeriod = details.lastIndexOf('.'); if (lastPeriod > 200) { details = details.substring(0, lastPeriod + 1); } else { details += '...'; } } return ` `; }).join('')}
${index + 1} ${encounter.name} ${details}
` : ''}
${data.treasure && data.treasure.length > 0 ? `

Treasure

${data.treasure.map(item => `
${typeof item === 'object' && item.name ? `${item.name} — ${item.description}` : item}
`).join('')}
` : ''} ${data.npcs && data.npcs.length > 0 ? `

NPCs

${data.npcs.map(npc => `
${npc.name}: ${npc.trait}
`).join('')}
` : ''} ${data.plotResolutions && data.plotResolutions.length > 0 ? `

Plot Resolutions

${data.plotResolutions.map(resolution => { // Truncate to 1-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 `
${text}
`; }).join('')}
` : ''}
${hasMap ? `
Dungeon Map
` : ''} `; }