Skip to content

Enable code syntax highlighting on "Snippet" page #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"eslint-plugin-react": "^7.4.0",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.5",
"glob": "^7.1.2",
"html-webpack-plugin": "^2.30.1",
"style-loader": "^0.19.0",
"stylus": "^0.54.5",
Expand Down
4 changes: 3 additions & 1 deletion src/components/Snippet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { connect } from 'react-redux';
import { Controlled as CodeMirror } from 'react-codemirror2';

import codemirror from 'codemirror';
import 'codemirror/lib/codemirror.css';

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

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

return (
[
Expand Down Expand Up @@ -67,7 +69,7 @@ class Snippet extends React.Component {
<div className="snippet-code">
<CodeMirror
value={`${snippet.get('content')}`}
options={{ lineNumbers: true, readOnly: 'nocursor' }}
options={{ lineNumbers: true, readOnly: 'nocursor', mode: modeInfo.mime }}
/>
<div className="snippet-code-bottom-bar">
<button className="snippet-button light">Raw</button>
Expand Down
10 changes: 9 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const process = require('process');

const webpack = require('webpack');
const merge = require('webpack-merge');
const glob = require('glob');

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

entry: {
app: path.resolve(__dirname, 'src', 'index.jsx'),
app: [
path.resolve(__dirname, 'src', 'index.jsx'),

// Bundle CodeMirror's syntaxes along with main application. There are
// around 120 syntaxes and we, of course, do not want to import all of
// them from within the application, hence this hack.
...glob.sync(path.resolve(__dirname, 'node_modules', 'codemirror', 'mode', '*', '*.js')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice workaround! 👍

],
},

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