From 8557df4753795039dd158cd0d5aa9fc35b5816d8 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Fri, 15 Mar 2024 22:19:53 +0100 Subject: [PATCH] Added anchor hash to break blocks --- .../Pages/endpoints/getBySlugEndpoint.ts | 35 +++++++++++++------ src/constants.ts | 4 ++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/collections/Pages/endpoints/getBySlugEndpoint.ts b/src/collections/Pages/endpoints/getBySlugEndpoint.ts index 6d0dff6..c5eed66 100644 --- a/src/collections/Pages/endpoints/getBySlugEndpoint.ts +++ b/src/collections/Pages/endpoints/getBySlugEndpoint.ts @@ -2,7 +2,9 @@ import { BreakBlockType, Collections, PageType, + RichTextBreakBlock, RichTextContent, + RichTextSectionBlock, isBlockNodeBreakBlock, isBlockNodeSectionBlock, isNodeBlockNode, @@ -57,22 +59,33 @@ const handleContent = ( { root: { children, ...others } }: RichTextContent, parentPrefix = "" ): RichTextContent => { - let sectionCount = 0; + let index = 0; return { root: { ...others, children: children.map((node) => { - if (isNodeBlockNode(node) && isBlockNodeSectionBlock(node)) { - sectionCount++; - const anchorHash = `${parentPrefix}${sectionCount}.`; - return { - ...node, - fields: { - ...node.fields, + if (isNodeBlockNode(node)) { + if (isBlockNodeSectionBlock(node)) { + index++; + const anchorHash = `${parentPrefix}${index}.`; + const newNode: RichTextSectionBlock = { + ...node, + fields: { + ...node.fields, + content: handleContent(node.fields.content, anchorHash), + }, anchorHash, - content: handleContent(node.fields.content, anchorHash), - }, - }; + }; + return newNode; + } else if (isBlockNodeBreakBlock(node)) { + index++; + const anchorHash = `${parentPrefix}${index}.`; + const newNode: RichTextBreakBlock = { + ...node, + anchorHash, + }; + return newNode; + } } return node; }), diff --git a/src/constants.ts b/src/constants.ts index 61231f0..7551ef0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -198,7 +198,8 @@ export interface RichTextBlockNode extends RichTextNode { } export interface RichTextSectionBlock extends RichTextBlockNode { - fields: SectionBlock & { anchorHash: string }; + fields: SectionBlock; + anchorHash: string; } export interface RichTextTranscriptBlock extends RichTextBlockNode { @@ -207,6 +208,7 @@ export interface RichTextTranscriptBlock extends RichTextBlockNode { export interface RichTextBreakBlock extends RichTextBlockNode { fields: BreakBlock; + anchorHash: string; } export const isNodeParagraphNode = (node: RichTextNode): node is RichTextParagraphNode =>