Skip to content

Commit ceab103

Browse files
author
Martin Krulis
committed
Code syntax highlighting added to markdown fields.
1 parent 5b4b8b6 commit ceab103

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"font-awesome-animation": "^0.2.1",
5757
"glob": "^7.1.2",
5858
"global": "^4.3.1",
59+
"highlight.js": "^9.16.2",
5960
"immutable": "^3.8.1",
6061
"isomorphic-fetch": "^2.2.1",
6162
"js-yaml": "^3.13.1",

src/components/widgets/Markdown/Markdown.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4-
const md = require('markdown-it')().use(require('@iktakahiro/markdown-it-katex'));
4+
const hljs = require('highlight.js');
5+
const md = require('markdown-it')({
6+
highlight: function(str, lang) {
7+
if (lang && hljs.getLanguage(lang)) {
8+
try {
9+
return hljs.highlight(lang, str).value;
10+
} catch (error) {
11+
/* just ignore the error */
12+
}
13+
}
14+
15+
return ''; // use external default escaping
16+
},
17+
}).use(require('@iktakahiro/markdown-it-katex'));
518

619
const Markdown = ({ source }) => (
720
<div

views/index.ejs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
1515
crossorigin="anonymous"
1616
/>
17-
<link
18-
rel="stylesheet"
19-
href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.css"
20-
/>
17+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.css" />
18+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css">
2119
<link
2220
rel="stylesheet"
2321
href="https://cdn.jsdelivr.net/github-markdown-css/2.2.1/github-markdown.css"

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5075,6 +5075,11 @@ [email protected]:
50755075
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
50765076
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==
50775077

5078+
highlight.js@^9.16.2:
5079+
version "9.16.2"
5080+
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.16.2.tgz#68368d039ffe1c6211bcc07e483daf95de3e403e"
5081+
integrity sha512-feMUrVLZvjy0oC7FVJQcSQRqbBq9kwqnYE4+Kj9ZjbHh3g+BisiPgF49NyQbVLNdrL/qqZr3Ca9yOKwgn2i/tw==
5082+
50785083
history@^4.9.0:
50795084
version "4.9.0"
50805085
resolved "https://registry.yarnpkg.com/history/-/history-4.9.0.tgz#84587c2068039ead8af769e9d6a6860a14fa1bca"

0 commit comments

Comments
 (0)