Skip to content

Commit

Permalink
fix: add id to portable text headings
Browse files Browse the repository at this point in the history
  • Loading branch information
milewskibogumil committed Jan 17, 2025
1 parent 522ba8d commit 92bd428
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
26 changes: 23 additions & 3 deletions apps/astro/src/components/Blog/Post/Content/content-pt/Block.astro
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
---
import { toPlainText } from 'astro-portabletext/utils'
import type { BlockProps } from 'astro-portabletext/types'
import { slugify } from '@repo/utils/slugify'
type Props = BlockProps
// eslint-disable-next-line no-unused-vars
const { node, index, isInline, ...attrs } = Astro.props
const styleIs = (style: string) => style === node.style
// Recursive function to get text from nested children
const getTextFromNode = (node: any): string => {
if (typeof node === 'string') return node
if (!node) return ''
if (node.text) return node.text
if (node.children) return node.children.map((child: any) => getTextFromNode(child)).join('')
return ''
}
const rawText =
node.children
?.map((child) => {
if (child._type === '@text') return child.text
if (child._type === '@span') return getTextFromNode(child)
return ''
})
.join('') || ''
const text = rawText.replace('', '')
const props = {
...attrs,
id: slugify(toPlainText(node)),
...(text && { id: slugify(text) }),
}
---

Expand Down
26 changes: 23 additions & 3 deletions apps/astro/src/components/Legal/Content/content-pt/Block.astro
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
---
import { toPlainText } from 'astro-portabletext/utils'
import type { BlockProps } from 'astro-portabletext/types'
import { slugify } from '@repo/utils/slugify'
type Props = BlockProps
// eslint-disable-next-line no-unused-vars
const { node, index, isInline, ...attrs } = Astro.props
const styleIs = (style: string) => style === node.style
// Recursive function to get text from nested children
const getTextFromNode = (node: any): string => {
if (typeof node === 'string') return node
if (!node) return ''
if (node.text) return node.text
if (node.children) return node.children.map((child: any) => getTextFromNode(child)).join('')
return ''
}
const rawText =
node.children
?.map((child) => {
if (child._type === '@text') return child.text
if (child._type === '@span') return getTextFromNode(child)
return ''
})
.join('') || ''
const text = rawText.replace('', '')
const props = {
...attrs,
id: slugify(toPlainText(node)),
...(text && { id: slugify(text) }),
}
---

Expand Down
2 changes: 1 addition & 1 deletion apps/astro/src/components/global/DecryptTabs.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { toPlainText } from 'astro-portabletext/utils'
import { toPlainText } from 'astro-portabletext'
import type { PortableTextBlock, PortableTextSpan } from '@portabletext/types'
import PortableText, { PortableTextQuery, type PortableTextValue } from '@repo/ui/portable-text'
import CategoryTab from '@repo/ui/CategoryTab'
Expand Down
2 changes: 1 addition & 1 deletion apps/astro/src/components/shared/ContentNav.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import type { PortableTextProps } from 'astro-portabletext/types'
import type { PortableTextBlock } from '@portabletext/types'
import { toPlainText } from 'astro-portabletext/utils'
import { toPlainText } from 'astro-portabletext'
import { slugify } from '@repo/utils/slugify'
type Props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default defineType({
options: {
filter: ({ document }) => {
const language = (document as { language?: string })?.language;
console.log(language)
return {
filter: 'language == $language',
params: { language: language }
Expand Down

0 comments on commit 92bd428

Please sign in to comment.