From 77a5dc0fb678e3bd515ad740a2bdbf9a89d40680 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sun, 4 Oct 2020 00:10:42 +0300 Subject: [PATCH] Implement custom syntax-highlighting --- src/App.tsx | 17 +++++++++------ src/components/CodeEditor.tsx | 40 ++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c9699ac..45c3fa4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,14 +6,19 @@ import { ErrorMessage } from './components/ErrorMessage'; import styled from 'styled-components'; import { CodeEditor } from './components/CodeEditor'; -const defaultWorkout = `Name: Hello +const defaultWorkout = `Name: Sample workout +Description: Try changing it, and see what happens below. -Warmup: 10:00 30%..60% -Interval: 20:00 100% +Warmup: 10:00 30%..75% +Interval: 15:00 100% 90rpm + @ 00:00 Start off easy + @ 01:00 Settle into rythm + @ 07:30 Half way through + @ 14:00 Final minute, stay strong! Rest: 10:00 75% -Interval: 20:00 100% -FreeRide: 10:00 -Cooldown: 10:00 60%..30% +FreeRide: 20:00 + @ 00:10 Just have some fun, riding as you wish +Cooldown: 10:00 70%..30% `; const AppContainer = styled.div` diff --git a/src/components/CodeEditor.tsx b/src/components/CodeEditor.tsx index 544d246..f07e83b 100644 --- a/src/components/CodeEditor.tsx +++ b/src/components/CodeEditor.tsx @@ -1,11 +1,49 @@ import Editor from 'react-simple-code-editor'; import styled from 'styled-components'; -export const CodeEditor = styled(Editor).attrs({ padding: 10, highlight: code => code })` +const highlight = (code: string): string => { + return code + .replace(/^(Name|Author|Description|Warmup|Rest|Interval|Cooldown|FreeRide):/gm, "$&") + .replace(/(\d{1,2}:)?\d{1,2}:\d{1,2}/g, "$&") + .replace(/\d+%/g, "$&") + .replace(/\d+rpm/g, "$&") + .replace(/\.\./g, "$&") + .replace(/@(.*?)$/gm, "@$1"); +}; + +export const CodeEditor = styled(Editor).attrs({ padding: 10, highlight })` font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 14px; line-height: 1.3; border: 1px inset #bbb; border-radius: 3px; background: #fff; + + code.keyword { + font-weight: bold; + } + code.duration { + color: #681caf; + } + code.intensity { + color: #af391c; + } + code.cadence { + color: #86af1c; + } + code.range { + color: #888; + } + code.comment { + font-style: italic; + color: #888; + .duration { + color: #8d67af; + } + } + code.comment-start { + font-weight: bold; + font-style: italic; + color: #888; + } `;