1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-08-06 06:07:33 +02:00

Highlight sample code

This commit is contained in:
Phuoc Nguyen
2019-11-15 16:31:03 +07:00
parent 3668a6ee3a
commit 73587005ff
4 changed files with 30 additions and 0 deletions

13
client/SampleCode.jsx Normal file
View File

@@ -0,0 +1,13 @@
import React from 'react';
import highlight from './helpers/highlight';
const SampleCode = ({ code, lang }) => {
return code === ''
? <></>
: (
<pre className="lh-copy mh0 mv3" dangerouslySetInnerHTML={{ __html: highlight(code, lang) }} />
);
};
export default SampleCode;

11
client/helpers/highlight.js Executable file
View File

@@ -0,0 +1,11 @@
import hljs from 'highlight.js';
const highlight = (input, language) => {
const lang = language || 'html';
const { value } = hljs.highlight(lang, input);
const highlighted = value.replace('&amp;', '&').trim();
return `<code class="hljs ${lang}">${highlighted}</code>`;
};
export default highlight;

5
package-lock.json generated
View File

@@ -4137,6 +4137,11 @@
"util-deprecate": "^1.0.2"
}
},
"highlight.js": {
"version": "9.16.2",
"resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-9.16.2.tgz",
"integrity": "sha512-feMUrVLZvjy0oC7FVJQcSQRqbBq9kwqnYE4+Kj9ZjbHh3g+BisiPgF49NyQbVLNdrL/qqZr3Ca9yOKwgn2i/tw=="
},
"history": {
"version": "4.10.1",
"resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz",

View File

@@ -6,6 +6,7 @@
"postbuild": "react-snap"
},
"dependencies": {
"highlight.js": "^9.16.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",