Browse Source

Highlight: Use highlight.js.

master
Maciej Smolinski 10 years ago
parent
commit
dc7ecb7621
  1. 50
      resources/public/js/highlight.js
  2. 27
      resources/public/style.css

50
resources/public/js/highlight.js

@ -6,18 +6,16 @@
* *
* High Level API: * High Level API:
* *
* // Load basic dependencies and language support for present code tags
* api.init() * api.init()
* *
* *
* Hooks To: * Hooks To:
* *
* 'document:loaded' ~> highlight.init(); * 'document:loaded' ~> highlight.init();
* *
*/ */
(function (global) { (function (global) {
// Load minimal requirements
function loadInitialScriptsAndStyles() { function loadInitialScriptsAndStyles() {
var link = var link =
document.createElement('link'); document.createElement('link');
@ -29,51 +27,29 @@
'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);
}, },
}); });
}); });
// Extend marked.js on edit page only
if ('marked' in global) {
document.body.appendChild(mainScript);
}
// Preview page requires scripts only
document.head.appendChild(link); document.head.appendChild(link);
document.body.appendChild(mainScript);
} }
// High Level API // High Level API

27
resources/public/style.css

@ -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-"]) {
font-size: 1.05em; background: #efefef;
} }
/* Prevent over-sized code in when syntax-highlighted */ /* Non-syntax highlighted code tag to be slightly bigger so it outstands in text */
pre code { code:not([class*="lang-"]) {
font-size: 1em; font-size: 1.05em;
} }
pre { /* Syntax highlighted code tag to get some padding, nice rounded corners and keep font-size */
border: 1px solid #aaa; code[class*="lang-"] {
padding: 0.5em;
border-radius: 3px; border-radius: 3px;
font-size: 1em;
padding: 1em;
} }
*:focus { *:focus {
@ -231,7 +232,7 @@ td {
padding: 0.5em; padding: 0.5em;
} }
/* MEDIA QUERIES */ /* MEDIA QUERIES */
@media screen and (min-width: 1024px) { @media screen and (min-width: 1024px) {
article, .central-element { article, .central-element {
@ -317,4 +318,4 @@ fieldset {
#plain-password { #plain-password {
width: 10em; width: 10em;
} }

Loading…
Cancel
Save