docs(ComponentCode/ComponentExample): disable prettier by default

This commit is contained in:
Benjamin Canac
2024-07-26 13:02:55 +02:00
parent 839b97a9dc
commit a6c4ffa013
8 changed files with 71 additions and 19 deletions

View File

@@ -16,6 +16,11 @@ const props = defineProps<{
items?: { [key: string]: string[] } items?: { [key: string]: string[] }
props?: { [key: string]: any } props?: { [key: string]: any }
slots?: { [key: string]: any } slots?: { [key: string]: any }
/**
* Whether to format the code with Prettier
* @defaultValue false
*/
prettier?: boolean
}>() }>()
const route = useRoute() const route = useRoute()
@@ -85,23 +90,26 @@ const code = computed(() => {
<script setup lang="ts"> <script setup lang="ts">
` `
for (const key of props.external) { for (const key of props.external) {
code += `const ${key} = ${json5.stringify(componentProps[key], null, 2).replace(/,([ |\t\n]+[}|\]])/g, '$1')} code += `const ${key === 'modelValue' ? 'value' : key} = ref(${json5.stringify(componentProps[key], null, 2).replace(/,([ |\t\n]+[}|\]])/g, '$1')})
` `
} }
code += ` code += `<\/script>
<\/script>
` `
} }
code += ` code += `
<template> <template>
<${name}` <${name}`
for (const [key, value] of Object.entries(componentProps)) { for (const [key, value] of Object.entries(componentProps)) {
if (value === undefined || value === null || value === '' || props.hide?.includes(key)) { if (value === undefined || value === null || value === '' || props.hide?.includes(key)) {
continue continue
} }
if (key === 'modelValue') {
code += ` v-model="value"`
continue
}
const prop = meta?.meta?.props?.find((prop: any) => prop.name === key) const prop = meta?.meta?.props?.find((prop: any) => prop.name === key)
const name = kebabCase(key) const name = kebabCase(key)
@@ -130,13 +138,7 @@ const code = computed(() => {
if (props.slots) { if (props.slots) {
if (props.slots && Object.keys(props.slots).length === 1 && props.slots.default) { if (props.slots && Object.keys(props.slots).length === 1 && props.slots.default) {
if (Object.keys(props.props || {}).length > 0) { code += `>${props.slots.default}</${name}>`
code += `>
${props.slots.default}
</${name}>`
} else {
code += `>${props.slots.default}</${name}>`
}
} else { } else {
code += `> code += `>
${Object.entries(props.slots).map(([key, value]) => `<template #${key}> ${Object.entries(props.slots).map(([key, value]) => `<template #${key}>
@@ -155,6 +157,10 @@ ${props.slots.default}
}) })
const { data: ast } = await useAsyncData(`component-code-${name}-${JSON.stringify({ props: componentProps, slots: props.slots })}`, async () => { const { data: ast } = await useAsyncData(`component-code-${name}-${JSON.stringify({ props: componentProps, slots: props.slots })}`, async () => {
if (!props.prettier) {
return parseMarkdown(code.value)
}
let formatted = '' let formatted = ''
try { try {
formatted = await $prettier.format(code.value, { formatted = await $prettier.format(code.value, {

View File

@@ -4,6 +4,11 @@ import { camelCase } from 'scule'
const props = defineProps<{ const props = defineProps<{
name: string name: string
props?: { [key: string]: any } props?: { [key: string]: any }
/**
* Whether to format the code with Prettier
* @defaultValue false
*/
prettier?: boolean
}>() }>()
const { $prettier } = useNuxtApp() const { $prettier } = useNuxtApp()
@@ -17,6 +22,10 @@ const componentProps = reactive({ ...(props.props || {}) })
const code = computed(() => `\`\`\`vue\n${data?.code ?? ''}\n\`\`\``) const code = computed(() => `\`\`\`vue\n${data?.code ?? ''}\n\`\`\``)
const { data: ast } = await useAsyncData(`component-example-${camelName}`, async () => { const { data: ast } = await useAsyncData(`component-example-${camelName}`, async () => {
if (!props.prettier) {
return parseMarkdown(code.value)
}
let formatted = '' let formatted = ''
try { try {
formatted = await $prettier.format(code.value, { formatted = await $prettier.format(code.value, {

View File

@@ -23,6 +23,7 @@ Use the `description` prop to set the description of the Alert.
::component-code ::component-code
--- ---
prettier: true
props: props:
title: 'Heads up!' title: 'Heads up!'
description: 'You can change the primary color in your app config.' description: 'You can change the primary color in your app config.'
@@ -35,6 +36,7 @@ Use the `icon` prop to show an [Icon](/components/icon).
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- title - title
- description - description
@@ -51,6 +53,7 @@ Use the `avatar` prop to show an [Avatar](/components/avatar).
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- title - title
- description - description
@@ -67,6 +70,7 @@ Use the `color` and `variant` props to change the style of the Alert.
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- title - title
- description - description
@@ -92,6 +96,7 @@ Use the `close-icon` prop to customize the button [Icon](/components/icon). Defa
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- title - title
- description - description
@@ -112,6 +117,7 @@ You can pass all the props of the [Button](/components/button) component to cust
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- title - title
- description - description
@@ -133,6 +139,7 @@ Use the `actions` prop to add some [Button](/components/button) actions to the A
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- title - title
- actions - actions
@@ -159,6 +166,7 @@ Use the `class` prop to override the base styles of the Button.
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- title - title
- description - description
@@ -175,6 +183,7 @@ Use the `ui` prop to override the slots styles of the Button.
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- ui - ui
- title - title

View File

@@ -13,6 +13,7 @@ Wrap multiple [Avatar](/components/avatar) within an AvatarGroup to stack them.
::component-code ::component-code
--- ---
prettier: true
slots: slots:
default: | default: |
@@ -31,6 +32,7 @@ Use the `size` prop to change the size of all the avatars.
::component-code ::component-code
--- ---
prettier: true
props: props:
size: md size: md
slots: slots:
@@ -51,10 +53,12 @@ Use the `max` prop to limit the number of avatars displayed. The rest will be di
::component-code ::component-code
--- ---
prettier: true
props: props:
max: 2 max: 2
slots: slots:
default: > default: |
<UAvatar src="https://avatars.githubusercontent.com/u/739984?v=4" alt="Benjamin Canac" /> <UAvatar src="https://avatars.githubusercontent.com/u/739984?v=4" alt="Benjamin Canac" />
<UAvatar src="https://avatars.githubusercontent.com/u/25613751?v=4" alt="Romain Hamel" /> <UAvatar src="https://avatars.githubusercontent.com/u/25613751?v=4" alt="Romain Hamel" />
<UAvatar src="https://avatars.githubusercontent.com/u/19751938?v=4" alt="Neil Richter" /> <UAvatar src="https://avatars.githubusercontent.com/u/19751938?v=4" alt="Neil Richter" />

View File

@@ -13,6 +13,7 @@ Wrap multiple [Button](/components/button) within a ButtonGroup to group them to
::component-code ::component-code
--- ---
prettier: true
slots: slots:
default: | default: |
@@ -29,6 +30,7 @@ Use the `size` prop to change the size of all the buttons.
::component-code ::component-code
--- ---
prettier: true
props: props:
size: md size: md
slots: slots:
@@ -47,6 +49,7 @@ Use the `orientation` prop to change the orientation of the buttons.
::component-code ::component-code
--- ---
prettier: true
props: props:
orientation: vertical orientation: vertical
slots: slots:
@@ -67,6 +70,7 @@ You can use components like [Input](/components/input), [InputMenu](/components/
::component-code ::component-code
--- ---
prettier: true
slots: slots:
default: | default: |

View File

@@ -172,6 +172,7 @@ Use the `ui` prop to override the slots styles of the Button.
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- ui - ui
- color - color

View File

@@ -72,6 +72,7 @@ You can also choose to only render the content of the active tab by setting `con
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- content.forceMount - content.forceMount
external: external:

View File

@@ -21,10 +21,13 @@ Use the `text` prop to set the content of the Tooltip.
::component-code ::component-code
--- ---
prettier: true
props: props:
text: 'Open on GitHub' text: 'Open on GitHub'
slots: slots:
default: <UButton icon="i-simple-icons-github" /> default: |
<UButton icon="i-simple-icons-github" />
--- ---
:u-button{icon="i-simple-icons-github"} :u-button{icon="i-simple-icons-github"}
@@ -36,6 +39,7 @@ Use the `kbds` prop to render [Kbd](/components/kbd) components in the Tooltip.
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- text - text
- kbds - kbds
@@ -45,7 +49,9 @@ props:
- meta - meta
- G - G
slots: slots:
default: <UButton icon="i-simple-icons-github" /> default: |
<UButton icon="i-simple-icons-github" />
--- ---
:u-button{icon="i-simple-icons-github"} :u-button{icon="i-simple-icons-github"}
@@ -61,13 +67,16 @@ Use the `delay-duration` prop to change the delay before the Tooltip appears. Fo
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- text - text
props: props:
delayDuration: 0 delayDuration: 0
text: 'Open on GitHub' text: 'Open on GitHub'
slots: slots:
default: <UButton icon="i-simple-icons-github" /> default: |
<UButton icon="i-simple-icons-github" />
--- ---
:u-button{icon="i-simple-icons-github"} :u-button{icon="i-simple-icons-github"}
@@ -83,6 +92,7 @@ Use the `arrow` prop to display an arrow on the Tooltip.
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- text - text
- arrow - arrow
@@ -90,7 +100,9 @@ props:
arrow: true arrow: true
text: 'Open on GitHub' text: 'Open on GitHub'
slots: slots:
default: <UButton icon="i-simple-icons-github" /> default: |
<UButton icon="i-simple-icons-github" />
--- ---
:u-button{icon="i-simple-icons-github"} :u-button{icon="i-simple-icons-github"}
@@ -102,6 +114,7 @@ Use the `content` prop to control how the Tooltip content is rendered, like its
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- text - text
items: items:
@@ -116,7 +129,9 @@ props:
sideOffset: 8 sideOffset: 8
text: 'Open on GitHub' text: 'Open on GitHub'
slots: slots:
default: <UButton icon="i-simple-icons-github" /> default: |
<UButton icon="i-simple-icons-github" />
--- ---
:u-button{icon="i-simple-icons-github"} :u-button{icon="i-simple-icons-github"}
@@ -132,13 +147,16 @@ Use the `disabled` prop to disable the Tooltip.
::component-code ::component-code
--- ---
prettier: true
ignore: ignore:
- text - text
props: props:
disabled: true disabled: true
text: 'Open on GitHub' text: 'Open on GitHub'
slots: slots:
default: <UButton icon="i-simple-icons-github" /> default: |
<UButton icon="i-simple-icons-github" />
--- ---
:u-button{icon="i-simple-icons-github"} :u-button{icon="i-simple-icons-github"}