Skip to content

Commit

Permalink
Merge pull request #30 from stoplightio/bug/sl-715/select-unsupported…
Browse files Browse the repository at this point in the history
…-language-throws-exception

fix(code-editor): fail gracefully when cannot highlight code SL-715
  • Loading branch information
chris-miaskowski authored Nov 27, 2018
2 parents 64d5b86 + ffb589c commit e57f532
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ export interface ICodeEditorProps {
onChange?: (code: string) => any;
}

const defaultSupport = {
css: 'CSS',
javascript: 'JavaScript',
markup: 'Markup',
clike: 'C-like',
};

const optionalSupport = {
// cpp: 'C++',
// csharp: 'C#',
// go: 'Go',
// html: 'HTML',
// http: 'HTTP',
// java: 'Java',
// json: 'JSON',
// jsx: 'JSX',
// markdown: 'Markdown',
// objectivec: 'Objective-C',
// perl: 'Perl',
// php: 'PHP',
// python: 'Python',
// ruby: 'Ruby',
// scala: 'Scala',
// bash: 'Bash',
// swift: 'Swift',
// yaml: 'YAML',
};

/**
* List of supported languages: https://prismjs.com/#languages-list
*/
export const supportedLanguages = {
...defaultSupport,
...optionalSupport,
};

const CodeEditorView = (props: ICodeEditorProps & { className: string }) => {
const { className, language, onChange = noop, value } = props;

Expand Down
6 changes: 2 additions & 4 deletions src/__stories__/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ const store = new Store({

export const codeEditorKnobs = (tabName = 'Code Editor') => {
return {
language: text('language', 'js', tabName),
language: text('language', 'javascript', tabName),
value: text('value', 'const defaultValue = stoplight.io();', tabName),
style: object('style', { fontSize: '12px' }, tabName),
};
};

storiesOf('CodeEditor', module)
.addDecorator(withKnobs)
.add('with defaults', () => (
<CodeEditor {...codeEditorKnobs()} onChange={action('onChange')} />
))
.add('with defaults', () => <CodeEditor {...codeEditorKnobs()} onChange={action('onChange')} />)
.addDecorator(StateDecorator(store))
.add('with store', () => (
<CodeEditor {...codeEditorKnobs()} value={store.get('value')} onChange={(value: string) => store.set({ value })} />
Expand Down
3 changes: 2 additions & 1 deletion src/utils/highlightCode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { highlight, languages } from 'prismjs';

export const highlightCode = (code: string, language: string) => {
return highlight(code, languages[language]);
const langDef = languages[language];
return langDef ? highlight(code, langDef) : code;
};

0 comments on commit e57f532

Please sign in to comment.