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

fix(steps): simplify logic #112

wants to merge 5 commits into from

Conversation

cpreston321
Copy link
Member

πŸ”— Linked issue

#106

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Resolves #106

In this PR, I make the component more generic so it can work for all cases.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@cpreston321 cpreston321 requested a review from pi0 March 29, 2024 16:45
Copy link

cloudflare-pages bot commented Mar 29, 2024

Deploying undocs with Β Cloudflare Pages Β Cloudflare Pages

Latest commit: 020c7e8
Status:Β βœ…Β  Deploy successful!
Preview URL: https://4d58e8cb.undocs.pages.dev
Branch Preview URL: https://fix-steps.undocs.pages.dev

View logs

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
    }
  }
}

@cpreston321 cpreston321 marked this pull request as draft April 1, 2024 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

steps doesn't parse correctly with links.
2 participants