From 9224944d220409088590e72754f21ef868285456 Mon Sep 17 00:00:00 2001 From: Luc Date: Wed, 4 Sep 2024 02:27:02 +0000 Subject: [PATCH] Introduce early return, ensip sorting, and extrapolate regexes --- app/src/index.tsx | 22 +++++++++++++++++++--- app/src/specs/validateFrontmatter.ts | 6 ++---- app/src/util/regex.ts | 5 +++++ 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 app/src/util/regex.ts diff --git a/app/src/index.tsx b/app/src/index.tsx index 4217a3a..0c4cb6f 100644 --- a/app/src/index.tsx +++ b/app/src/index.tsx @@ -12,6 +12,7 @@ import { } from './specs/validateFrontmatter'; import { extractTitle } from './specs/validateTitle'; import { TracedError } from './util/error'; +import { ENSIPNumberMatch } from './util/regex'; console.log('Building preview...'); @@ -96,12 +97,27 @@ for (const file of files) { ',line=1,col=1,endColumn=2::Unable to load file' ); } + + // eslint-disable-next-line unicorn/no-process-exit + process.exit(1); } } -ensips = ensips.sort((a, b) => - a.frontmatter.ensip.created.localeCompare(b.frontmatter.ensip.created) -); +// sort ensips by number, all titles start with ENSIP-1 to ENSIP-19, I want 19 to be the last, 1 to be the first +ensips = ensips.sort((a, b) => { + const titleA = ENSIPNumberMatch.exec(a.title)![1] as string; + const titleB = ENSIPNumberMatch.exec(b.title)![1] as string; + + if (titleA.toLowerCase() === 'x') { + return 1; + } + + if (titleB.toLowerCase() === 'x') { + return -1; + } + + return Number.parseInt(titleA, 10) - Number.parseInt(titleB, 10); +}); // Render Index await writeFile( diff --git a/app/src/specs/validateFrontmatter.ts b/app/src/specs/validateFrontmatter.ts index 7b60805..b55025f 100644 --- a/app/src/specs/validateFrontmatter.ts +++ b/app/src/specs/validateFrontmatter.ts @@ -3,6 +3,8 @@ import type { Plugin } from 'unified'; import { parse as parseYaml, YAMLParseError } from 'yaml'; import { z, ZodError } from 'zod'; +import { ENSNameRegex, GithubUsernameRegex } from '../util/regex'; + export type UnparsedFrontmatter = { type: 'yaml'; value: string; @@ -21,10 +23,6 @@ export type Frontmatter = { }; }; -// TODO: update to be more accurate -const ENSNameRegex = /^[\da-z]+\.[\da-z]+$/; -const GithubUsernameRegex = /^[\dA-Za-z]+$/; - export const FrontMatterZod = z.object({ description: z.string().min(5).max(160), contributors: z diff --git a/app/src/util/regex.ts b/app/src/util/regex.ts new file mode 100644 index 0000000..4b3692e --- /dev/null +++ b/app/src/util/regex.ts @@ -0,0 +1,5 @@ +// TODO: update to be more accurate +export const ENSNameRegex = /^[\da-z]+\.[\da-z]+$/; +export const GithubUsernameRegex = /^[\dA-Za-z]+$/; + +export const ENSIPNumberMatch = /ENSIP-(\d+|[Xx]): /;