Highlight: Use highlight.js.
This commit is contained in:
@@ -6,7 +6,6 @@
|
|||||||
*
|
*
|
||||||
* High Level API:
|
* High Level API:
|
||||||
*
|
*
|
||||||
* // Load basic dependencies and language support for present code tags
|
|
||||||
* api.init()
|
* api.init()
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
@@ -17,7 +16,6 @@
|
|||||||
*/
|
*/
|
||||||
(function (global) {
|
(function (global) {
|
||||||
|
|
||||||
// Load minimal requirements
|
|
||||||
function loadInitialScriptsAndStyles() {
|
function loadInitialScriptsAndStyles() {
|
||||||
var link =
|
var link =
|
||||||
document.createElement('link');
|
document.createElement('link');
|
||||||
@@ -29,53 +27,31 @@
|
|||||||
'stylesheet';
|
'stylesheet';
|
||||||
|
|
||||||
link.href =
|
link.href =
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/themes/prism-tomorrow.min.css';
|
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.5.0/styles/railscasts.min.css';
|
||||||
|
|
||||||
mainScript.src =
|
mainScript.src =
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/prism.min.js';
|
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.5.0/highlight.min.js';
|
||||||
|
|
||||||
|
// On main script load, configure marked
|
||||||
mainScript.addEventListener('load', function () {
|
mainScript.addEventListener('load', function () {
|
||||||
// Escape early if back-end rendering is used
|
|
||||||
if (!('marked' in global)) {
|
|
||||||
// @TODO Autoload er
|
|
||||||
return Prism.highlightAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up autoloading
|
|
||||||
marked.setOptions({
|
marked.setOptions({
|
||||||
highlight: function (code, language) {
|
langPrefix: 'hljs lang-',
|
||||||
if (!(language in Prism.languages)) {
|
highlight: function (code) {
|
||||||
var additionalLanguageScript =
|
return hljs.highlightAuto(code).value;
|
||||||
document.createElement('script');
|
|
||||||
|
|
||||||
additionalLanguageScript.src =
|
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/components/prism-' + language + '.min.js';
|
|
||||||
|
|
||||||
// On success, highlight code for given language
|
|
||||||
additionalLanguageScript.addEventListener('load', function () {
|
|
||||||
[].forEach.call(document.querySelectorAll('.lang-' + language), function (element) {
|
|
||||||
Prism.highlightElement(element);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Remove if language not available
|
|
||||||
additionalLanguageScript.addEventListener('error', function () {
|
|
||||||
document.body.removeChild(additionalLanguageScript);
|
|
||||||
});
|
|
||||||
|
|
||||||
document.body.appendChild(additionalLanguageScript);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Prism.highlight(code, Prism.languages[language] || Prism.languages.markup);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
document.head.appendChild(link);
|
// Extend marked.js on edit page only
|
||||||
|
if ('marked' in global) {
|
||||||
document.body.appendChild(mainScript);
|
document.body.appendChild(mainScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Preview page requires scripts only
|
||||||
|
document.head.appendChild(link);
|
||||||
|
}
|
||||||
|
|
||||||
// High Level API
|
// High Level API
|
||||||
var api = {
|
var api = {
|
||||||
init: function () {
|
init: function () {
|
||||||
|
|||||||
+13
-12
@@ -185,25 +185,26 @@ blockquote {
|
|||||||
margin-bottom: 7em;
|
margin-bottom: 7em;
|
||||||
}
|
}
|
||||||
|
|
||||||
code, pre {
|
/* Code tags and pre tags alone to be monospaced */
|
||||||
background: #efefef;
|
pre, code {
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Code tags alone to be slightly bigger than defaults */
|
/* Non-syntax highlighted code and pre tags to get default background */
|
||||||
code {
|
pre, code:not([class*="lang-"]) {
|
||||||
|
background: #efefef;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Non-syntax highlighted code tag to be slightly bigger so it outstands in text */
|
||||||
|
code:not([class*="lang-"]) {
|
||||||
font-size: 1.05em;
|
font-size: 1.05em;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prevent over-sized code in when syntax-highlighted */
|
/* Syntax highlighted code tag to get some padding, nice rounded corners and keep font-size */
|
||||||
pre code {
|
code[class*="lang-"] {
|
||||||
font-size: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
pre {
|
|
||||||
border: 1px solid #aaa;
|
|
||||||
padding: 0.5em;
|
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
|
font-size: 1em;
|
||||||
|
padding: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
*:focus {
|
*:focus {
|
||||||
|
|||||||
Reference in New Issue
Block a user