Skip to content

Instantly share code, notes, and snippets.

@agjs
Created November 13, 2023 20:25
Show Gist options
  • Save agjs/bcd1b5757b055742aaba0057f628a226 to your computer and use it in GitHub Desktop.
Save agjs/bcd1b5757b055742aaba0057f628a226 to your computer and use it in GitHub Desktop.
tiptap
export const tiptapToHtml = ({ string, idToLabel, enableSuggestions, controls }) => {
try {
if (!string) {
return "";
}
const htmlContent = sanitize(
generateHTML(JSON.parse(string), editorConfig({ controls, suggestionItems: () => [], idToLabel, enableSuggestions }).extensions),
{
allowedTags: ["p", "span", "a", "iframe", "img", ...getControls(controls)],
allowedAttributes: {
a: ["href", "data-type"],
iframe: ["src"],
img: ["src", "class", "srcset", "alt", "title", "width", "height", "loading"],
span: ["data-id", "data-type"]
},
selfClosing: ["img"],
allowedClasses: {
"*": ["text-primary", "text-4xl", "text-3xl", "text-2xl", "text-xl", "my-4", "mb-2", "mt-2", "wordbreak"],
ol: [
"list-decimal",
"list-disc",
"mb-2",
"pl-1",
"ml-4",
"text-primary-text-color",
"marker:text-primary",
"marker:italic",
"marker:font-bold"
],
ul: [
"list-decimal",
"list-disc",
"mb-2",
"pl-1",
"ml-4",
"text-primary-text-color",
"marker:text-primary",
"marker:italic",
"marker:font-bold"
],
iframe: ["aspect-video", "w-full", "h-full"],
img: ["block", "w-full", "h-full", "object-cover", "object-center", "mx-auto", "p-2", "border", "border-primary-background-color"]
},
allowedIframeHostnames: ["www.youtube.com"]
}
);
return { htmlContent };
} catch (error) {
return { htmlContent: "" };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment