Skip to content

Commit

Permalink
Introduce early return, ensip sorting, and extrapolate regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 4, 2024
1 parent 3fd9740 commit 9224944
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
22 changes: 19 additions & 3 deletions app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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...');

Expand Down Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions app/src/specs/validateFrontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/src/util/regex.ts
Original file line number Diff line number Diff line change
@@ -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]): /;

0 comments on commit 9224944

Please sign in to comment.