-
Notifications
You must be signed in to change notification settings - Fork 0
Syntax highlight
chinanf-boy edited this page Nov 19, 2018
·
1 revision
You can easily get your code highlighted using Glorious Demo with PrismJS.
import '@glorious/demo/gdemo.min.css';
import 'prismjs/themes/prism-tomorrow.css';
import GDemo from '@glorious/demo';
import Prism from 'prismjs';
const demo = new GDemo('#container');
const code = `
function greet(){
console.log("Hello World!");
}
greet();
`
const highlightedCode = Prism.highlight(
code,
Prism.languages.javascript,
'javascript'
);
demo
.openApp('editor', {minHeight: '350px', windowTitle: '~/gdemo.rb'})
.write(highlightedCode)
.end();
To highlight any language other than HTML, CSS or Javascript, make sure to include its respective Prism package:
import '@glorious/demo/gdemo.min.css';
import 'prismjs/themes/prism-tomorrow.css';
import GDemo from '@glorious/demo';
import Prism from 'prismjs';
import 'prismjs/components/prism-ruby'; // Ruby language specific package
const demo = new GDemo('#container');
const code = `
def greet
printf "Hello World!"
end
greet
`
const highlightedCode = Prism.highlight(
code,
Prism.languages.ruby,
'ruby'
);
demo
.openApp('editor', {minHeight: '350px', windowTitle: '~/gdemo.rb'})
.write(highlightedCode)
.end();
Prism offers some excellent themes for you. To use one of them, all you need to do is to include the Theme's CSS:
import 'prismjs/themes/prism-dark.css'; // Some other Prism theme
import '@glorious/demo/gdemo.min.css';
import GDemo from '@glorious/demo';
import Prism from 'prismjs';
const demo = new GDemo('#container');
const code = `
function greet(){
console.log("Hello World!");
}
greet();
`
const highlightedCode = Prism.highlight(
code,
Prism.languages.javascript,
'javascript'
);
demo
.openApp('editor', {minHeight: '350px', windowTitle: '~/gdemo.rb'})
.write(highlightedCode)
.end();
<!-- Prism -->
<script src="https://cdn.jsdelivr.net/npm/prismjs"></script>
<!-- Prism Language-Specific -->
<script src="https://cdn.jsdelivr.net/npm/prismjs/components/prism-ruby.js"></script>
<!-- Prism Theme-Specific -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs/themes/prism-tomorrow.css">