From 6ab9635a22d69186c8a24181ad5df7736e288577 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Fri, 17 Nov 2023 17:27:54 +0100
Subject: fix: generate an id for each headings in the page main contents

Since #be4d907 the ids was no longer addded to headings in
useHeadingsTree hook. It was a bad practice to manipulate the DOM
that way. However, I did not move the implementation elsewhere...
To fix this, I now use rehype-slug on both markdown contents and
html string coming from WordPress.
I'm not sure the dynamic imports are really useful here since the
table of contents is on almost all pages but Jest was failing with
regular import because of ESM. It is the only thing that makes the
tests functional again so... However if we want to test the
`updateContentTree` function, Jest fails for the same reason. So I
decided to not test this function. I've already spend too much time
on this issue.
Another problem: the ToC on projects page. Currently we use the ref
on the body but the page contents are imported dynamically so the
hook is executed before the contents are loaded. It makes the ToC
empty... We should refactor the pages so we can use the ref
directly on the imported contents.
---
 src/components/templates/page/page-layout.tsx | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

(limited to 'src/components/templates/page/page-layout.tsx')

diff --git a/src/components/templates/page/page-layout.tsx b/src/components/templates/page/page-layout.tsx
index 8ea0087..db71e07 100644
--- a/src/components/templates/page/page-layout.tsx
+++ b/src/components/templates/page/page-layout.tsx
@@ -4,14 +4,13 @@ import {
   type FC,
   type HTMLAttributes,
   type ReactNode,
-  useRef,
   useCallback,
 } from 'react';
 import { useIntl } from 'react-intl';
 import type { BreadcrumbList } from 'schema-dts';
 import { sendComment } from '../../../services/graphql';
 import type { SendCommentInput } from '../../../types';
-import { useHeadingsTree, useIsMounted } from '../../../utils/hooks';
+import { useHeadingsTree } from '../../../utils/hooks';
 import { Heading, Sidebar } from '../../atoms';
 import {
   PageFooter,
@@ -137,9 +136,9 @@ export const PageLayout: FC<PageLayoutProps> = ({
     id: 'eys2uX',
   });
 
-  const bodyRef = useRef<HTMLDivElement>(null);
-  const isMounted = useIsMounted(bodyRef);
-  const headingsTree = useHeadingsTree(bodyRef, { fromLevel: 2 });
+  const { ref: bodyRef, tree: headingsTree } = useHeadingsTree<HTMLDivElement>({
+    fromLevel: 2,
+  });
 
   const saveComment: CommentFormSubmit = useCallback(
     async (data) => {
@@ -223,12 +222,10 @@ export const PageLayout: FC<PageLayoutProps> = ({
           })}
           className={`${styles.sidebar} ${styles['sidebar--first']}`}
         >
-          {isMounted && bodyRef.current ? (
-            <TocWidget
-              heading={<Heading level={3}>{tocTitle}</Heading>}
-              tree={headingsTree}
-            />
-          ) : null}
+          <TocWidget
+            heading={<Heading level={3}>{tocTitle}</Heading>}
+            tree={headingsTree}
+          />
         </Sidebar>
       ) : null}
       {typeof children === 'string' ? (
-- 
cgit v1.2.3

