Skip to content

Commit

Permalink
Always enable syntax highlighting, minor refactor
Browse files Browse the repository at this point in the history
- Extract styles into module.css
- Remove obsolete types
  • Loading branch information
AJGeel committed Nov 15, 2024
1 parent f07caae commit 8135f9f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use client';

import { ReactElement, useState } from 'react';
import { ReactElement } from 'react';
import { taskfile } from '@/components/Generator/GeneredTaskfile/taskfile';
import { useFormContext } from 'react-hook-form';
import { GeneratorSettings } from '@/components/Generator/Generator';
import CopyToClipboard from '@/components/Generator/GeneredTaskfile/Copy';
import { highlighter } from './Highlighter';

const GeneratedTaskfile = (): ReactElement => {
const [showHighlighting, setShowHighlighting] = useState(true);

const form = useFormContext<GeneratorSettings>();

const settings = form.watch();
Expand All @@ -19,9 +17,7 @@ const GeneratedTaskfile = (): ReactElement => {
return (
<>
<CopyToClipboard onCopy={() => navigator.clipboard.writeText(resultTaskfile)} />
<pre onClick={() => setShowHighlighting(!showHighlighting)}>
{showHighlighting ? highlighter(resultTaskfile) : resultTaskfile}
</pre>
<pre>{highlighter(resultTaskfile)}</pre>
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ReactElement } from 'react';

import { styles } from './styles';
import styles from './highlighter.module.css';

import { lineRenderers } from './lineRenderers';

export const highlighter = (code: string): ReactElement[] => {
Expand All @@ -15,7 +16,7 @@ export const highlighter = (code: string): ReactElement[] => {
}

return (
<div key={index} style={styles.colors.white}>
<div key={index} className={styles['text-white']}>
{line}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.line {
display: flex;
}

.text-white {
color: #fff;
}

.text-gray {
color: #888589;
}

.text-yellow {
color: #fde047;
}

.text-yellow-light {
color: #fef08a;
}

.text-pink {
color: #ec4899;
}

.text-purple {
color: #a855f7;
}

.text-blue {
color: #60a5fa;
}

.text-blue-light {
color: #93c5fd;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';

import { LineRenderer, RendererType } from './types';
import { styles } from './styles';

import styles from './highlighter.module.css';

export const lineRenderers: LineRenderer[] = [
{
Expand All @@ -15,10 +16,10 @@ export const lineRenderers: LineRenderer[] = [
render: (line, i) => {
const [, name, rest] = line.match(/^function\s+([a-zA-Z_:]+)(.*)/) || [];
return (
<div key={i} style={styles.line}>
<span style={styles.colors.purple}>function </span>
<span style={styles.colors.yellow}>{name}</span>
<span style={styles.colors.white}>{rest}</span>
<div key={i} className={styles.line}>
<span className={styles['text-purple']}>function </span>
<span className={styles['text-yellow']}>{name}</span>
<span className={styles['text-white']}>{rest}</span>
</div>
);
},
Expand All @@ -27,7 +28,7 @@ export const lineRenderers: LineRenderer[] = [
type: RendererType.Comments,
test: (line) => line.trim().startsWith('#'),
render: (line, i) => (
<div key={i} style={styles.colors.gray}>
<div key={i} className={styles['text-gray']}>
{line}
</div>
),
Expand All @@ -38,10 +39,10 @@ export const lineRenderers: LineRenderer[] = [
render: (line, i) => {
const [varName, ...rest] = line.split('=');
return (
<div key={i} style={styles.line}>
<span style={styles.colors.blue}>{varName}</span>
<span style={styles.colors.white}>=</span>
<span style={styles.colors.lightYellow}>{rest.join('=')}</span>
<div key={i} className={styles.line}>
<span className={styles['text-blue']}>{varName}</span>
<span className={styles['text-white']}>=</span>
<span className={styles['text-yellow-light']}>{rest.join('=')}</span>
</div>
);
},
Expand All @@ -50,7 +51,7 @@ export const lineRenderers: LineRenderer[] = [
type: RendererType.Conditionals,
test: (line) => /^\s*if\s+|^\s*then\s+|^\s*else\s+|^\s*fi\s*/.test(line),
render: (line, i) => (
<div key={i} style={styles.colors.pink}>
<div key={i} className={styles['text-pink']}>
{line}
</div>
),
Expand All @@ -61,10 +62,10 @@ export const lineRenderers: LineRenderer[] = [
render: (line, i) => {
const parts = line.split('echo');
return (
<div key={i} style={styles.line}>
<span style={styles.colors.white}>{parts[0]}</span>
<span style={styles.colors.lightBlue}>echo</span>
<span style={styles.colors.lightYellow}>{parts[1]}</span>
<div key={i} className={styles.line}>
<span className={styles['text-white']}>{parts[0]}</span>
<span className={styles['text-blue-light']}>echo</span>
<span className={styles['text-yellow-light']}>{parts[1]}</span>
</div>
);
},
Expand Down
18 changes: 0 additions & 18 deletions src/components/Generator/GeneredTaskfile/Highlighter/styles.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/components/Generator/GeneredTaskfile/Highlighter/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { ReactElement } from 'react';

type ColorName = 'white' | 'purple' | 'yellow' | 'green' | 'blue' | 'pink' | 'lightYellow' | 'lightBlue' | 'gray';

export type StylesConfig = {
line: { display: string };
colors: Record<ColorName, { color: string }>;
};

export enum RendererType {
EmptyLines,
FunctionDefinitions,
Expand Down

0 comments on commit 8135f9f

Please sign in to comment.