|
1 |
| -# First Pages >> Use JavaScript ||40 |
| 1 | +# First Pages >> Use JavaScript || 40 |
2 | 2 |
|
3 |
| -You can use `js script` to execute JavaScript (`type="module"`). |
| 3 | +If you would like to add JavaScript to a page, you can do it inline using the `script` markdown directive. The script you write runs on the page as a module. |
4 | 4 |
|
5 |
| -```` |
| 5 | +<!-- prettier-ignore-start --> |
| 6 | +~~~markdown |
6 | 7 | ```js script
|
7 |
| -console.log('foo'); |
| 8 | +const message = 'Hello, World!'; |
| 9 | +console.log(message); |
8 | 10 | ```
|
9 |
| -```` |
| 11 | +~~~ |
| 12 | +<!-- prettier-ignore-end --> |
10 | 13 |
|
11 |
| -This can be useful for importing web components and using them in Markdown. |
| 14 | +Adding the above will log `Hello, World!` to the console without adding a global `message` variable. |
12 | 15 |
|
13 |
| -```` |
| 16 | +This can be useful for importing web components and using them in Markdown. Imagine you had some `magic-reveal` element that you wanted to use on a page: |
| 17 | + |
| 18 | +<!-- prettier-ignore-start --> |
| 19 | +~~~markdown |
14 | 20 | ```js script
|
15 | 21 | import 'magic-reveal/magic-reveal.js';
|
| 22 | +``` |
16 | 23 |
|
17 | 24 | <magic-reveal>
|
18 | 25 |
|
19 | 26 | This text will get magically revealed. I can **still** use Markdown as long as there is an empty line between the opening/closing tags and my text.
|
20 | 27 |
|
21 | 28 | </magic-reveal>
|
22 |
| -``` |
23 |
| -```` |
| 29 | +~~~ |
| 30 | +<!-- prettier-ignore-end --> |
| 31 | + |
| 32 | +## Component Story Format |
24 | 33 |
|
25 |
| -or you can use `js story`, `js preview-story`, ... |
| 34 | +You can also add storybook-style CSF (v2 only) stories to a page using `js story` or `js preview-story`, just make sure to import `html` from `@mdjs/mdjs-preview` instead of from `lit` or `lit-html`. |
| 35 | + |
| 36 | +<!-- prettier-ignore-start --> |
| 37 | +~~~markdown |
| 38 | +```js story |
| 39 | +import { html } from '@mdjs/mdjs-preview'; |
| 40 | + |
| 41 | +export const StoryPreview = () => html` |
| 42 | + <p>Use stories in Rocket!</p> |
| 43 | +`; |
| 44 | +``` |
| 45 | +~~~ |
| 46 | +<!-- prettier-ignore-end --> |
0 commit comments