html-utils.js (1.8 KB)
1 // Insert content right after <head> tag in HTML string 2 export function insertAfterHead(html, content) { 3 const headMatch = html.match(/<head[^>]*>/i); 4 if (headMatch) { 5 const insertPos = html.indexOf(headMatch[0]) + headMatch[0].length; 6 return html.slice(0, insertPos) + content + html.slice(insertPos); 7 } 8 return html; 9 } 10 11 // Extract title and favicon from user's HTML 12 export function extractTitleAndFavicon(htmlCode) { 13 const parser = new DOMParser(); 14 const doc = parser.parseFromString(htmlCode, 'text/html'); 15 const titleElement = doc.querySelector('title'); 16 const title = titleElement ? titleElement.textContent.trim() : null; 17 const faviconSelectors = [ 18 'link[rel="icon"]', 19 'link[rel="shortcut icon"]', 20 'link[rel="apple-touch-icon"]' 21 ]; 22 let favicon = null; 23 for (const selector of faviconSelectors) { 24 const faviconElement = doc.querySelector(selector); 25 if (faviconElement && faviconElement.getAttribute('href')) { 26 favicon = faviconElement.getAttribute('href'); 27 break; 28 } 29 } 30 return { title, favicon }; 31 } 32 33 export function updateMainPageTitleAndFavicon(title, favicon) { 34 if (title) { 35 document.title = title; 36 } else { 37 document.title = 'zine studio'; 38 } 39 let faviconLink = document.querySelector('link[rel="icon"]'); 40 if (!faviconLink) { 41 faviconLink = document.createElement('link'); 42 faviconLink.rel = 'icon'; 43 document.head.appendChild(faviconLink); 44 } 45 if (favicon) { 46 faviconLink.href = favicon; 47 } else { 48 faviconLink.href = 'data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🖨️</text></svg>'; 49 } 50 } 51 52 // Create an empty placeholder div for single-page spreads 53 export function createEmptyPlaceholder(doc) { 54 const empty = doc.createElement('div'); 55 empty.className = 'zine-empty'; 56 return empty; 57 }