import type { SliceMachineHelpers, SnippetReadHook, } from "@slicemachine/plugin-kit"; import { stripIndent } from "common-tags"; import type { PluginOptions } from "../types"; const dotPath = (segments: string[]): string => { return segments.join("."); }; const format = async (input: string, helpers: SliceMachineHelpers) => { const formattedInput = await helpers.format(input, undefined, { includeNewlineAtEnd: false, prettier: { parser: "vue", }, }); return formattedInput.endsWith(";") ? formattedInput.substring(0, formattedInput.length - 1) : formattedInput; }; export const snippetRead: SnippetReadHook = async ( data, { helpers }, ) => { const { fieldPath, itemName } = data; const label = "Vue"; switch (data.model.type) { case "StructuredText": { return [ { label: `${label} (rich)`, language: "vue", code: await format( stripIndent` `, helpers, ), }, { label: `${label} (plain)`, language: "vue", code: await format( stripIndent` `, helpers, ), }, ]; } case "Link": { const repeat = data.model.config?.repeat ?? false; const allowText = data.model.config?.allowText ?? false; const allowVariants = Boolean(data.model.config?.variants); const variant = (path: string) => allowVariants ? ` :class="${path}.variant"` : ""; const path = dotPath(fieldPath); let codeText; if (!repeat && !allowText) { codeText = stripIndent` Link `; } else if (!repeat && allowText) { codeText = stripIndent` `; } else if (repeat && !allowText) { codeText = stripIndent` `; } else if (repeat && allowText) { codeText = stripIndent` `; } else { throw new Error("Invalid configuration."); } return { label, language: "vue", code: await format(codeText, helpers), }; } case "Image": { return { label, language: "vue", code: await format( stripIndent` `, helpers, ), }; } case "Table": { return { label, language: "vue", code: await format( stripIndent` `, helpers, ), }; } case "Embed": { return { label, language: "vue", code: await format( stripIndent` `, helpers, ), }; } case "Group": { return { label, language: "vue", code: await format( stripIndent` `, helpers, ), }; } case "Slices": { const code = await format( stripIndent` `, helpers, ); return { label, language: "vue", code, }; } default: { return { label, language: "vue", code: await format( stripIndent` {{${dotPath(fieldPath)}}} `, helpers, ), }; } } };