Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(steps): simplify logic #112

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/components/global/Steps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const steps = computed(() => {
return s.map((step, index) => {
return {
idx: index + 1,
label: step.props?.label || `Step ${index + 1}`,
component: step,
}
})
Expand All @@ -33,8 +32,7 @@ defineOptions({
</div>

<div class="w-full overflow-hidden pl-8 md:pl-12 pr-px">
<p class="mt-2 text-lg font-semibold text-gray-900 dark:text-gray-200">{{ step.label }}</p>
<component :is="step.component" />
<component class="mt-2.5" :is="step.component" />
</div>
</div>
</div>
Expand Down
10 changes: 3 additions & 7 deletions app/server/plugins/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,18 @@ function transformStepsList(node: ContentNode) {
// TODO: Find a way to opt out of this transformation if needed within markdown.
if (node.tag === 'ol' && (node.children?.length || 0) > 0 && node.children?.[0].tag === 'li') {
const stepsChildren = node.children.map((li) => {
const label = li.children?.[0]?.value ?? undefined
// Exclude br tags from children to avoid spacing
const children = ((label && li.children?.slice(1)) || []).filter((child) => !['br'].includes(child.tag || ''))
const children = li.children || []

return {
type: 'element',
tag: 'div',
props: {
label,
},
children,
}
})

// For now we only check if there is at least (1) content to generate the steps..
const stepsHaveContent = stepsChildren.some((step) => step.children.length > 0)
// When children have length > 1, it means there is more than just the text content.
const stepsHaveContent = stepsChildren.some((step) => step.children.length > 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that html tags (even inlined ones) can cause to make more children have you tried this? (also what about inline code?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh you are right. I came up with something that could be better but this is the solution I came up with:

function transformStepsList(node: ContentNode) {
  // CONVERT OL->LI to Steps
  // TODO: Find a way to opt out of this transformation if needed within markdown.
  if (node.tag === 'ol' && (node.children?.length || 0) > 0 && node.children?.[0].tag === 'li') {
    const stepsChildren = node.children.map((li) => {
      const children = li.children || []

      return {
        type: 'element',
        tag: 'div',
        children,
      }
    })

    const lastLeadingSpaceOnStep = stepsChildren.map((step) => {
      let lastLeadingSpace = -1;

      for (let i = 0; i < step.children.length; i++) {
        const child = step.children[i];
        const prevChild = step.children[i - 1];
        if (
          (child?.type === 'text' && child.value?.startsWith(' ')) ||
          (prevChild?.type === 'text' && prevChild?.value?.endsWith(' '))
        ) {
          lastLeadingSpace = i;
        }
      }

      return lastLeadingSpace;
    }).filter((step) => step > -1);

    const stepsHaveContent = stepsChildren.some((step) => {
      if (lastLeadingSpaceOnStep.length > 0) {
        return step.children.slice(lastLeadingSpaceOnStep[0], lastLeadingSpaceOnStep[0]).length > 1
      }

      return step.children.length > 1
    })

    if (stepsHaveContent) {
      node.type = 'element'
      node.tag = 'Steps'
      node.props = {}
      node.children = stepsChildren
    }
  }
}

if (stepsHaveContent) {
node.type = 'element'
node.tag = 'Steps'
Expand Down
14 changes: 12 additions & 2 deletions docs/1.guide/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,19 @@ This is automatic transformation, if you have code blocks right after each other
})
```

```ts [server/api/hello.get.ts]
export default defineEventHandler(() => {
return {
hello: 'world'
}
})
```

```vue [app.vue]
<template>
<h1>Hello World</h1>
<div>
<h1>Welcome to the homepage</h1>
</div>
</template>
```
```
Expand All @@ -149,7 +159,7 @@ export default defineEventHandler(() => {
```vue [app.vue]
<template>
<div>
<h1>Welcome to the homepage</h1>
<h1>Welcome to the homepage</h1>
</div>
</template>
```
Expand Down