Skip to content

Commit

Permalink
Merge pull request #1491 from prismicio/dani/repetable-link-code-snip…
Browse files Browse the repository at this point in the history
…pets
  • Loading branch information
dani-mp authored Nov 25, 2024
2 parents 899f84b + 3bc1118 commit dd75569
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
30 changes: 18 additions & 12 deletions packages/adapter-next/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,31 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
const repeat = data.model.config?.repeat ?? false;
const allowText = data.model.config?.allowText ?? false;

let codeText;
let code;
if (!repeat && !allowText) {
codeText = stripIndent`
code = await format(
stripIndent`
<PrismicNextLink field={${dotPath(fieldPath)}}>Link</PrismicNextLink>
`;
`,
helpers,
);
} else if (!repeat && allowText) {
codeText = stripIndent`
code = await format(
stripIndent`
<PrismicNextLink field={${dotPath(fieldPath)}} />
`;
`,
helpers,
);
} else if (repeat && !allowText) {
codeText = stripIndent`
{${dotPath(fieldPath)}.map((link, index) => (
<PrismicNextLink key={index} field={link}>Link</PrismicNextLink>
code = stripIndent`
{${dotPath(fieldPath)}.map((link) => (
<PrismicNextLink key={link.key} field={link}>Link</PrismicNextLink>
))}
`;
} else if (repeat && allowText) {
codeText = stripIndent`
{${dotPath(fieldPath)}.map((link, index) => (
<PrismicNextLink key={index} field={link} />
code = stripIndent`
{${dotPath(fieldPath)}.map((link) => (
<PrismicNextLink key={link.key} field={link} />
))}
`;
} else {
Expand All @@ -90,7 +96,7 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
return {
label,
language: "tsx",
code: await format(codeText, helpers),
code,
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-nuxt/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
`;
} else if (repeat && !allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<template v-for="link in ${dotPath(fieldPath)}" :key="link.key">
<PrismicLink :field="link">Link</PrismicLink>
</template>
`;
} else if (repeat && allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<template v-for="link in ${dotPath(fieldPath)}" :key="link.key">
<PrismicLink :field="link" />
</template>
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-nuxt2/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
`;
} else if (repeat && !allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<template v-for="link in ${dotPath(fieldPath)}" :key="link.key">
<PrismicLink :field="link">Link</PrismicLink>
</template>
`;
} else if (repeat && allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<template v-for="link in ${dotPath(fieldPath)}" :key="link.key">
<PrismicLink :field="link" />
</template>
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-sveltekit/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
`;
} else if (repeat && !allowText) {
codeText = stripIndent`
{#each ${dotPath(fieldPath)} as link, index (index)}
{#each ${dotPath(fieldPath)} as link (link.key)}
<PrismicLink field={link}>Link</PrismicLink>
{/each}
`;
} else if (repeat && allowText) {
codeText = stripIndent`
{#each ${dotPath(fieldPath)} as link, index (index)}
{#each ${dotPath(fieldPath)} as link (link.key)}
<PrismicLink field={link} />
{/each}
`;
Expand Down

0 comments on commit dd75569

Please sign in to comment.