export function escapeHTML(text) { return String(text).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]); } export function createDocument(template, rendered) { const m = rendered.metadata; const escape = escapeHTML; const schema = rendered.schema ? `\n` : ''; const head = `${escape(m.title)}\n` + `\n` + `\n` + `\n\n` + `\n` + `\n` + `\n` + (m.image ? `\n` : '') + schema; return template.replace(/[\s\S]*?<\/title>/, '').replace(/<meta name="description"[^>]*>/, '') .replace('</head>', `${head}</head>`) .replace('<div id="root"></div>', `<div id="root">${rendered.html}</div>\n<script id="page-data" type="application/json">${rendered.initial}</script>`); }