UNPKG

@maizzle/framework

Version:

Maizzle is a framework that helps you quickly build HTML emails with Tailwind CSS.

38 lines (37 loc) 1.58 kB
//#region src/render/plugins/codeBlockExtract.ts /** * Vite plugin that extracts raw slot content from <CodeBlock> tags * and passes it as a :code prop before Vue compiles the template. * * This lets users write HTML naturally inside CodeBlock slots without * Vue attempting to compile it as template syntax. */ function codeBlockExtract() { const re = /<(CodeBlock|code-block)((?:\s[^>]*?)?)>([\s\S]*?)<\/\1>/g; return { name: "maizzle:code-block-extract", enforce: "pre", transform(code, id) { if (!id.endsWith(".vue") && !id.endsWith(".md")) return; if (!code.includes("CodeBlock") && !code.includes("code-block")) return; const transformed = code.replace(re, (_match, tag, attrs, content) => { if (/(?:^|\s):code\b/.test(attrs) || /v-bind:code\b/.test(attrs)) return _match; /** * Strip leading/trailing blank lines, then dedent based on * the minimum indent of non-empty lines (à la min-indent). */ const stripped = content.replace(/^\n+/, "").replace(/\s+$/, ""); if (!stripped) return _match; const minIndent = stripped.match(/^[ \t]*(?=\S)/gm)?.reduce((min, ws) => Math.min(min, ws.length), Infinity) ?? 0; return `<${tag}${attrs} code="${(minIndent > 0 ? stripped.replace(new RegExp(`^[ \\t]{${minIndent}}`, "gm"), "") : stripped).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;")}" />`; }); if (transformed !== code) return { code: transformed, map: null }; } }; } //#endregion export { codeBlockExtract }; //# sourceMappingURL=codeBlockExtract.js.map