histoire 0.0.3 → 0.0.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.
Files changed (115) hide show
  1. package/README.md +11 -0
  2. package/dist/client/app/App.vue +1 -1
  3. package/dist/client/app/codegen/vue3.js +28 -4
  4. package/dist/client/app/codegen/vue3.ts +32 -4
  5. package/dist/client/app/components/app/AppHeader.vue +10 -3
  6. package/dist/client/app/components/base/BaseTab.vue +46 -0
  7. package/dist/client/app/components/exposed/Story.vue +21 -1
  8. package/dist/client/app/components/exposed/Variant.vue +1 -0
  9. package/dist/client/app/components/misc/CheckerboardPattern.vue +32 -0
  10. package/dist/client/app/components/sandbox/SandboxVue3.vue +6 -0
  11. package/dist/client/app/components/search/SearchItem.vue +5 -9
  12. package/dist/client/app/components/{StoryControls.vue → story/StoryControls.vue} +3 -3
  13. package/dist/client/app/components/story/StoryDocs.vue +37 -0
  14. package/dist/client/app/components/{StoryList.vue → story/StoryList.vue} +4 -4
  15. package/{src/client/app/components → dist/client/app/components/story}/StoryListFolder.vue +5 -5
  16. package/{src/client/app/components → dist/client/app/components/story}/StoryListItem.vue +11 -6
  17. package/dist/client/app/components/story/StoryResponsivePreview.vue +214 -0
  18. package/dist/client/app/components/{StorySourceCode.vue → story/StorySourceCode.vue} +4 -8
  19. package/{src/client/app/components → dist/client/app/components/story}/StoryVariantGridItem.vue +14 -6
  20. package/dist/client/app/components/{StoryVariantListItem.vue → story/StoryVariantListItem.vue} +9 -8
  21. package/dist/client/app/components/story/StoryVariantSingleView.vue +182 -0
  22. package/dist/client/app/components/{StoryView.vue → story/StoryView.vue} +37 -10
  23. package/dist/client/app/global-components.js +2 -0
  24. package/dist/client/app/global-components.ts +3 -0
  25. package/dist/client/app/index.js +4 -0
  26. package/dist/client/app/index.ts +4 -0
  27. package/dist/client/app/router.js +1 -1
  28. package/dist/client/app/router.ts +1 -1
  29. package/dist/client/app/sandbox.js +37 -3
  30. package/dist/client/app/sandbox.ts +37 -3
  31. package/dist/client/app/stores/story.d.ts +15 -0
  32. package/dist/client/app/style/main.css +15 -0
  33. package/dist/client/app/style/sandbox.css +5 -0
  34. package/dist/client/app/style/transitions.css +12 -0
  35. package/dist/client/app/types.d.ts +1 -0
  36. package/dist/client/app/types.ts +1 -0
  37. package/dist/client/app/util/const.d.ts +2 -0
  38. package/dist/client/app/util/const.js +2 -0
  39. package/dist/client/app/util/const.ts +2 -0
  40. package/dist/client/app/util/mapping.js +3 -0
  41. package/dist/client/app/util/mapping.ts +2 -0
  42. package/dist/client/app/util/preview-settings.d.ts +16 -0
  43. package/dist/client/app/util/preview-settings.js +15 -0
  44. package/dist/client/app/util/preview-settings.ts +26 -0
  45. package/dist/client/app/util/reactivity.d.ts +1 -0
  46. package/dist/client/app/util/reactivity.js +16 -0
  47. package/dist/client/app/util/reactivity.ts +22 -0
  48. package/dist/client/app/util/scroll.d.ts +2 -0
  49. package/dist/client/app/util/scroll.js +24 -0
  50. package/dist/client/app/util/scroll.ts +27 -0
  51. package/dist/client/app/{composable → util}/variant.d.ts +0 -0
  52. package/dist/client/app/{composable → util}/variant.js +0 -0
  53. package/dist/client/app/{composable → util}/variant.ts +0 -0
  54. package/dist/client/server/Story.server.vue +25 -2
  55. package/dist/client/server/index.js +2 -0
  56. package/dist/client/server/index.ts +3 -0
  57. package/dist/controls.d.ts +1 -0
  58. package/dist/controls.js +1 -0
  59. package/dist/node/build.js +13 -8
  60. package/dist/node/collect.js +12 -3
  61. package/dist/node/config.d.ts +24 -2
  62. package/dist/node/config.js +44 -4
  63. package/dist/node/markdown.d.ts +2 -0
  64. package/dist/node/markdown.js +48 -0
  65. package/dist/node/plugin.js +29 -2
  66. package/dist/node/util/slugify.d.ts +1 -0
  67. package/dist/node/util/slugify.js +22 -0
  68. package/dist/style.css +826 -171
  69. package/package.json +14 -4
  70. package/src/client/app/App.vue +1 -1
  71. package/src/client/app/codegen/vue3.ts +32 -4
  72. package/src/client/app/components/app/AppHeader.vue +10 -3
  73. package/src/client/app/components/base/BaseTab.vue +46 -0
  74. package/src/client/app/components/exposed/Story.vue +21 -1
  75. package/src/client/app/components/exposed/Variant.vue +1 -0
  76. package/src/client/app/components/misc/CheckerboardPattern.vue +32 -0
  77. package/src/client/app/components/sandbox/SandboxVue3.vue +6 -0
  78. package/src/client/app/components/search/SearchItem.vue +5 -9
  79. package/src/client/app/components/{StoryControls.vue → story/StoryControls.vue} +3 -3
  80. package/src/client/app/components/story/StoryDocs.vue +37 -0
  81. package/src/client/app/components/{StoryList.vue → story/StoryList.vue} +4 -4
  82. package/{dist/client/app/components → src/client/app/components/story}/StoryListFolder.vue +5 -5
  83. package/{dist/client/app/components → src/client/app/components/story}/StoryListItem.vue +11 -6
  84. package/src/client/app/components/story/StoryResponsivePreview.vue +214 -0
  85. package/src/client/app/components/{StorySourceCode.vue → story/StorySourceCode.vue} +4 -8
  86. package/{dist/client/app/components → src/client/app/components/story}/StoryVariantGridItem.vue +14 -6
  87. package/src/client/app/components/{StoryVariantListItem.vue → story/StoryVariantListItem.vue} +9 -8
  88. package/src/client/app/components/story/StoryVariantSingleView.vue +182 -0
  89. package/src/client/app/components/{StoryView.vue → story/StoryView.vue} +37 -10
  90. package/src/client/app/global-components.ts +3 -0
  91. package/src/client/app/index.ts +4 -0
  92. package/src/client/app/router.ts +1 -1
  93. package/src/client/app/sandbox.ts +37 -3
  94. package/src/client/app/style/main.css +15 -0
  95. package/src/client/app/style/sandbox.css +5 -0
  96. package/src/client/app/style/transitions.css +12 -0
  97. package/src/client/app/types.ts +1 -0
  98. package/src/client/app/util/const.ts +2 -0
  99. package/src/client/app/util/mapping.ts +2 -0
  100. package/src/client/app/util/preview-settings.ts +26 -0
  101. package/src/client/app/util/reactivity.ts +22 -0
  102. package/src/client/app/util/scroll.ts +27 -0
  103. package/src/client/app/{composable → util}/variant.ts +0 -0
  104. package/src/client/server/Story.server.vue +25 -2
  105. package/src/client/server/index.ts +3 -0
  106. package/src/controls.ts +1 -0
  107. package/src/node/build.ts +15 -8
  108. package/src/node/collect.ts +12 -3
  109. package/src/node/config.ts +69 -6
  110. package/src/node/markdown.ts +56 -0
  111. package/src/node/plugin.ts +31 -2
  112. package/src/node/util/slugify.ts +26 -0
  113. package/tailwind.config.cjs +79 -4
  114. package/dist/client/app/components/StoryVariantSingleView.vue +0 -173
  115. package/src/client/app/components/StoryVariantSingleView.vue +0 -173
@@ -1,6 +1,6 @@
1
1
  import './style/sandbox.css'
2
2
  import { parseQuery } from 'vue-router'
3
- import { computed, createApp, h, ref, toRaw, watch } from 'vue'
3
+ import { computed, createApp, h, onMounted, ref, watch } from 'vue'
4
4
  import { createPinia } from 'pinia'
5
5
  import { registerGlobalComponents } from './global-components'
6
6
  import SandboxVue3 from './components/sandbox/SandboxVue3.vue'
@@ -8,7 +8,11 @@ import type { StoryFile } from './types'
8
8
  import { mapFile } from './util/mapping'
9
9
  // @ts-expect-error virtual module
10
10
  import { files } from '$histoire-stories'
11
- import { STATE_SYNC } from './util/const.js'
11
+ import { PREVIEW_SETTINGS_SYNC, STATE_SYNC, SANDBOX_READY } from './util/const.js'
12
+ import { applyPreviewSettings } from './util/preview-settings.js'
13
+ import { isDark } from './util/dark.js'
14
+ import { histoireConfig } from './util/config.js'
15
+ import { toRawDeep } from './util/reactivity'
12
16
 
13
17
  const query = parseQuery(window.location.search)
14
18
  const file = ref<StoryFile>(mapFile(files.find(f => f.id === query.storyId)))
@@ -18,21 +22,36 @@ const app = createApp({
18
22
  const story = computed(() => file.value.story)
19
23
  const variant = computed(() => story.value?.variants.find(v => v.id === query.variantId))
20
24
 
25
+ let synced = false
26
+ let mounted = false
27
+
21
28
  window.addEventListener('message', event => {
22
29
  if (event.data?.type === STATE_SYNC) {
30
+ if (!mounted) return
31
+ synced = true
23
32
  Object.assign(variant.value.state, event.data.state)
33
+ } else if (event.data?.type === PREVIEW_SETTINGS_SYNC) {
34
+ applyPreviewSettings(event.data.settings)
24
35
  }
25
36
  })
26
37
 
27
38
  watch(() => variant.value.state, value => {
39
+ if (synced && mounted) {
40
+ synced = false
41
+ return
42
+ }
28
43
  window.parent?.postMessage({
29
44
  type: STATE_SYNC,
30
- state: toRaw(value),
45
+ state: toRawDeep(value),
31
46
  })
32
47
  }, {
33
48
  deep: true,
34
49
  })
35
50
 
51
+ onMounted(() => {
52
+ mounted = true
53
+ })
54
+
36
55
  return {
37
56
  story,
38
57
  variant,
@@ -50,6 +69,11 @@ const app = createApp({
50
69
  ? h(SandboxVue3, {
51
70
  story: this.story,
52
71
  variant: this.variant,
72
+ onReady: () => {
73
+ window.parent?.postMessage({
74
+ type: SANDBOX_READY,
75
+ })
76
+ },
53
77
  })
54
78
  : null,
55
79
  ]
@@ -58,3 +82,13 @@ const app = createApp({
58
82
  app.use(createPinia())
59
83
  registerGlobalComponents(app)
60
84
  app.mount('#app')
85
+
86
+ watch(isDark, value => {
87
+ if (value) {
88
+ document.documentElement.classList.add(histoireConfig.sandboxDarkClass)
89
+ } else {
90
+ document.documentElement.classList.remove(histoireConfig.sandboxDarkClass)
91
+ }
92
+ }, {
93
+ immediate: true,
94
+ })
@@ -1,6 +1,7 @@
1
1
  @import url('https://fonts.googleapis.com/css2?family=Lato&display=swap');
2
2
  @import 'floating-vue/dist/style.css';
3
3
  @import 'floating-vue.css';
4
+ @import 'transitions.css';
4
5
 
5
6
  *,
6
7
  ::before,
@@ -50,3 +51,17 @@ body,
50
51
  .__histoire-sandbox {
51
52
  contain: content;
52
53
  }
54
+
55
+ .__histoire-code .shiki {
56
+ background: transparent !important;
57
+ }
58
+
59
+ .htw-prose {
60
+ h1, h2, h3, h4 {
61
+ &:not(:hover) {
62
+ .header-anchor[aria-hidden="true"] {
63
+ visibility: hidden;
64
+ }
65
+ }
66
+ }
67
+ }
@@ -1,3 +1,8 @@
1
+ html,
2
+ body {
3
+ background: transparent !important;
4
+ }
5
+
1
6
  body {
2
7
  margin: 0;
3
8
  }
@@ -0,0 +1,12 @@
1
+ .__histoire-scale-x {
2
+ &-enter-active,
3
+ &-leave-active {
4
+ transition: transform .15s, opacity .15s;
5
+ }
6
+
7
+ &-enter-from,
8
+ &-leave-to {
9
+ transform: scaleX(0);
10
+ opacity: 0;
11
+ }
12
+ }
@@ -33,6 +33,7 @@ export interface Variant {
33
33
  slots?: () => Readonly<any>
34
34
  state?: any
35
35
  source?: string
36
+ ready?: boolean
36
37
  }
37
38
 
38
39
  export type TreeLeaf = {
@@ -1 +1,3 @@
1
1
  export const STATE_SYNC = '__histoire:state-sync'
2
+ export const SANDBOX_READY = '__histoire:sandbox-ready'
3
+ export const PREVIEW_SETTINGS_SYNC = '__histoire:preview-settings-sync'
@@ -21,6 +21,7 @@ export function mapFile (file: StoryFile, existingFile?: StoryFile): StoryFile {
21
21
  if (existingVariant.state) Object.assign(variant.state, existingVariant.state)
22
22
  if (existingVariant.initState) variant.initState = existingVariant.initState
23
23
  if (existingVariant.slots) variant.slots = existingVariant.slots
24
+ if (existingVariant.ready) variant.ready = existingVariant.ready
24
25
  }
25
26
  }
26
27
  if (existingFile.story.lastSelectedVariant) {
@@ -37,5 +38,6 @@ export function mapVariant (variant: Variant): Variant {
37
38
  state: {},
38
39
  initState: null,
39
40
  slots: () => ({}),
41
+ ready: false,
40
42
  }
41
43
  }
@@ -0,0 +1,26 @@
1
+ import { useStorage } from '@vueuse/core'
2
+ import { reactive } from 'vue'
3
+
4
+ export interface PreviewSettings {
5
+ responsiveWidth: number
6
+ responsiveHeight: number
7
+ rotate: boolean
8
+ backgroundColor: string
9
+ checkerboard: boolean
10
+ }
11
+
12
+ export function usePreviewSettings () {
13
+ return useStorage<PreviewSettings>('_histoire-sandbox-settings-v1', {
14
+ responsiveWidth: 720,
15
+ responsiveHeight: null,
16
+ rotate: false,
17
+ backgroundColor: 'transparent',
18
+ checkerboard: false,
19
+ })
20
+ }
21
+
22
+ export const receivedSettings = reactive<PreviewSettings>({} as PreviewSettings)
23
+
24
+ export function applyPreviewSettings (settings: PreviewSettings) {
25
+ Object.assign(receivedSettings, settings)
26
+ }
@@ -0,0 +1,22 @@
1
+ import { unref, isRef } from 'vue'
2
+
3
+ const isObject = (val) => val !== null && typeof val === 'object'
4
+
5
+ export function toRawDeep (val) {
6
+ const unwrappedValue = isRef(val) ? unref(val) : val
7
+
8
+ if (!isObject(unwrappedValue)) {
9
+ return unwrappedValue
10
+ }
11
+
12
+ if (Array.isArray(unwrappedValue)) {
13
+ return unwrappedValue.map(toRawDeep)
14
+ }
15
+
16
+ return toRawObject(unwrappedValue)
17
+ }
18
+
19
+ const toRawObject = (obj: Record<any, any>) => Object.keys(obj).reduce((raw, key) => {
20
+ raw[key] = toRawDeep(obj[key])
21
+ return raw
22
+ }, {})
@@ -0,0 +1,27 @@
1
+ import { watch, onMounted, Ref } from 'vue'
2
+ import scrollIntoView from 'scroll-into-view-if-needed'
3
+
4
+ export function useScrollOnActive (active: Ref<boolean>, el: Ref<HTMLElement>) {
5
+ watch(active, value => {
6
+ if (value) {
7
+ autoScroll()
8
+ }
9
+ })
10
+
11
+ function autoScroll () {
12
+ if (el.value) {
13
+ scrollIntoView(el.value, {
14
+ scrollMode: 'if-needed',
15
+ block: 'center',
16
+ inline: 'nearest',
17
+ behavior: 'smooth',
18
+ })
19
+ }
20
+ }
21
+
22
+ onMounted(() => {
23
+ if (active.value) {
24
+ autoScroll()
25
+ }
26
+ })
27
+ }
File without changes
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { defineComponent, inject, PropType, provide, useAttrs } from 'vue'
2
+ import { defineComponent, inject, onMounted, PropType, provide, useAttrs } from 'vue'
3
3
  import type { StoryFile, Story, Variant } from '../../node/types'
4
4
 
5
5
  export default defineComponent({
@@ -53,13 +53,36 @@ export default defineComponent({
53
53
  story.variants.push(variant)
54
54
  })
55
55
 
56
+ onMounted(() => {
57
+ if (!story.variants.length) {
58
+ story.variants.push({
59
+ id: '_default',
60
+ title: 'default',
61
+ })
62
+ }
63
+ })
64
+
56
65
  return {
57
66
  story,
58
67
  }
59
68
  },
60
69
 
61
70
  render () {
62
- return this.$slots.default()
71
+ let suppressError = false
72
+ try {
73
+ return this.$slots.default({
74
+ get state () {
75
+ // No variant tags
76
+ suppressError = true
77
+ return {}
78
+ },
79
+ })
80
+ } catch (e) {
81
+ if (!suppressError) {
82
+ console.error(e)
83
+ }
84
+ return null
85
+ }
63
86
  },
64
87
  })
65
88
  </script>
@@ -1,4 +1,5 @@
1
1
  import { createApp, h } from 'vue'
2
+ import { registerVueComponents } from '@histoire/controls'
2
3
  import type { StoryFile } from '../../node/types'
3
4
  import Story from './Story.server.vue'
4
5
  import Variant from './Variant.server.vue'
@@ -24,6 +25,8 @@ export async function run (file: StoryFile, storyData, el) {
24
25
  // eslint-disable-next-line vue/multi-word-component-names
25
26
  app.component('Variant', Variant)
26
27
 
28
+ registerVueComponents(app)
29
+
27
30
  app.mount(el)
28
31
  app.unmount()
29
32
  }
@@ -0,0 +1 @@
1
+ export * from '@histoire/controls'
package/src/node/build.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { join } from 'pathe'
2
- import { build as viteBuild, createServer as createViteServer, resolveConfig as resolveViteConfig, Plugin as VitePlugin } from 'vite'
2
+ import { build as viteBuild, createServer as createViteServer } from 'vite'
3
3
  import fs from 'fs-extra'
4
4
  import { lookup as lookupMime } from 'mrmime'
5
+ import pc from 'picocolors'
6
+ import { performance } from 'perf_hooks'
5
7
  import { APP_PATH } from './alias.js'
6
8
  import { Context } from './context.js'
7
9
  import { createVitePlugins } from './plugin.js'
@@ -10,17 +12,12 @@ import type { RollupOutput } from 'rollup'
10
12
  import { useCollectStories } from './collect.js'
11
13
 
12
14
  export async function build (ctx: Context) {
15
+ const startTime = performance.now()
13
16
  await findAllStories(ctx)
14
17
 
15
18
  // Collect story data
16
- const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build')
17
- const plugins: VitePlugin[] = []
18
- const hasVuePlugin = viteConfig.plugins.find(p => p.name === 'vite:vue')
19
- if (!hasVuePlugin) {
20
- plugins.push((await import('@vitejs/plugin-vue')).default())
21
- }
22
19
  const server = await createViteServer({
23
- plugins,
20
+ plugins: await createVitePlugins(ctx),
24
21
  })
25
22
  await server.pluginContainer.buildStart({})
26
23
  const { executeStoryFile, destroy: destroyCollectStories } = useCollectStories({
@@ -33,6 +30,10 @@ export async function build (ctx: Context) {
33
30
  await server.close()
34
31
  await destroyCollectStories()
35
32
 
33
+ const storyCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ? 1 : 0), 0)
34
+ const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0)
35
+ const emptyStoryCount = ctx.storyFiles.length - storyCount
36
+
36
37
  const results = await viteBuild({
37
38
  plugins: await createVitePlugins(ctx),
38
39
  build: {
@@ -58,6 +59,12 @@ export async function build (ctx: Context) {
58
59
  // Sandbox
59
60
  const sandboxOutput = result.output.find(o => o.name === 'sandbox' && o.type === 'chunk')
60
61
  await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', ctx)
62
+
63
+ const duration = performance.now() - startTime
64
+ if (emptyStoryCount) {
65
+ console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`))
66
+ }
67
+ console.log(pc.green(`✅ Built ${storyCount} stories (${variantCount} variants) in ${Math.round(duration / 1000 * 100) / 100}s`))
61
68
  }
62
69
 
63
70
  async function writeHtml (jsEntryFile: string, cssEntryFile: string, htmlFileName: string, ctx: Context) {
@@ -20,7 +20,13 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
20
20
  const { server } = options
21
21
  const { destroy: destroyDomEnv } = createDomEnv()
22
22
 
23
- const node = new ViteNodeServer(server)
23
+ const node = new ViteNodeServer(server, {
24
+ deps: {
25
+ inline: [
26
+ /histoire\/dist/,
27
+ ],
28
+ },
29
+ })
24
30
  const runner = new ViteNodeRunner({
25
31
  root: server.config.root,
26
32
  base: server.config.base,
@@ -31,6 +37,9 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
31
37
  }
32
38
  return node.fetchModule(id)
33
39
  },
40
+ resolveId (id, importer) {
41
+ return node.resolveId(id, importer)
42
+ },
34
43
  })
35
44
 
36
45
  function clearCache () {
@@ -46,10 +55,10 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
46
55
  const storyData: Story[] = []
47
56
  await run(storyFile, storyData, el)
48
57
  if (storyData.length === 0) {
49
- console.warn(pc.yellow(`No story found for ${storyFile.path}`))
58
+ console.warn(pc.yellow(`⚠️ No story found for ${storyFile.path}`))
50
59
  return
51
60
  } else if (storyData.length > 1) {
52
- console.warn(pc.yellow(`Multiple stories not supported: ${storyFile.path}`))
61
+ console.warn(pc.yellow(`⚠️ Multiple stories not supported: ${storyFile.path}`))
53
62
  }
54
63
  storyFile.story = storyData[0]
55
64
  const file: TreeFile = {
@@ -1,12 +1,13 @@
1
1
  import path from 'pathe'
2
2
  import fs from 'fs'
3
3
  import defu from 'defu'
4
- import { createServer, resolveConfig as resolveViteConfig, UserConfig as ViteConfig } from 'vite'
4
+ import { createServer, resolveConfig as resolveViteConfig, UserConfig as ViteConfig, ConfigEnv as ViteConfigEnv } from 'vite'
5
5
  import { ViteNodeServer } from 'vite-node/server'
6
6
  import { ViteNodeRunner } from 'vite-node/client'
7
7
  import pc from 'picocolors'
8
8
  import { TreeFile } from './tree.js'
9
9
  import { defaultColors } from './colors.js'
10
+ import type MarkdownIt from 'markdown-it'
10
11
 
11
12
  type CustomizableColors = 'primary' | 'gray'
12
13
  type ColorKeys = '50' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'
@@ -15,6 +16,12 @@ type GrayColorKeys = ColorKeys | '750' | '850' | '950'
15
16
  interface ResponsivePreset {
16
17
  label: string
17
18
  width: number
19
+ height: number
20
+ }
21
+
22
+ interface BackgroundPreset {
23
+ label: string
24
+ color: string
18
25
  }
19
26
 
20
27
  export interface HistoireConfig {
@@ -89,6 +96,10 @@ export interface HistoireConfig {
89
96
  [key in ColorKeys]?: string
90
97
  }
91
98
  }
99
+ /**
100
+ * Add a link to the main logo
101
+ */
102
+ logoHref?: string
92
103
  }
93
104
  /**
94
105
  * Setup file exposting a default function executed when setting up each story preview.
@@ -102,10 +113,22 @@ export interface HistoireConfig {
102
113
  * Predefined responsive sizes for story playgrounds.
103
114
  */
104
115
  responsivePresets?: ResponsivePreset[]
116
+ /**
117
+ * Background color of the story preview.
118
+ */
119
+ backgroundPresets?: BackgroundPreset[]
120
+ /**
121
+ * Class added to the html root of the story preview when dark mode is enabled.
122
+ */
123
+ sandboxDarkClass?: string
124
+ /**
125
+ * Customize the markdown-it renderer
126
+ */
127
+ markdown?: (md: MarkdownIt) => MarkdownIt | Promise<MarkdownIt>
105
128
  /**
106
129
  * Vite config override
107
130
  */
108
- vite?: ViteConfig
131
+ vite?: ViteConfig | ((config: ViteConfig, env: ViteConfigEnv) => void | ViteConfig | Promise<void | ViteConfig>)
109
132
  }
110
133
 
111
134
  export function getDefaultConfig (): HistoireConfig {
@@ -128,36 +151,67 @@ export function getDefaultConfig (): HistoireConfig {
128
151
  {
129
152
  label: 'Mobile (Small)',
130
153
  width: 320,
154
+ height: 560,
131
155
  },
132
156
  {
133
157
  label: 'Mobile (Medium)',
134
- width: 375,
158
+ width: 360,
159
+ height: 640,
135
160
  },
136
161
  {
137
162
  label: 'Mobile (Large)',
138
- width: 425,
163
+ width: 414,
164
+ height: 896,
139
165
  },
140
166
  {
141
167
  label: 'Tablet',
142
168
  width: 768,
169
+ height: 1024,
143
170
  },
144
171
  {
145
172
  label: 'Laptop (Small)',
146
173
  width: 1024,
174
+ height: null,
147
175
  },
148
176
  {
149
177
  label: 'Laptop (Large)',
150
- width: 1440,
178
+ width: 1366,
179
+ height: null,
151
180
  },
152
181
  {
153
182
  label: 'Desktop',
154
183
  width: 1920,
184
+ height: null,
155
185
  },
156
186
  {
157
187
  label: '4K',
158
188
  width: 3840,
189
+ height: null,
159
190
  },
160
191
  ],
192
+ backgroundPresets: [
193
+ {
194
+ label: 'Transparent',
195
+ color: 'transparent',
196
+ },
197
+ {
198
+ label: 'White',
199
+ color: '#fff',
200
+ },
201
+ {
202
+ label: 'Light gray',
203
+ color: '#aaa',
204
+ },
205
+ {
206
+ label: 'Dark gray',
207
+ color: '#333',
208
+ },
209
+ {
210
+ label: 'Black',
211
+ color: '#000',
212
+ },
213
+ ],
214
+ sandboxDarkClass: 'dark',
161
215
  }
162
216
  }
163
217
 
@@ -194,12 +248,21 @@ export async function loadConfigFile (configFile: string): Promise<Partial<Histo
194
248
  try {
195
249
  const server = await createServer()
196
250
  await server.pluginContainer.buildStart({})
197
- const node = new ViteNodeServer(server)
251
+ const node = new ViteNodeServer(server, {
252
+ deps: {
253
+ inline: [
254
+ /histoire\/dist/,
255
+ ],
256
+ },
257
+ })
198
258
  const runner = new ViteNodeRunner({
199
259
  root: path.dirname(configFile),
200
260
  fetchModule (id) {
201
261
  return node.fetchModule(id)
202
262
  },
263
+ resolveId (id, importer) {
264
+ return node.resolveId(id, importer)
265
+ },
203
266
  })
204
267
  const result: { default: Partial<HistoireConfig> } = await runner.executeFile(configFile)
205
268
  await server.close()
@@ -0,0 +1,56 @@
1
+ import MarkdownIt from 'markdown-it'
2
+ import shiki from 'shiki'
3
+ import anchor from 'markdown-it-anchor'
4
+ import attrs from 'markdown-it-attrs'
5
+ import emoji from 'markdown-it-emoji'
6
+ import { slugify } from './util/slugify.js'
7
+
8
+ export async function createMarkdownRenderer () {
9
+ const highlighter = await shiki.getHighlighter({
10
+ theme: 'github-dark',
11
+ })
12
+
13
+ const md = new MarkdownIt({
14
+ highlight: (code, lang) => `<div class="htw-relative htw-not-prose __histoire-code"><div class="htw-absolute htw-top-0 htw-right-0 htw-text-xs htw-text-white/40">${lang}</div>${highlighter.codeToHtml(code, {
15
+ lang,
16
+ })}</div>`,
17
+ linkify: true,
18
+ html: true,
19
+ breaks: true,
20
+ })
21
+
22
+ md.use(anchor, {
23
+ slugify,
24
+ permalink: anchor.permalink.ariaHidden({}),
25
+ })
26
+ .use(attrs)
27
+ .use(emoji)
28
+
29
+ // External links
30
+ {
31
+ const defaultRender = md.renderer.rules.link_open || function (tokens, idx, options, env, self) {
32
+ return self.renderToken(tokens, idx, options)
33
+ }
34
+
35
+ md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
36
+ const hrefIndex = tokens[idx].attrIndex('href')
37
+ const classIndex = tokens[idx].attrIndex('class')
38
+
39
+ if (hrefIndex >= 0 && !tokens[idx].attrs[hrefIndex][1].startsWith('/') && (classIndex < 0 || !tokens[idx].attrs[classIndex][1].includes('header-anchor'))) {
40
+ // If you are sure other plugins can't add `target` - drop check below
41
+ const aIndex = tokens[idx].attrIndex('target')
42
+
43
+ if (aIndex < 0) {
44
+ tokens[idx].attrPush(['target', '_blank']) // add new attribute
45
+ } else {
46
+ tokens[idx].attrs[aIndex][1] = '_blank' // replace value of existing attr
47
+ }
48
+ }
49
+
50
+ // pass token to default renderer.
51
+ return defaultRender(tokens, idx, options, env, self)
52
+ }
53
+ }
54
+
55
+ return md
56
+ }
@@ -6,6 +6,7 @@ import { Context } from './context.js'
6
6
  import { notifyStoryChange } from './stories.js'
7
7
  import { makeTree } from './tree.js'
8
8
  import { parseColor } from './colors.js'
9
+ import { createMarkdownRenderer } from './markdown.js'
9
10
 
10
11
  export const STORIES_ID = '$histoire-stories'
11
12
  export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`
@@ -29,8 +30,8 @@ export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
29
30
  if (ctx.config.vite) {
30
31
  plugins.push({
31
32
  name: 'histoire-vite-config-override',
32
- config () {
33
- return ctx.config.vite
33
+ config (config, env) {
34
+ return typeof ctx.config.vite === 'function' ? ctx.config.vite(config, env) : ctx.config.vite
34
35
  },
35
36
  })
36
37
  }
@@ -201,6 +202,9 @@ if (import.meta.hot) {
201
202
  },
202
203
  })
203
204
 
205
+ // Custom blocks
206
+ plugins.push(...await createCustomBlocksPlugins(ctx))
207
+
204
208
  if (ctx.mode === 'build') {
205
209
  // Add file name in build mode to have components names instead of <Anonymous>
206
210
  const include = [/\.vue$/]
@@ -223,3 +227,28 @@ if (import.meta.hot) {
223
227
 
224
228
  return plugins
225
229
  }
230
+
231
+ async function createCustomBlocksPlugins (ctx) {
232
+ const plugins: VitePlugin[] = []
233
+
234
+ let md = await createMarkdownRenderer()
235
+ if (ctx.config.markdown) {
236
+ const result = await ctx.config.markdown(md)
237
+ if (result) {
238
+ md = result
239
+ }
240
+ }
241
+ plugins.push({
242
+ name: 'histoire-vue-docs-block',
243
+ transform (code, id) {
244
+ if (!id.includes('?vue&type=docs')) return
245
+ if (!id.includes('lang.md')) return
246
+ const html = md.render(code)
247
+ return `export default Comp => {
248
+ Comp.doc = ${JSON.stringify(html)}
249
+ }`
250
+ },
251
+ })
252
+
253
+ return plugins
254
+ }