import {TiptapAttrValue, TiptapLinkAttrs, TiptapNode} from "@/lib/types/chapter"; function isTiptapLinkAttrs(value: TiptapAttrValue): value is TiptapLinkAttrs { return typeof value === 'object' && value !== null && 'href' in value; } export function getPageCount(text: string): number { const charactersPerLine: number = 90; const linesPerPage: number = 40; const lines: string[] = text.split('\n'); let totalLines: number = 0; lines.forEach((line: string) => { const lineLength: number = line.length; const estimatedLines: number = Math.ceil(lineLength / charactersPerLine); totalLines += estimatedLines; }); return Math.ceil(totalLines / linesPerPage); } export function convertTiptapToHTML(node: TiptapNode): string { let html: string = ''; switch (node.type) { case 'doc': if (node.content) { node.content.forEach((childNode: TiptapNode) => { html += convertTiptapToHTML(childNode); }); } break; case 'paragraph': html += '

'; if (node.content) { node.content.forEach((childNode: TiptapNode) => { html += convertTiptapToHTML(childNode); }); } html += '

'; break; case 'text': let textContent: string = node.text || ''; if (node.attrs) { if (node.attrs.bold) { textContent = `${textContent}`; } if (node.attrs.italic) { textContent = `${textContent}`; } if (node.attrs.underline) { textContent = `${textContent}`; } if (node.attrs.strike) { textContent = `${textContent}`; } if (node.attrs.link && isTiptapLinkAttrs(node.attrs.link)) { textContent = `${textContent}`; } } html += textContent; break; case 'heading': const level: number = typeof node.attrs?.level === 'number' ? node.attrs.level : 1; html += ``; if (node.content) { node.content.forEach((childNode: TiptapNode) => { html += convertTiptapToHTML(childNode); }); } html += ``; break; case 'bulletList': html += ''; break; case 'orderedList': html += '
    '; if (node.content) { node.content.forEach((childNode: TiptapNode) => { html += convertTiptapToHTML(childNode); }); } html += '
'; break; case 'listItem': html += '
  • '; if (node.content) { node.content.forEach((childNode: TiptapNode) => { html += convertTiptapToHTML(childNode); }); } html += '
  • '; break; case 'blockquote': html += '
    '; if (node.content) { node.content.forEach((childNode: TiptapNode) => { html += convertTiptapToHTML(childNode); }); } html += '
    '; break; case 'codeBlock': html += '
    ';
                if (node.content) {
                    node.content.forEach((childNode: TiptapNode) => {
                        html += convertTiptapToHTML(childNode);
                    });
                }
                html += '
    '; break; default: if (node.content) { node.content.forEach((childNode: TiptapNode) => { html += convertTiptapToHTML(childNode); }); } break; } return html; }