|
1
|
# Tutorial MDX Conventions |
|
2
|
|
|
3
|
Reference for MDX structure, components, and patterns used in Medusa tutorials. |
|
4
|
|
|
5
|
## Frontmatter |
|
6
|
|
|
7
|
```yaml |
|
8
|
--- |
|
9
|
sidebar_label: "Feature Name" |
|
10
|
tags: |
|
11
|
- name: module-name |
|
12
|
label: "Implement Feature Name" |
|
13
|
- server |
|
14
|
- tutorial |
|
15
|
products: |
|
16
|
- product |
|
17
|
- customer |
|
18
|
--- |
|
19
|
``` |
|
20
|
|
|
21
|
- `sidebar_label`: Short display name for the sidebar |
|
22
|
- `tags`: Mix of string tags (`server`, `tutorial`) and object tags (`{name, label}`) for cross-linking |
|
23
|
- `products`: Medusa Commerce Modules covered (product, cart, order, customer, promotion, payment, etc.) |
|
24
|
|
|
25
|
For integration guides, add the service type as a tag string (e.g., `analytics`, `notification`, `cms`). |
|
26
|
|
|
27
|
## Required Imports and Metadata |
|
28
|
|
|
29
|
```mdx |
|
30
|
import { Github, PlaySolid } from "@medusajs/icons" |
|
31
|
import { Prerequisites, WorkflowDiagram, CardList } from "docs-ui" |
|
32
|
|
|
33
|
export const metadata = { |
|
34
|
title: `Implement X in Medusa`, |
|
35
|
} |
|
36
|
``` |
|
37
|
|
|
38
|
## Standard Section Order |
|
39
|
|
|
40
|
```text |
|
41
|
# {metadata.title} |
|
42
|
|
|
43
|
[Intro paragraph] |
|
44
|
|
|
45
|
## Summary |
|
46
|
|
|
47
|
[Bullet list] |
|
48
|
|
|
49
|
You can follow this tutorial whether you're new to Medusa or an advanced Medusa developer. |
|
50
|
|
|
51
|
 |
|
52
|
|
|
53
|
<CardList items={[...]} /> |
|
54
|
|
|
55
|
--- |
|
56
|
|
|
57
|
## Step 1: Install a Medusa Application |
|
58
|
[pre-written template — see below] |
|
59
|
|
|
60
|
## Step 2: ... |
|
61
|
... |
|
62
|
|
|
63
|
## Next Steps |
|
64
|
|
|
65
|
### Troubleshooting |
|
66
|
|
|
67
|
### Getting Help |
|
68
|
``` |
|
69
|
|
|
70
|
Note: `## Troubleshooting` and `## Getting Help` are **subsections** (`###`) under `## Next Steps`, not top-level sections. |
|
71
|
|
|
72
|
## Pre-Written Step 1 Template (Always Use Verbatim) |
|
73
|
|
|
74
|
Copy this exactly — do not rewrite it: |
|
75
|
|
|
76
|
````mdx |
|
77
|
## Step 1: Install a Medusa Application |
|
78
|
|
|
79
|
<Prerequisites items={[ |
|
80
|
{ |
|
81
|
text: "Node.js v20+", |
|
82
|
link: "https://nodejs.org/en/download" |
|
83
|
}, |
|
84
|
{ |
|
85
|
text: "Git CLI tool", |
|
86
|
link: "https://git-scm.com/downloads" |
|
87
|
}, |
|
88
|
{ |
|
89
|
text: "PostgreSQL", |
|
90
|
link: "https://www.postgresql.org/download/" |
|
91
|
} |
|
92
|
]} /> |
|
93
|
|
|
94
|
Start by installing the Medusa application on your machine with the following command: |
|
95
|
|
|
96
|
```bash npx2yarnExec |
|
97
|
npx create-medusa-app@latest |
|
98
|
``` |
|
99
|
|
|
100
|
You'll first be asked for the project's name. Then, when asked whether you want to install the [Next.js Starter Storefront](../../../nextjs-starter/page.mdx), choose Yes. |
|
101
|
|
|
102
|
Afterward, the installation process will start, which will install the Medusa application in a directory with your project's name, and the Next.js Starter Storefront in a separate directory with the `{project-name}-storefront` name. |
|
103
|
|
|
104
|
<Note title="Why is the storefront installed separately"> |
|
105
|
|
|
106
|
The Medusa application is composed of a headless Node.js server and an admin dashboard. The storefront is installed or custom-built separately and connects to the Medusa application through its REST endpoints, called [API routes](!docs!/learn/fundamentals/api-routes). Learn more in [Medusa's Architecture documentation](!docs!/learn/introduction/architecture). |
|
107
|
|
|
108
|
</Note> |
|
109
|
|
|
110
|
Once the installation finishes successfully, the Medusa Admin dashboard will open with a form to create a new user. Enter the user's credentials and submit the form. Afterward, you can log in with the new user and explore the dashboard. |
|
111
|
|
|
112
|
<Note title="Ran into Errors"> |
|
113
|
|
|
114
|
Check out the [troubleshooting guides](../../../troubleshooting/create-medusa-app-errors/page.mdx) for help. |
|
115
|
|
|
116
|
</Note> |
|
117
|
|
|
118
|
--- |
|
119
|
```` |
|
120
|
|
|
121
|
For integration guides where a storefront may not be needed, omit the storefront-related sentences and the "Why is the storefront installed separately" note. |
|
122
|
|
|
123
|
## Intro Paragraph Pattern |
|
124
|
|
|
125
|
``` |
|
126
|
In this tutorial, you'll learn how to implement [X] in Medusa. |
|
127
|
|
|
128
|
When you install a Medusa application, you get a fully-fledged commerce platform with a Framework for customization. The Medusa application's commerce features are built around [Commerce Modules](../../../commerce-modules/page.mdx) which are available out-of-the-box. |
|
129
|
|
|
130
|
Medusa doesn't provide [X] out-of-the-box, but the Medusa Framework facilitates implementing customizations like [X]. In this tutorial, you'll learn how to customize the Medusa server[, Admin dashboard, and Next.js Starter Storefront] to implement [X]. |
|
131
|
|
|
132
|
You can follow this guide whether you're new to Medusa or an advanced Medusa developer. |
|
133
|
``` |
|
134
|
|
|
135
|
## Summary Section Pattern |
|
136
|
|
|
137
|
```mdx |
|
138
|
## Summary |
|
139
|
|
|
140
|
By following this tutorial, you'll learn how to: |
|
141
|
|
|
142
|
- Install and set up Medusa. |
|
143
|
- [Main feature: e.g., Define product review models and implement their management features in the Medusa server.] |
|
144
|
- [Admin: e.g., Customize the Medusa Admin to allow merchants to view and manage product reviews.] |
|
145
|
- [Storefront: e.g., Customize the Next.js Starter Storefront to display and submit product reviews.] |
|
146
|
``` |
|
147
|
|
|
148
|
## CardList (GitHub + OpenAPI) |
|
149
|
|
|
150
|
```mdx |
|
151
|
<CardList items={[ |
|
152
|
{ |
|
153
|
href: "https://github.com/medusajs/examples/tree/main/{example-name}", |
|
154
|
title: "{Feature Name} Repository", |
|
155
|
text: "Find the full code for this guide in this repository.", |
|
156
|
icon: Github, |
|
157
|
}, |
|
158
|
{ |
|
159
|
href: "https://res.cloudinary.com/dza7lstvk/raw/upload/v.../OpenApi/{name}.yaml", |
|
160
|
title: "OpenApi Specs for Postman", |
|
161
|
text: "Import this OpenApi Specs file into tools like Postman.", |
|
162
|
icon: PlaySolid, |
|
163
|
}, |
|
164
|
]} /> |
|
165
|
``` |
|
166
|
|
|
167
|
If no OpenAPI specs exist, omit the second card. |
|
168
|
|
|
169
|
## Components |
|
170
|
|
|
171
|
### Prerequisites |
|
172
|
Use only at Step 1: |
|
173
|
```mdx |
|
174
|
<Prerequisites items={[ |
|
175
|
{ text: "Node.js v20+", link: "https://nodejs.org/en/download" }, |
|
176
|
{ text: "Git CLI tool", link: "https://git-scm.com/downloads" }, |
|
177
|
{ text: "PostgreSQL", link: "https://www.postgresql.org/download/" } |
|
178
|
]} /> |
|
179
|
``` |
|
180
|
|
|
181
|
Add extra prerequisites for integration guides (e.g., third-party account, API key). |
|
182
|
|
|
183
|
### WorkflowDiagram |
|
184
|
Place immediately after introducing a workflow: |
|
185
|
```mdx |
|
186
|
<WorkflowDiagram |
|
187
|
workflow={{ |
|
188
|
name: "myWorkflowName", |
|
189
|
steps: [ |
|
190
|
{ |
|
191
|
type: "step", |
|
192
|
name: "stepName", |
|
193
|
description: "What this step does.", |
|
194
|
depth: 1, |
|
195
|
}, |
|
196
|
{ |
|
197
|
type: "when", |
|
198
|
condition: "If condition is true", |
|
199
|
depth: 2, |
|
200
|
steps: [ |
|
201
|
{ type: "step", name: "conditionalStep", description: "...", depth: 3 }, |
|
202
|
], |
|
203
|
}, |
|
204
|
], |
|
205
|
}} |
|
206
|
/> |
|
207
|
``` |
|
208
|
|
|
209
|
### Note |
|
210
|
```mdx |
|
211
|
<Note> |
|
212
|
Plain note with no title. |
|
213
|
</Note> |
|
214
|
|
|
215
|
<Note title="Why is this needed?"> |
|
216
|
Note with a custom title. |
|
217
|
</Note> |
|
218
|
``` |
|
219
|
|
|
220
|
## Code Block Attributes |
|
221
|
|
|
222
|
``` |
|
223
|
```ts title="src/modules/review/models/product-review.ts" |
|
224
|
// backend code |
|
225
|
``` |
|
226
|
|
|
227
|
```tsx title="src/app/products/[id]/page.tsx" badgeLabel="Storefront" badgeColor="blue" |
|
228
|
// storefront code |
|
229
|
``` |
|
230
|
|
|
231
|
```ts title="src/workflows/create-review.ts" highlights={createReviewHighlights} |
|
232
|
// workflow code with line highlights |
|
233
|
``` |
|
234
|
|
|
235
|
```bash npm2yarn |
|
236
|
npm install some-package |
|
237
|
``` |
|
238
|
|
|
239
|
```bash npx2yarnExec |
|
240
|
npx create-medusa-app@latest |
|
241
|
``` |
|
242
|
``` |
|
243
|
|
|
244
|
**Badge conventions:** |
|
245
|
- `badgeLabel="Storefront"` + `badgeColor="blue"` — Next.js storefront files |
|
246
|
- `badgeLabel="Medusa Application"` + `badgeColor="green"` — backend files (only when both appear close together) |
|
247
|
|
|
248
|
## Highlights Arrays |
|
249
|
|
|
250
|
Define highlights as exported constants before the MDX body: |
|
251
|
|
|
252
|
```ts |
|
253
|
export const workflowHighlights = [ |
|
254
|
["5", "input", "The workflow receives the review data as input."], |
|
255
|
["10", "product_id", "Retrieve the product to validate it exists."], |
|
256
|
["18", "review", "Create the product review record."], |
|
257
|
] |
|
258
|
``` |
|
259
|
|
|
260
|
Format: `[lineNumber, keyword/identifier, description]` |
|
261
|
|
|
262
|
Reference in code block: `highlights={workflowHighlights}` |
|
263
|
|
|
264
|
## Service Method Sub-Steps Pattern |
|
265
|
|
|
266
|
When a step requires implementing multiple methods on a service class (common in module provider integrations), each method gets its own lettered sub-step under the parent step. Structure: |
|
267
|
|
|
268
|
1. First sub-step: create the service file with the class scaffold (no methods yet), then explain the class structure below the code block |
|
269
|
2. Subsequent sub-steps: one per method — explain what it does, show the code adding that method to the class, then explain implementation details |
|
270
|
|
|
271
|
**Sub-step heading format:** `### {letter}. Implement {methodName} Method` |
|
272
|
|
|
273
|
**Example structure for a service step:** |
|
274
|
|
|
275
|
````mdx |
|
276
|
## Step 3: Create Mailchimp Module Provider |
|
277
|
|
|
278
|
[concept definition + module creation context] |
|
279
|
|
|
280
|
### a. Create Module Directory |
|
281
|
|
|
282
|
[directory creation instruction] |
|
283
|
|
|
284
|
### b. Create Service |
|
285
|
|
|
286
|
[1-2 sentences on what the service does] |
|
287
|
|
|
288
|
Create the file `src/modules/mailchimp/service.ts` with the following content: |
|
289
|
|
|
290
|
```ts title="src/modules/mailchimp/service.ts" highlights={serviceHighlights} |
|
291
|
// class scaffold with constructor only, no method implementations yet |
|
292
|
``` |
|
293
|
|
|
294
|
[Explanation of class structure: what it extends, static `identifier`, constructor params and what each option means] |
|
295
|
|
|
296
|
In the next sections, you'll implement the methods of the `MailchimpNotificationProviderService` class. |
|
297
|
|
|
298
|
### c. Implement validateOptions Method |
|
299
|
|
|
300
|
The `validateOptions` method is used to validate the options passed to the module provider. If the method throws an error, the Medusa application won't start. |
|
301
|
|
|
302
|
Add the `validateOptions` method to the `MailchimpNotificationProviderService` class: |
|
303
|
|
|
304
|
```ts title="src/modules/mailchimp/service.ts" |
|
305
|
class MailchimpNotificationProviderService extends AbstractNotificationProviderService { |
|
306
|
// ... |
|
307
|
validateOptions(options: Options) { |
|
308
|
// ... |
|
309
|
} |
|
310
|
} |
|
311
|
``` |
|
312
|
|
|
313
|
[Explain what the method does and any important implementation details] |
|
314
|
|
|
315
|
### d. Implement send Method |
|
316
|
|
|
317
|
When the Medusa application needs to send a notification, it calls the `send` method of the module provider. |
|
318
|
|
|
319
|
[Continue with remaining methods...] |
|
320
|
```` |
|
321
|
|
|
322
|
**Key rules:** |
|
323
|
- Show only the new method being added in each sub-step's code block — use `// ...` to omit already-shown code |
|
324
|
- Always add a transition sentence like "In the next sections, you'll implement the methods of the `XService` class." after introducing the scaffold |
|
325
|
- Helper/private methods that support a required method can be nested as `####` sub-sub-steps under the parent method's `###` sub-step |
|
326
|
- The final sub-step of a service step is always exporting the module definition (`### {letter}. Export Module Definition`) |
|
327
|
|
|
328
|
## Test it Out Sections |
|
329
|
|
|
330
|
Every major step (API routes, subscribers, admin UI, storefront) must end with a test section: |
|
331
|
|
|
332
|
```mdx |
|
333
|
### Test it Out |
|
334
|
|
|
335
|
To test the [feature], send a request to the [endpoint]: |
|
336
|
|
|
337
|
```bash |
|
338
|
curl -X POST http://localhost:9000/store/products/{id}/reviews \ |
|
339
|
-H "Content-Type: application/json" \ |
|
340
|
--data-raw '{ |
|
341
|
"rating": 5, |
|
342
|
"content": "Great product!" |
|
343
|
}' |
|
344
|
``` |
|
345
|
|
|
346
|
If successful, you'll receive a response similar to: |
|
347
|
|
|
348
|
```json |
|
349
|
{ |
|
350
|
"review": { |
|
351
|
"id": "review_123", |
|
352
|
"rating": 5, |
|
353
|
"content": "Great product!" |
|
354
|
} |
|
355
|
} |
|
356
|
``` |
|
357
|
``` |
|
358
|
|
|
359
|
For Admin UI steps, describe where to navigate and what to look for. For storefront steps, describe the user interaction. |
|
360
|
|
|
361
|
## Next Steps, Troubleshooting, and Getting Help (Verbatim Template) |
|
362
|
|
|
363
|
Use this structure exactly. Adjust the bullet points in `## Next Steps` to match the tutorial's topic. |
|
364
|
|
|
365
|
`### Troubleshooting` and `### Getting Help` are subsections of `## Next Steps` — do not promote them to `##`. |
|
366
|
|
|
367
|
````mdx |
|
368
|
## Next Steps |
|
369
|
|
|
370
|
You've now implemented [X] in Medusa. You can expand on this feature to add more features like: |
|
371
|
|
|
372
|
- [Idea 1, e.g., Automated emails to customers when they reach a new tier.] |
|
373
|
- [Idea 2, e.g., More complex rules based on product categories or collections.] |
|
374
|
- [Idea 3, e.g., Other privileges, such as early access to new products or free shipping.] |
|
375
|
|
|
376
|
If you're new to Medusa, check out the [main documentation](!docs!/learn) for a more in-depth understanding of the concepts you've used in this guide and more. |
|
377
|
|
|
378
|
To learn more about the commerce features Medusa provides, check out [Commerce Modules](../../../commerce-modules/page.mdx). |
|
379
|
|
|
380
|
### Troubleshooting |
|
381
|
|
|
382
|
If you encounter issues during your development, check out the [troubleshooting guides](../../../troubleshooting/page.mdx). |
|
383
|
|
|
384
|
### Getting Help |
|
385
|
|
|
386
|
If you encounter issues not covered in the troubleshooting guides: |
|
387
|
|
|
388
|
1. Visit the [Medusa GitHub repository](https://github.com/medusajs/medusa) to report issues or ask questions. |
|
389
|
2. Join the [Medusa Discord community](https://discord.gg/medusajs) for real-time support from community members. |
|
390
|
```` |