Skip to content

Commit 674b5df

Browse files
authored
Merge pull request #28 from xsnippet/syntax-highlight
Enable code syntax highlighting on "Snippet" page
2 parents 41f83b4 + 4ca2dc9 commit 674b5df

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"eslint-plugin-react": "^7.4.0",
4747
"extract-text-webpack-plugin": "^3.0.2",
4848
"file-loader": "^1.1.5",
49+
"glob": "^7.1.2",
4950
"html-webpack-plugin": "^2.30.1",
5051
"style-loader": "^0.19.0",
5152
"stylus": "^0.54.5",

src/components/Snippet.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { connect } from 'react-redux';
33
import { Controlled as CodeMirror } from 'react-codemirror2';
44

5+
import codemirror from 'codemirror';
56
import 'codemirror/lib/codemirror.css';
67

78
import Title from './common/Title';
@@ -35,6 +36,7 @@ class Snippet extends React.Component {
3536
if (!snippet) return <Spinner />;
3637

3738
const snippetTitle = snippet.get('title') || `#${snippet.get('id')}, Untitled`;
39+
const modeInfo = codemirror.findModeByName(snippet.get('syntax'));
3840

3941
return (
4042
[
@@ -67,7 +69,7 @@ class Snippet extends React.Component {
6769
<div className="snippet-code">
6870
<CodeMirror
6971
value={`${snippet.get('content')}`}
70-
options={{ lineNumbers: true, readOnly: 'nocursor' }}
72+
options={{ lineNumbers: true, readOnly: 'nocursor', mode: modeInfo.mime }}
7173
/>
7274
<div className="snippet-code-bottom-bar">
7375
<button className="snippet-button light">Raw</button>

webpack.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const process = require('process');
33

44
const webpack = require('webpack');
55
const merge = require('webpack-merge');
6+
const glob = require('glob');
67

78
const CleanWebpackPlugin = require('clean-webpack-plugin');
89
const ExtractTextPlugin = require('extract-text-webpack-plugin');
@@ -25,7 +26,14 @@ module.exports = () => {
2526
},
2627

2728
entry: {
28-
app: path.resolve(__dirname, 'src', 'index.jsx'),
29+
app: [
30+
path.resolve(__dirname, 'src', 'index.jsx'),
31+
32+
// Bundle CodeMirror's syntaxes along with main application. There are
33+
// around 120 syntaxes and we, of course, do not want to import all of
34+
// them from within the application, hence this hack.
35+
...glob.sync(path.resolve(__dirname, 'node_modules', 'codemirror', 'mode', '*', '*.js')),
36+
],
2937
},
3038

3139
// Use [chunkhash] in order to invalidate browsers cache on new deployments.

0 commit comments

Comments
 (0)