summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-16 19:40:23 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-16 19:40:23 +0200
commitc77c58e18143233be042c4980a6ed08ae9beac52 (patch)
tree94f7d828571a86470ae299fff7dffd32fb38de7c /src/utils
parent2155550fa36a3bc3c8f66e0926530123b4018cd4 (diff)
chore: adjust and complete missing styles
* add logo to topics pages and links * add Prism styles to articles * and a few other adjustements
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/helpers/pages.ts5
-rw-r--r--src/utils/hooks/use-add-prism-class-attr.tsx60
2 files changed, 64 insertions, 1 deletions
diff --git a/src/utils/helpers/pages.ts b/src/utils/helpers/pages.ts
index 93582f0..62337db 100644
--- a/src/utils/helpers/pages.ts
+++ b/src/utils/helpers/pages.ts
@@ -5,12 +5,14 @@ import {
type RawThematicPreview,
type RawTopicPreview,
} from '@ts/types/raw-data';
+import { getImageFromRawData } from './images';
/**
* Convert raw data to a Link object.
*
* @param data - An object.
* @param {number} data.databaseId - The data id.
+ * @param {number} [data.logo] - The data logo.
* @param {string} data.slug - The data slug.
* @param {string} data.title - The data name.
* @returns {PageLink} The link data (id, slug and title).
@@ -18,10 +20,11 @@ import {
export const getPageLinkFromRawData = (
data: RawThematicPreview | RawTopicPreview
): PageLink => {
- const { databaseId, slug, title } = data;
+ const { databaseId, featuredImage, slug, title } = data;
return {
id: databaseId,
+ logo: featuredImage ? getImageFromRawData(featuredImage?.node) : undefined,
name: title,
slug,
};
diff --git a/src/utils/hooks/use-add-prism-class-attr.tsx b/src/utils/hooks/use-add-prism-class-attr.tsx
new file mode 100644
index 0000000..7d33cc2
--- /dev/null
+++ b/src/utils/hooks/use-add-prism-class-attr.tsx
@@ -0,0 +1,60 @@
+import { useCallback, useEffect, useState } from 'react';
+
+export type AttributesMap = {
+ [key: string]: string;
+};
+
+export type useAddPrismClassAttrProps = {
+ attributes?: AttributesMap;
+ classNames?: string;
+};
+
+/**
+ * Add classnames and/or attributes to pre elements.
+ *
+ * @param props - An object of attributes and classnames.
+ */
+const useAddPrismClassAttr = ({
+ attributes,
+ classNames,
+}: useAddPrismClassAttrProps) => {
+ const [elements, setElements] = useState<HTMLPreElement[]>([]);
+
+ useEffect(() => {
+ const targetElements = document.querySelectorAll('pre');
+ setElements(Array.from(targetElements));
+ }, []);
+
+ const setClassNameAndAttributes = useCallback(
+ (array: HTMLElement[]) => {
+ array.forEach((el) => {
+ if (classNames) {
+ const classNamesArray = classNames.split(' ');
+ const isCommandLine = el.classList.contains('command-line');
+ const removedClassName = isCommandLine
+ ? 'line-numbers'
+ : 'command-line';
+ const filteredClassNames = classNamesArray.filter(
+ (className) => className !== removedClassName
+ );
+ filteredClassNames.forEach((className) =>
+ el.classList.add(className)
+ );
+ }
+
+ if (attributes) {
+ for (const [key, value] of Object.entries(attributes)) {
+ el.setAttribute(key, value);
+ }
+ }
+ });
+ },
+ [attributes, classNames]
+ );
+
+ useEffect(() => {
+ if (elements.length > 0) setClassNameAndAttributes(elements);
+ }, [elements, setClassNameAndAttributes]);
+};
+
+export default useAddPrismClassAttr;