@slicemachine/adapter-next 0.3.3-dev-next-release.2 → 0.3.3-dev-next-release.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/documentation-read.cjs +377 -0
- package/dist/hooks/documentation-read.cjs.map +1 -0
- package/dist/hooks/documentation-read.d.ts +3 -0
- package/dist/hooks/documentation-read.js +377 -0
- package/dist/hooks/documentation-read.js.map +1 -0
- package/dist/plugin.cjs +3 -1
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.js +3 -1
- package/dist/plugin.js.map +1 -1
- package/package.json +3 -3
- package/src/hooks/documentation-read.ts +395 -0
- package/src/plugin.ts +3 -2
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { stripIndent, source } from "common-tags";
|
|
2
|
+
|
|
3
|
+
import type { DocumentationReadHook } from "@slicemachine/plugin-kit";
|
|
4
|
+
|
|
5
|
+
import type { PluginOptions } from "../types";
|
|
6
|
+
import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
|
|
7
|
+
import { getJSFileExtension } from "../lib/getJSFileExtension";
|
|
8
|
+
|
|
9
|
+
export const documentationRead: DocumentationReadHook<PluginOptions> = async (
|
|
10
|
+
data,
|
|
11
|
+
{ options, helpers },
|
|
12
|
+
) => {
|
|
13
|
+
const isTypeScriptProject = await checkIsTypeScriptProject({
|
|
14
|
+
helpers,
|
|
15
|
+
options,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
if (data.kind === "PageSnippet") {
|
|
19
|
+
const { model } = data.data;
|
|
20
|
+
const extension = await getJSFileExtension({ helpers, options, jsx: true });
|
|
21
|
+
|
|
22
|
+
const appFilePath = `${
|
|
23
|
+
model.repeatable ? "[uid]" : model.id
|
|
24
|
+
}/page.${extension}`;
|
|
25
|
+
const pagesFilePath = `${
|
|
26
|
+
model.repeatable ? "[uid]" : model.id
|
|
27
|
+
}.${extension}`;
|
|
28
|
+
|
|
29
|
+
let appFileContent: string;
|
|
30
|
+
let pagesFileContent: string;
|
|
31
|
+
if (isTypeScriptProject) {
|
|
32
|
+
if (model.repeatable) {
|
|
33
|
+
appFileContent = stripIndent`
|
|
34
|
+
import { Metadata } from "next";
|
|
35
|
+
import { notFound } from "next/navigation";
|
|
36
|
+
import { SliceZone } from "@prismicio/react";
|
|
37
|
+
|
|
38
|
+
import { createClient } from "@/prismicio";
|
|
39
|
+
import { components } from "@/slices";
|
|
40
|
+
|
|
41
|
+
type Params = { uid: string };
|
|
42
|
+
|
|
43
|
+
export const dynamicParams = false;
|
|
44
|
+
|
|
45
|
+
export default async function Page({ params }: { params: Params }) {
|
|
46
|
+
const client = createClient();
|
|
47
|
+
const page = await client
|
|
48
|
+
.getByUID("${model.id}", params.uid)
|
|
49
|
+
.catch(() => notFound());
|
|
50
|
+
|
|
51
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function generateMetadata({
|
|
55
|
+
params,
|
|
56
|
+
}: {
|
|
57
|
+
params: Params;
|
|
58
|
+
}): Promise<Metadata> {
|
|
59
|
+
const client = createClient();
|
|
60
|
+
const page = await client.getByUID("${model.id}", params.uid);
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
title: page.data.meta_title,
|
|
64
|
+
description: page.data.meta_description,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function generateStaticParams() {
|
|
69
|
+
const client = createClient();
|
|
70
|
+
const pages = await client.getAllByType("${model.id}");
|
|
71
|
+
|
|
72
|
+
return pages.map((page) => {
|
|
73
|
+
return { uid: page.uid };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
`;
|
|
77
|
+
pagesFileContent = stripIndent`
|
|
78
|
+
import { GetStaticPropsContext, InferGetStaticPropsType } from "next";
|
|
79
|
+
import Head from "next/head";
|
|
80
|
+
import { isFilled, asLink } from "@prismicio/client";
|
|
81
|
+
import { SliceZone } from "@prismicio/react";
|
|
82
|
+
|
|
83
|
+
import { components } from "@/slices";
|
|
84
|
+
import { createClient } from "@/prismicio";
|
|
85
|
+
|
|
86
|
+
type Params = { uid: string };
|
|
87
|
+
|
|
88
|
+
export default function Page({
|
|
89
|
+
page,
|
|
90
|
+
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
|
91
|
+
return (
|
|
92
|
+
<>
|
|
93
|
+
<Head>
|
|
94
|
+
<title>{page.data.meta_title}</title>
|
|
95
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
96
|
+
<meta name="description" content={page.data.meta_description} />
|
|
97
|
+
) : null}
|
|
98
|
+
</Head>
|
|
99
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
100
|
+
</>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function getStaticProps({
|
|
105
|
+
params,
|
|
106
|
+
previewData,
|
|
107
|
+
}: GetStaticPropsContext<Params>) {
|
|
108
|
+
// The \`previewData\` parameter allows your app to preview
|
|
109
|
+
// drafts from the Page Builder.
|
|
110
|
+
const client = createClient({ previewData });
|
|
111
|
+
|
|
112
|
+
const page = await client.getByUID("${model.id}", params!.uid);
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
props: { page },
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export async function getStaticPaths() {
|
|
120
|
+
const client = createClient();
|
|
121
|
+
|
|
122
|
+
const pages = await client.getAllByType("${model.id}");
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
paths: pages.map((page) => {
|
|
126
|
+
return asLink(page);
|
|
127
|
+
}),
|
|
128
|
+
fallback: false,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
`;
|
|
132
|
+
} else {
|
|
133
|
+
appFileContent = stripIndent`
|
|
134
|
+
import { Metadata } from "next";
|
|
135
|
+
import { SliceZone } from "@prismicio/react";
|
|
136
|
+
|
|
137
|
+
import { createClient } from "@/prismicio";
|
|
138
|
+
import { components } from "@/slices";
|
|
139
|
+
|
|
140
|
+
export default async function Page() {
|
|
141
|
+
const client = createClient();
|
|
142
|
+
const page = await client.getSingle("${model.id}");
|
|
143
|
+
|
|
144
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export async function generateMetadata(): Promise<Metadata> {
|
|
148
|
+
const client = createClient();
|
|
149
|
+
const page = await client.getSingle("${model.id}");
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
title: page.data.meta_title,
|
|
153
|
+
description: page.data.meta_description,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
`;
|
|
157
|
+
pagesFileContent = stripIndent`
|
|
158
|
+
import { GetStaticPropsContext, InferGetStaticPropsType } from "next";
|
|
159
|
+
import Head from "next/head";
|
|
160
|
+
import { isFilled } from "@prismicio/client";
|
|
161
|
+
import { SliceZone } from "@prismicio/react";
|
|
162
|
+
|
|
163
|
+
import { components } from "@/slices";
|
|
164
|
+
import { createClient } from "@/prismicio";
|
|
165
|
+
|
|
166
|
+
export default function Page({
|
|
167
|
+
page,
|
|
168
|
+
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
|
169
|
+
return (
|
|
170
|
+
<>
|
|
171
|
+
<Head>
|
|
172
|
+
<title>{page.data.meta_title}</title>
|
|
173
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
174
|
+
<meta name="description" content={page.data.meta_description} />
|
|
175
|
+
) : null}
|
|
176
|
+
</Head>
|
|
177
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
178
|
+
</>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export async function getStaticProps({ previewData }: GetStaticPropsContext) {
|
|
183
|
+
// The \`previewData\` parameter allows your app to preview
|
|
184
|
+
// drafts from the Page Builder.
|
|
185
|
+
const client = createClient({ previewData });
|
|
186
|
+
|
|
187
|
+
// The query fetches the page's data based on the current URL.
|
|
188
|
+
const page = await client.getSingle("${model.id}");
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
props: { page },
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
`;
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
if (model.repeatable) {
|
|
198
|
+
appFileContent = stripIndent`
|
|
199
|
+
import { notFound } from "next/navigation";
|
|
200
|
+
import { SliceZone } from "@prismicio/react";
|
|
201
|
+
|
|
202
|
+
import { createClient } from "@/prismicio";
|
|
203
|
+
import { components } from "@/slices";
|
|
204
|
+
|
|
205
|
+
export const dynamicParams = false;
|
|
206
|
+
|
|
207
|
+
export default async function Page({ params }) {
|
|
208
|
+
const client = createClient();
|
|
209
|
+
const page = await client
|
|
210
|
+
.getByUID("${model.id}", params.uid)
|
|
211
|
+
.catch(() => notFound());
|
|
212
|
+
|
|
213
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export async function generateMetadata({ params }) {
|
|
217
|
+
const client = createClient();
|
|
218
|
+
const page = await client.getByUID("${model.id}", params.uid);
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
title: page.data.meta_title,
|
|
222
|
+
description: page.data.meta_description,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export async function generateStaticParams() {
|
|
227
|
+
const client = createClient();
|
|
228
|
+
const pages = await client.getAllByType("${model.id}");
|
|
229
|
+
|
|
230
|
+
return pages.map((page) => {
|
|
231
|
+
return { uid: page.uid };
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
`;
|
|
235
|
+
pagesFileContent = stripIndent`
|
|
236
|
+
import Head from "next/head";
|
|
237
|
+
import { isFilled, asLink } from "@prismicio/client";
|
|
238
|
+
import { SliceZone } from "@prismicio/react";
|
|
239
|
+
|
|
240
|
+
import { components } from "@/slices";
|
|
241
|
+
import { createClient } from "@/prismicio";
|
|
242
|
+
|
|
243
|
+
export default function Page({ page }) {
|
|
244
|
+
return (
|
|
245
|
+
<>
|
|
246
|
+
<Head>
|
|
247
|
+
<title>{page.data.meta_title}</title>
|
|
248
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
249
|
+
<meta name="description" content={page.data.meta_description} />
|
|
250
|
+
) : null}
|
|
251
|
+
</Head>
|
|
252
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
253
|
+
</>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export async function getStaticProps({
|
|
258
|
+
params,
|
|
259
|
+
previewData,
|
|
260
|
+
}) {
|
|
261
|
+
// The \`previewData\` parameter allows your app to preview
|
|
262
|
+
// drafts from the Page Builder.
|
|
263
|
+
const client = createClient({ previewData });
|
|
264
|
+
|
|
265
|
+
const page = await client.getByUID("${model.id}", params.uid);
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
props: { page },
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export async function getStaticPaths() {
|
|
273
|
+
const client = createClient();
|
|
274
|
+
|
|
275
|
+
const pages = await client.getAllByType("${model.id}");
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
paths: pages.map((page) => {
|
|
279
|
+
return asLink(page);
|
|
280
|
+
}),
|
|
281
|
+
fallback: false,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
`;
|
|
285
|
+
} else {
|
|
286
|
+
appFileContent = stripIndent`
|
|
287
|
+
import { SliceZone } from "@prismicio/react";
|
|
288
|
+
|
|
289
|
+
import { createClient } from "@/prismicio";
|
|
290
|
+
import { components } from "@/slices";
|
|
291
|
+
|
|
292
|
+
export default async function Page() {
|
|
293
|
+
const client = createClient();
|
|
294
|
+
const page = await client.getSingle("${model.id}");
|
|
295
|
+
|
|
296
|
+
return <SliceZone slices={page.data.slices} components={components} />;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export async function generateMetadata() {
|
|
300
|
+
const client = createClient();
|
|
301
|
+
const page = await client.getSingle("${model.id}");
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
title: page.data.meta_title,
|
|
305
|
+
description: page.data.meta_description,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
`;
|
|
309
|
+
pagesFileContent = stripIndent`
|
|
310
|
+
import Head from "next/head";
|
|
311
|
+
import { isFilled } from "@prismicio/client";
|
|
312
|
+
import { SliceZone } from "@prismicio/react";
|
|
313
|
+
|
|
314
|
+
import { components } from "@/slices";
|
|
315
|
+
import { createClient } from "@/prismicio";
|
|
316
|
+
|
|
317
|
+
export default function Page({ page }) {
|
|
318
|
+
return (
|
|
319
|
+
<>
|
|
320
|
+
<Head>
|
|
321
|
+
<title>{page.data.meta_title}</title>
|
|
322
|
+
{isFilled.keyText(page.data.meta_description) ? (
|
|
323
|
+
<meta name="description" content={page.data.meta_description} />
|
|
324
|
+
) : null}
|
|
325
|
+
</Head>
|
|
326
|
+
<SliceZone slices={page.data.slices} components={components} />
|
|
327
|
+
</>
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export async function getStaticProps({ previewData }) {
|
|
332
|
+
// The \`previewData\` parameter allows your app to preview
|
|
333
|
+
// drafts from the Page Builder.
|
|
334
|
+
const client = createClient({ previewData });
|
|
335
|
+
|
|
336
|
+
// The query fetches the page's data based on the current URL.
|
|
337
|
+
const page = await client.getSingle("${model.id}");
|
|
338
|
+
|
|
339
|
+
return {
|
|
340
|
+
props: { page },
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
`;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (options.format) {
|
|
348
|
+
appFileContent = await helpers.format(
|
|
349
|
+
appFileContent,
|
|
350
|
+
helpers.joinPathFromRoot(`index.${extension}`),
|
|
351
|
+
{
|
|
352
|
+
includeNewlineAtEnd: false,
|
|
353
|
+
},
|
|
354
|
+
);
|
|
355
|
+
pagesFileContent = await helpers.format(
|
|
356
|
+
pagesFileContent,
|
|
357
|
+
helpers.joinPathFromRoot(`index.${extension}`),
|
|
358
|
+
{
|
|
359
|
+
includeNewlineAtEnd: false,
|
|
360
|
+
},
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return [
|
|
365
|
+
{
|
|
366
|
+
label: "App Router",
|
|
367
|
+
content: source`
|
|
368
|
+
## Create your ${model.label}'s page component
|
|
369
|
+
|
|
370
|
+
Add a new route by creating an \`app/${appFilePath}\` file. (If the route should be nested in a child directory, you can create the file in a directory, like \`app/marketing/${appFilePath}\`.)
|
|
371
|
+
|
|
372
|
+
Paste in this code:
|
|
373
|
+
|
|
374
|
+
${`~~~${extension} [app/${appFilePath}]\n${appFileContent}\n~~~`}
|
|
375
|
+
|
|
376
|
+
Make sure all of your import paths are correct. See the [install guide](https://prismic.io/docs/setup-nextjs) for more information.
|
|
377
|
+
`,
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
label: "Page Router",
|
|
381
|
+
content: source`
|
|
382
|
+
## Create your ${model.label}'s page component
|
|
383
|
+
|
|
384
|
+
Add a new route by creating a \`pages/${pagesFilePath}\` file. (If the route should be nested in a child directory, you can create the file in a directory, like \`pages/marketing/${pagesFilePath}\`.)
|
|
385
|
+
|
|
386
|
+
${`~~~${extension} [pages/${pagesFilePath}]\n${pagesFileContent}\n~~~`}
|
|
387
|
+
|
|
388
|
+
Make sure all of your import paths are correct. See the [install guide](https://prismic.io/docs/setup-nextjs) for more information.
|
|
389
|
+
`,
|
|
390
|
+
},
|
|
391
|
+
];
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return [];
|
|
395
|
+
};
|
package/src/plugin.ts
CHANGED
|
@@ -21,9 +21,10 @@ import { sliceDelete } from "./hooks/slice-delete";
|
|
|
21
21
|
import { sliceLibraryRead } from "./hooks/sliceLibrary-read";
|
|
22
22
|
import { sliceRead } from "./hooks/slice-read";
|
|
23
23
|
import { sliceRename } from "./hooks/slice-rename";
|
|
24
|
-
import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read";
|
|
25
24
|
import { sliceUpdate } from "./hooks/slice-update";
|
|
26
25
|
import { snippetRead } from "./hooks/snippet-read";
|
|
26
|
+
import { documentationRead } from "./hooks/documentation-read";
|
|
27
|
+
import { sliceSimulatorSetupRead } from "./hooks/sliceSimulator-setup-read";
|
|
27
28
|
|
|
28
29
|
export const plugin = defineSliceMachinePlugin<PluginOptions>({
|
|
29
30
|
meta: {
|
|
@@ -57,7 +58,7 @@ export const plugin = defineSliceMachinePlugin<PluginOptions>({
|
|
|
57
58
|
hook("custom-type-library:read", customTypeLibraryRead);
|
|
58
59
|
|
|
59
60
|
hook("snippet:read", snippetRead);
|
|
60
|
-
|
|
61
|
+
hook("documentation:read", documentationRead);
|
|
61
62
|
hook("slice-simulator:setup:read", sliceSimulatorSetupRead);
|
|
62
63
|
},
|
|
63
64
|
});
|