From 9c8921db92d16b07ffc2a63ff3c80c4dcdd9ff9d Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 10 May 2022 17:38:07 +0200 Subject: chore: add Project single pages --- src/utils/helpers/strings.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/utils/helpers/strings.ts (limited to 'src/utils/helpers/strings.ts') diff --git a/src/utils/helpers/strings.ts b/src/utils/helpers/strings.ts new file mode 100644 index 0000000..5d90161 --- /dev/null +++ b/src/utils/helpers/strings.ts @@ -0,0 +1,29 @@ +/** + * Convert a text into a slug or id. + * https://gist.github.com/codeguy/6684588#gistcomment-3332719 + * + * @param {string} text - A text to slugify. + * @returns {string} The slug. + */ +export const slugify = (text: string): string => { + return text + .toString() + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .toLowerCase() + .trim() + .replace(/\s+/g, '-') + .replace(/[^\w\-]+/g, '-') + .replace(/\-\-+/g, '-') + .replace(/(^-)|(-$)/g, ''); +}; + +/** + * Capitalize the first letter of a string. + * + * @param {string} text - A text to capitalize. + * @returns {string} The capitalized text. + */ +export const capitalize = (text: string): string => { + return text.replace(/^\w/, (firstLetter) => firstLetter.toUpperCase()); +}; -- cgit v1.2.3 From bbd63400f94b43fde04449e0c71d14763d893e6a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 19 May 2022 19:46:24 +0200 Subject: refactor: rewrite Prism hooks and providers It avoid some hydratation errors on project pages (not in article however) and the hooks are now reusable. --- src/components/molecules/forms/motion-toggle.tsx | 3 +- src/components/molecules/layout/code.tsx | 25 +--- src/components/templates/page/page-layout.tsx | 5 +- src/pages/article/[slug].tsx | 54 +++++-- src/styles/pages/partials/_article-prism.scss | 2 +- src/utils/helpers/strings.ts | 10 ++ src/utils/hooks/use-add-classname.tsx | 34 +++++ src/utils/hooks/use-add-prism-class-attr.tsx | 60 -------- src/utils/hooks/use-attributes.tsx | 35 ++++- src/utils/hooks/use-code-blocks-theme.tsx | 22 --- src/utils/hooks/use-prism-plugins.tsx | 115 -------------- src/utils/hooks/use-prism.tsx | 182 +++++++++++++++++++++++ src/utils/hooks/use-query-selector-all.tsx | 12 +- src/utils/providers/prism-theme.tsx | 78 ++++------ 14 files changed, 346 insertions(+), 291 deletions(-) create mode 100644 src/utils/hooks/use-add-classname.tsx delete mode 100644 src/utils/hooks/use-add-prism-class-attr.tsx delete mode 100644 src/utils/hooks/use-code-blocks-theme.tsx delete mode 100644 src/utils/hooks/use-prism-plugins.tsx create mode 100644 src/utils/hooks/use-prism.tsx (limited to 'src/utils/helpers/strings.ts') diff --git a/src/components/molecules/forms/motion-toggle.tsx b/src/components/molecules/forms/motion-toggle.tsx index e3bb11a..cbe38fe 100644 --- a/src/components/molecules/forms/motion-toggle.tsx +++ b/src/components/molecules/forms/motion-toggle.tsx @@ -33,7 +33,8 @@ const MotionToggle: FC = ({ value ); useAttributes({ - attribute: 'reducedMotion', + element: document.documentElement || undefined, + attribute: 'reduced-motion', value: `${isReduced}`, }); diff --git a/src/components/molecules/layout/code.tsx b/src/components/molecules/layout/code.tsx index 2959ae5..f5b60b9 100644 --- a/src/components/molecules/layout/code.tsx +++ b/src/components/molecules/layout/code.tsx @@ -1,7 +1,5 @@ -import usePrismPlugins, { - type PrismPlugin, -} from '@utils/hooks/use-prism-plugins'; -import { FC } from 'react'; +import usePrism, { OptionalPrismPlugin } from '@utils/hooks/use-prism'; +import { FC, useRef } from 'react'; import styles from './code.module.scss'; export type PrismLanguage = @@ -35,15 +33,6 @@ export type PrismLanguage = | 'twig' | 'yaml'; -export type OptionalPrismPlugin = Extract< - PrismPlugin, - | 'command-line' - | 'diff-highlight' - | 'inline-color' - | 'line-highlight' - | 'line-numbers' ->; - export type CodeProps = { /** * The code to highlight. @@ -79,17 +68,19 @@ const Code: FC = ({ plugins = [], outputPattern = '#output#', }) => { - const { pluginsAttribute, pluginsClassName } = usePrismPlugins(plugins); + const wrapperRef = useRef(null); + const { attributes, className } = usePrism({ language, plugins }); const outputAttribute = filterOutput ? { 'data-filter-output': outputPattern } : {}; return ( -
+
         {children}
diff --git a/src/components/templates/page/page-layout.tsx b/src/components/templates/page/page-layout.tsx
index 54b8d6e..8ff44d5 100644
--- a/src/components/templates/page/page-layout.tsx
+++ b/src/components/templates/page/page-layout.tsx
@@ -20,7 +20,6 @@ import CommentsList, {
 import TableOfContents from '@components/organisms/widgets/table-of-contents';
 import { type SendCommentVars } from '@services/graphql/api';
 import { sendComment } from '@services/graphql/comments';
-import useCodeBlocksTheme from '@utils/hooks/use-code-blocks-theme';
 import useIsMounted from '@utils/hooks/use-is-mounted';
 import Script from 'next/script';
 import { FC, HTMLAttributes, ReactNode, useRef, useState } from 'react';
@@ -181,11 +180,9 @@ const PageLayout: FC = ({
    * @param {MetaData} meta - The metadata.
    */
   const hasMeta = (meta: MetaData) => {
-    return Object.values(meta).every((value) => value === null);
+    return Object.values(meta).every((value) => value);
   };
 
-  useCodeBlocksTheme(bodyRef);
-
   return (
     <>