Skip to content

Commit

Permalink
feat: allow comments before main <Stories> tag
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Nov 8, 2022
1 parent f522d16 commit cfc2358
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
12 changes: 12 additions & 0 deletions examples/vite/src/writing-stories/1.default-export.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Button from '../components/Button.vue'

import type { Meta } from '@storybook/vue3'

export default {
/* 👇 The title prop is optional.
* See https://storybook.js.org/docs/vue/configure/overview#configure-story-loading
* to learn how to generate automatic titles
*/
title: 'Button',
component: Button,
} as Meta<typeof Button>
11 changes: 11 additions & 0 deletions examples/vite/src/writing-stories/1.default-export.stories.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup lang="ts">
import Button from '../components/Button.vue'
</script>
<template>
<!--
👇 The title prop is optional.
See https://storybook.js.org/docs/vue/configure/overview#configure-story-loading
to learn how to generate automatic titles
-->
<Stories title="Button"> </Stories>
</template>
1 change: 1 addition & 0 deletions examples/vite/src/writing-stories/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Contains the stories of https://storybook.js.org/docs/vue/writing-stories/introduction in both the classical `vue-ts` and the new `vue-native` format.
8 changes: 6 additions & 2 deletions src/core/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ function transformTemplate(content: string, resolvedScript?: SFCScriptBlock) {
}, */
})

const roots = template.ast?.children ?? []
if (roots.length !== 1)
const roots =
template.ast?.children.filter(
(node) => node.type === 1 /* NodeTypes.ELEMENT */
) ?? []
if (roots.length !== 1) {
throw new Error('Expected exactly one <Stories> element as root.')
}

const root = roots[0]
if (root.type !== 1 || root.tag !== 'Stories')
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ describe('transform', () => {
'"Story is missing a title"'
)
})
it('handles comment before stories tag', () => {
const code =
'<template><!-- comment --><Stories><Story title="Primary">hello</Story></Stories></template>'
const result = transform(code)
expect(result).toMatchInlineSnapshot(`
"const _sfc_main = {}
export default {
//component: MyComponent,
//decorators: [ ... ],
//parameters: { ... }
}
function renderPrimary(_ctx, _cache) {
return \\"hello\\"
}
export const Primary = () => Object.assign({render: renderPrimary}, _sfc_main)"
`)
})
it('handles multiple stories', () => {
const code = `
<template>
Expand Down

0 comments on commit cfc2358

Please sign in to comment.