From bf9c64da68d01732490d4006612025dd7a1d5270 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 11:41:24 +0200 Subject: [PATCH] refactor: Rearrange import statements in eslint.config.js --- app/eslint.config.js | 16 ++++++-------- app/hooks/useAutoComplete.ts | 37 --------------------------------- app/layouts/LayoutDefault.tsx | 2 +- app/lib/search.ts | 26 +++++++++++++---------- app/lib/sections.ts | 3 ++- app/markdoc/Tag.ts | 8 ++++++- app/markdoc/nodes.tsx | 2 +- app/markdoc/tags.tsx | 10 ++++----- app/pages/+Head.tsx | 1 + app/pages/_error/+Page.tsx | 4 +++- app/pages/index/+data.ts | 2 +- app/providers/ThemeProvider.tsx | 2 +- app/services/SnippetsService.ts | 2 -- 13 files changed, 43 insertions(+), 72 deletions(-) delete mode 100644 app/hooks/useAutoComplete.ts diff --git a/app/eslint.config.js b/app/eslint.config.js index 1ba6c0c..23320c6 100644 --- a/app/eslint.config.js +++ b/app/eslint.config.js @@ -1,10 +1,10 @@ // @ts-nocheck -import eslint from "@eslint/js"; -import prettier from "eslint-plugin-prettier/recommended"; import react from "eslint-plugin-react/configs/recommended.js"; -import globals from "globals"; +import prettier from "eslint-plugin-prettier/recommended"; import tseslint from "typescript-eslint"; +import eslint from "@eslint/js"; +import globals from "globals"; export default tseslint.config( { @@ -33,14 +33,10 @@ export default tseslint.config( }, { rules: { - "@typescript-eslint/no-unused-vars": [ - 1, - { - argsIgnorePattern: "^_", - }, - ], + "@typescript-eslint/no-unused-vars": [1, { argsIgnorePattern: "^_" }], "@typescript-eslint/no-namespace": 0, - "react/react-in-jsx-scope": false, + "react/react-in-jsx-scope": "warn", + "react/jsx-filename-extension": [1, { extensions: [".tsx"] }], }, }, diff --git a/app/hooks/useAutoComplete.ts b/app/hooks/useAutoComplete.ts deleted file mode 100644 index 41dafa9..0000000 --- a/app/hooks/useAutoComplete.ts +++ /dev/null @@ -1,37 +0,0 @@ -import type { SearchResult } from "@/lib/search"; - -import { useDebounce } from "./useDebounce"; -import { useState, useEffect, use } from "react"; - -export function useAutoComplete(onSearch: (query: string) => Promise) { - const [results, setResults] = useState([]); - const [isLoading, setIsLoading] = useState(false); - const [isOpened, setIsOpened] = useState(false); - const [query, setQuery] = useDebounce(); - - useEffect(() => { - // Attach the event listener to the window - function handleKeyDown(event: KeyboardEvent) { - if (event.key === "Escape") { - setIsOpened(false); - } else if (event.key === "K" && (event.ctrlKey || event.metaKey)) { - event.preventDefault(); - setIsOpened(true); - } - } - - window.addEventListener("keydown", handleKeyDown); - - return () => { - window.removeEventListener("keydown", handleKeyDown); - }; - }, []); - - return { - results, - isLoading, - isOpened, - query, - setQuery, - }; -} diff --git a/app/layouts/LayoutDefault.tsx b/app/layouts/LayoutDefault.tsx index 0cd38ce..24227b3 100644 --- a/app/layouts/LayoutDefault.tsx +++ b/app/layouts/LayoutDefault.tsx @@ -27,7 +27,7 @@ function GitHubIcon(props: React.ComponentPropsWithoutRef<"svg">) { } function Header() { - let [isScrolled, setIsScrolled] = useState(false); + const [isScrolled, setIsScrolled] = useState(false); useEffect(() => { function onScroll() { diff --git a/app/lib/search.ts b/app/lib/search.ts index d8d79ff..bb3ad9e 100644 --- a/app/lib/search.ts +++ b/app/lib/search.ts @@ -5,6 +5,8 @@ import glob from "fast-glob"; import * as path from "path"; import * as fs from "fs"; +type SearchOptionValue = string | number | boolean | null | undefined | object | unknown; + const slugify = slugifyWithCounter(); interface Node { @@ -32,20 +34,22 @@ export interface SearchResult { function toString(node: Node): string { let str = node.type === "text" && typeof node.attributes?.content === "string" ? node.attributes.content : ""; + if ("children" in node) { - for (let child of node.children!) { + for (const child of node.children!) { str += toString(child); } } + return str; } function extractSections(node: Node, sections: Section[], isRoot: boolean = true): void { - if (isRoot) { - slugify.reset(); - } + if (isRoot) slugify.reset(); + if (node.type === "heading" || node.type === "paragraph") { - let content = toString(node).trim(); + const content = toString(node).trim(); + if (node.type === "heading" && node.attributes?.level! <= 2) { let hash = node.attributes?.id ?? slugify(content); sections.push({ content, hash, subsections: [] }); @@ -53,7 +57,7 @@ function extractSections(node: Node, sections: Section[], isRoot: boolean = true sections[sections.length - 1].subsections.push(content); } } else if ("children" in node) { - for (let child of node.children!) { + for (const child of node.children!) { extractSections(child, sections, false); } } @@ -111,18 +115,18 @@ export function buildSearchIndex(pagesDir: string): FlexSearch.Document, query: string, - options: Record = {}, + options: Record = {}, ): SearchResult[] { const results = sectionIndex.search(query, { ...options, enrich: true, }); - if (results.length === 0) { - return []; - } + if (results.length === 0) return []; - return results[0].result.map((item: any) => ({ + const searchResults = results[0].result as unknown as { id: string; doc: { title: string; pageTitle: string } }[]; + + return searchResults.map((item) => ({ url: item.id, title: item.doc.title, pageTitle: item.doc.pageTitle, diff --git a/app/lib/sections.ts b/app/lib/sections.ts index 76d2a7a..2006737 100644 --- a/app/lib/sections.ts +++ b/app/lib/sections.ts @@ -41,7 +41,8 @@ function isH3Node(node: Node): node is H3Node { function getNodeText(node: Node) { let text = ""; - for (let child of node.children ?? []) { + + for (const child of node.children ?? []) { if (child.type === "text") { text += child.attributes.content; } diff --git a/app/markdoc/Tag.ts b/app/markdoc/Tag.ts index 8f5dc84..e50b59d 100644 --- a/app/markdoc/Tag.ts +++ b/app/markdoc/Tag.ts @@ -3,8 +3,14 @@ import type { ReactNode } from "react"; import { Tag as MarkdocTag } from "@markdoc/markdoc"; +type TagAttributesValue = string | number | boolean | null | undefined | object | unknown; + export class Tag extends MarkdocTag { - constructor(name: string | ReactNode, attributes: Record, children: RenderableTreeNode[]) { + constructor( + name: string | ReactNode, + attributes: Record, + children: RenderableTreeNode[], + ) { // Workaround for TypeScript's type system super(name as unknown as string, attributes, children); } diff --git a/app/markdoc/nodes.tsx b/app/markdoc/nodes.tsx index 7c1c656..3c32b0f 100644 --- a/app/markdoc/nodes.tsx +++ b/app/markdoc/nodes.tsx @@ -8,7 +8,7 @@ import { Fence } from "@syntax/Fence"; import { Tag } from "./Tag"; import yaml from "js-yaml"; -let documentSlugifyMap = new Map(); +const documentSlugifyMap = new Map(); const nodes = { document: { diff --git a/app/markdoc/tags.tsx b/app/markdoc/tags.tsx index e3098b8..9abbbb8 100644 --- a/app/markdoc/tags.tsx +++ b/app/markdoc/tags.tsx @@ -36,10 +36,10 @@ const tags = { alt: { type: String }, caption: { type: String }, }, - render: ({ src, alt = "", caption }: { src: string; alt: string; caption: string }) => ( + render: (props: { src: string; alt: string; caption: string }) => (
- {alt} -
{caption}
+ {props.alt} +
{props.caption}
), }, @@ -85,8 +85,8 @@ const tags = { }, }, img: { - render: ({ src, alt = "", className = "" }: { src: string; alt: string; className: string }) => ( - {alt} + render: (props: { src: string; alt: string; className: string }) => ( + {props.alt} ), attributes: { src: { type: String }, diff --git a/app/pages/+Head.tsx b/app/pages/+Head.tsx index 48371af..cb6c941 100644 --- a/app/pages/+Head.tsx +++ b/app/pages/+Head.tsx @@ -1,4 +1,5 @@ import logoUrl from "@/assets/logo.svg"; +import React from "react"; export default function HeadDefault() { return ( diff --git a/app/pages/_error/+Page.tsx b/app/pages/_error/+Page.tsx index b8448b0..92d0fa7 100644 --- a/app/pages/_error/+Page.tsx +++ b/app/pages/_error/+Page.tsx @@ -1,8 +1,10 @@ import { usePageContext } from "vike-react/usePageContext"; import { Link } from "@/components/common/Link"; +import React from "react"; export default function Page() { const { is404 } = usePageContext(); + if (is404) { return ( <> @@ -16,7 +18,7 @@ export default function Page() { Désolé, nous ne pouvons pas trouver la page que vous recherchez.

- Retour à l'accueil + Retour à l'accueil diff --git a/app/pages/index/+data.ts b/app/pages/index/+data.ts index d96d7f3..1cc024d 100644 --- a/app/pages/index/+data.ts +++ b/app/pages/index/+data.ts @@ -9,7 +9,7 @@ import { render } from "vike/abort"; export type Data = Awaited>; -export async function data(pageContext: PageContext) { +export async function data(_pageContext: PageContext) { const config = useConfig(); const doc = await docsService.getDoc("docs", "index"); diff --git a/app/providers/ThemeProvider.tsx b/app/providers/ThemeProvider.tsx index cabcac4..9d248f4 100644 --- a/app/providers/ThemeProvider.tsx +++ b/app/providers/ThemeProvider.tsx @@ -1,5 +1,5 @@ import { ThemeContext, type Theme } from "@/contexts/ThemeContext"; -import { useEffect, useState } from "react"; +import React, { useEffect, useState } from "react"; type ThemeProviderProps = { children: React.ReactNode; diff --git a/app/services/SnippetsService.ts b/app/services/SnippetsService.ts index a91393f..5d47b0d 100644 --- a/app/services/SnippetsService.ts +++ b/app/services/SnippetsService.ts @@ -1,5 +1,3 @@ -import path from "path"; - type SnippetsCache = Map; class SnippetsService {