All files / src/components/blogs/nodes layout-blocks.ts

100% Statements 44/44
53.84% Branches 14/26
100% Functions 31/31
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                                        3x 3x     3x             18x 4x 4x         9x       4x                 3x             18x     1x 4x         2x       4x           9x       4x 4x 4x     10x           3x             36x 2x 2x           18x       4x         4x           9x       8x           4x                   3x             18x 4x 4x         9x       4x                   3x             9x       2x         3x              
// TipTap layout-block nodes for blog content.
//
// Each node round-trips to the EXACT markup that blog-content.css targets, so
// the same wrapper renders identically in the editor and on the marketplace
// (the marketplace sanitizer allowlists div/figure/figcaption/span for this).
// Image data is stored in node attrs and extracted from the inner <img> on
// parse, so existing/AI-authored HTML loads back into the editor unflattened.
//
// Inner <img> tags deliberately omit loading/referrerpolicy — the marketplace
// adds those via a global regex; emitting them here would duplicate the attrs.
//
// `Node` is imported from @tiptap/react (which re-exports @tiptap/core) because
// @tiptap/core is not a direct dependency and is not resolvable from app source
// under the isolated install. Blocks are inserted via the built-in
// `insertContent` command (see RichTextEditor) rather than custom commands, so
// no `@tiptap/core` module augmentation is needed.
import { Node } from "@tiptap/react";
 
export type ImageAttr = { src: string | null; alt: string };
 
const imgSrc = (el: HTMLElement) => el.querySelector("img")?.getAttribute("src") ?? null;
const imgAlt = (el: HTMLElement) => el.querySelector("img")?.getAttribute("alt") ?? "";
 
/** Full-width hero image: <div class="blog-image-hero"><img></div> */
export const HeroImage = Node.create({
	name: "heroImage",
	group: "block",
	atom: true,
	draggable: true,
 
	addAttributes() {
		return {
			src: { default: null, parseHTML: imgSrc, renderHTML: () => ({}) },
			alt: { default: "", parseHTML: imgAlt, renderHTML: () => ({}) },
		};
	},
 
	parseHTML() {
		return [{ tag: "div.blog-image-hero" }];
	},
 
	renderHTML({ node }) {
		return [
			"div",
			{ class: "blog-image-hero" },
			["img", { src: node.attrs.src, alt: node.attrs.alt }],
		];
	},
});
 
/** 2/3-column image grid: <div class="blog-image-grid grid-2|grid-3"><img>…</div> */
export const ImageGrid = Node.create({
	name: "imageGrid",
	group: "block",
	atom: true,
	draggable: true,
 
	addAttributes() {
		return {
			columns: {
				default: 2,
				parseHTML: (el: HTMLElement) => (el.classList.contains("grid-3") ? 3 : 2),
				renderHTML: () => ({}),
			},
			images: {
				default: [] as ImageAttr[],
				parseHTML: (el: HTMLElement) =>
					Array.from(el.querySelectorAll("img")).map((img) => ({
						src: img.getAttribute("src"),
						alt: img.getAttribute("alt") ?? "",
					})),
				renderHTML: () => ({}),
			},
		};
	},
 
	parseHTML() {
		return [{ tag: "div.blog-image-grid" }];
	},
 
	renderHTML({ node }) {
		const cols = node.attrs.columns === 3 ? "grid-3" : "grid-2";
		const images: ImageAttr[] = Array.isArray(node.attrs.images) ? node.attrs.images : [];
		return [
			"div",
			{ class: `blog-image-grid ${cols}` },
			...images.map((im) => ["img", { src: im.src, alt: im.alt ?? "" }]),
		];
	},
});
 
/** Before/after comparison with labels. */
export const ImageCompare = Node.create({
	name: "imageCompare",
	group: "block",
	atom: true,
	draggable: true,
 
	addAttributes() {
		const side = (index: number, fallbackLabel: string) => (el: HTMLElement) => {
			const fig = el.querySelectorAll("figure")[index];
			return {
				src: fig?.querySelector("img")?.getAttribute("src") ?? null,
				alt: fig?.querySelector("img")?.getAttribute("alt") ?? "",
				label: fig?.querySelector(".compare-label")?.textContent ?? fallbackLabel,
			};
		};
		return {
			before: {
				default: { src: null, alt: "", label: "Before" },
				parseHTML: side(0, "Before"),
				renderHTML: () => ({}),
			},
			after: {
				default: { src: null, alt: "", label: "After" },
				parseHTML: side(1, "After"),
				renderHTML: () => ({}),
			},
		};
	},
 
	parseHTML() {
		return [{ tag: "div.blog-image-compare" }];
	},
 
	renderHTML({ node }) {
		const fig = (d: { src: string | null; alt: string; label: string }) => [
			"figure",
			{},
			["img", { src: d?.src ?? null, alt: d?.alt ?? "" }],
			["figcaption", { class: "compare-label" }, d?.label ?? ""],
		];
		return [
			"div",
			{ class: "blog-image-compare" },
			fig(node.attrs.before),
			fig(node.attrs.after),
		];
	},
});
 
/** Single image with an editable caption. */
export const CaptionedImage = Node.create({
	name: "captionedImage",
	group: "block",
	content: "inline*",
	draggable: true,
 
	addAttributes() {
		return {
			src: { default: null, parseHTML: imgSrc, renderHTML: () => ({}) },
			alt: { default: "", parseHTML: imgAlt, renderHTML: () => ({}) },
		};
	},
 
	parseHTML() {
		return [{ tag: "figure.blog-image-captioned", contentElement: "figcaption" }];
	},
 
	renderHTML({ node }) {
		return [
			"figure",
			{ class: "blog-image-captioned" },
			["img", { src: node.attrs.src, alt: node.attrs.alt }],
			["figcaption", { class: "caption" }, 0],
		];
	},
});
 
/** Highlighted tip card with editable block content. */
export const TipCard = Node.create({
	name: "tipCard",
	group: "block",
	content: "block+",
	defining: true,
 
	parseHTML() {
		return [{ tag: "div.blog-tip-card" }];
	},
 
	renderHTML() {
		return ["div", { class: "blog-tip-card" }, 0];
	},
});
 
/** All layout-block extensions, ready to spread into the editor's `extensions`. */
export const layoutBlockExtensions = [
	HeroImage,
	ImageGrid,
	ImageCompare,
	CaptionedImage,
	TipCard,
];