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 = `'; 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;
}