// composables/useHtmlConverter.ts
interface TextChild {
type?: "text";
text: string;
bold?: boolean;
italic?: boolean;
underline?: boolean;
}
interface LinkChild {
type: "link";
url: string;
children: TextChild[];
}
type ParagraphChild = TextChild | LinkChild;
interface ParagraphBlock {
type: "paragraph";
children: ParagraphChild[];
}
interface HeadingBlock {
type: "heading";
level: number;
children: TextChild[];
}
interface ListBlock {
type: "list";
format: "unordered" | "ordered";
children: {
children: TextChild[];
}[];
}
type RichTextBlock = ParagraphBlock | HeadingBlock | ListBlock;
export function useHtmlConverter() {
const convertToHTML = (data: RichTextBlock[], prepend?: string): string => {
let html = "";
let firstParagraph = true;
if (Array.isArray(data)) {
data.forEach((item) => {
switch (item.type) {
case "heading":
if (item.children?.[0]?.text) {
html += `
`; if (firstParagraph && prepend !== undefined) { html += `${prepend}`; firstParagraph = false; } item.children.forEach((c) => { if (c.type === "text") { if (c.bold) html += `${c.text}`; else if (c.underline) html += `${c.text}`; else if (c.italic) html += `${c.text}`; else html += `${c.text}`; } else if (c.type === "link") { html += `${c.children[0].text}`; } }); html += `
`; } break; case "list": if (Array.isArray(item.children)) { const tag = item.format === "ordered" ? "ol" : "ul"; html += `<${tag}>`; item.children.forEach((listItem) => { if (listItem.children?.[0]?.text) { html += `