From 92bd4281e3370f52a13b357cb84af7d94ad94400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82?= <47248711+milewskibogumil@users.noreply.github.com> Date: Fri, 17 Jan 2025 16:18:59 +0100 Subject: [PATCH] fix: add id to portable text headings --- .../Blog/Post/Content/content-pt/Block.astro | 26 ++++++++++++++++--- .../Legal/Content/content-pt/Block.astro | 26 ++++++++++++++++--- .../src/components/global/DecryptTabs.astro | 2 +- .../src/components/shared/ContentNav.astro | 2 +- .../BlogPost_Collection/index.tsx | 1 - 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/apps/astro/src/components/Blog/Post/Content/content-pt/Block.astro b/apps/astro/src/components/Blog/Post/Content/content-pt/Block.astro index 8165fff..739b3ff 100644 --- a/apps/astro/src/components/Blog/Post/Content/content-pt/Block.astro +++ b/apps/astro/src/components/Blog/Post/Content/content-pt/Block.astro @@ -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) }), } --- diff --git a/apps/astro/src/components/Legal/Content/content-pt/Block.astro b/apps/astro/src/components/Legal/Content/content-pt/Block.astro index 6201646..b6192d5 100644 --- a/apps/astro/src/components/Legal/Content/content-pt/Block.astro +++ b/apps/astro/src/components/Legal/Content/content-pt/Block.astro @@ -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) }), } --- diff --git a/apps/astro/src/components/global/DecryptTabs.astro b/apps/astro/src/components/global/DecryptTabs.astro index 2badbcd..d160c10 100644 --- a/apps/astro/src/components/global/DecryptTabs.astro +++ b/apps/astro/src/components/global/DecryptTabs.astro @@ -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' diff --git a/apps/astro/src/components/shared/ContentNav.astro b/apps/astro/src/components/shared/ContentNav.astro index a41b910..bf3e905 100644 --- a/apps/astro/src/components/shared/ContentNav.astro +++ b/apps/astro/src/components/shared/ContentNav.astro @@ -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 = { diff --git a/apps/sanity/schema/collectionTypes/BlogPost_Collection/index.tsx b/apps/sanity/schema/collectionTypes/BlogPost_Collection/index.tsx index 08d0315..bd6bdaa 100644 --- a/apps/sanity/schema/collectionTypes/BlogPost_Collection/index.tsx +++ b/apps/sanity/schema/collectionTypes/BlogPost_Collection/index.tsx @@ -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 }