Highlight: Use marked options.
This commit is contained in:
@@ -7,53 +7,16 @@
|
|||||||
* High Level API:
|
* High Level API:
|
||||||
*
|
*
|
||||||
* // Load basic dependencies and language support for present code tags
|
* // Load basic dependencies and language support for present code tags
|
||||||
*
|
* api.init()
|
||||||
* highlight.init()
|
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* // Re-highlights all code tags on demand
|
* Hooks To:
|
||||||
* // Useful when markdown has been parsed again
|
|
||||||
* // and new language occurred in the output for example
|
|
||||||
*
|
|
||||||
* highlight.update();
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* Hooks To:
|
|
||||||
*
|
*
|
||||||
* 'document:loaded' ~> highlight.init();
|
* 'document:loaded' ~> highlight.init();
|
||||||
* 'content:rendered' ~> highlight.update();
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
(function (global) {
|
(function (global) {
|
||||||
|
|
||||||
// Simplified debounce version (no args support)
|
|
||||||
function debounce(callback, milliseconds) {
|
|
||||||
var timeout;
|
|
||||||
|
|
||||||
return function () {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
|
|
||||||
timeout = setTimeout(function () {
|
|
||||||
callback();
|
|
||||||
}, milliseconds);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Highlight callback
|
|
||||||
function highlight() {
|
|
||||||
if (!('Prism' in global)) {
|
|
||||||
throw new Error(
|
|
||||||
'[Highlight] Prism not detected. Please run `highlight.init` to load all dependencies'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
global.Prism.highlightAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debounced highlight callback
|
|
||||||
var debouncedHighlight =
|
|
||||||
debounce(highlight, 300);
|
|
||||||
|
|
||||||
// Load minimal requirements
|
// Load minimal requirements
|
||||||
function loadInitialScriptsAndStyles() {
|
function loadInitialScriptsAndStyles() {
|
||||||
var link =
|
var link =
|
||||||
@@ -62,9 +25,6 @@
|
|||||||
var mainScript =
|
var mainScript =
|
||||||
document.createElement('script');
|
document.createElement('script');
|
||||||
|
|
||||||
var autoloaderScript =
|
|
||||||
document.createElement('script');
|
|
||||||
|
|
||||||
link.rel =
|
link.rel =
|
||||||
'stylesheet';
|
'stylesheet';
|
||||||
|
|
||||||
@@ -74,19 +34,42 @@
|
|||||||
mainScript.src =
|
mainScript.src =
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/prism.min.js';
|
'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/prism.min.js';
|
||||||
|
|
||||||
autoloaderScript.src =
|
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/plugins/autoloader/prism-autoloader.min.js';
|
|
||||||
|
|
||||||
mainScript.addEventListener('load', function () {
|
mainScript.addEventListener('load', function () {
|
||||||
// Load autoloader after Prism loads
|
// Escape early if back-end rendering is used
|
||||||
document.body.appendChild(autoloaderScript);
|
if (!('marked' in global)) {
|
||||||
});
|
// @TODO Autoload er
|
||||||
|
return Prism.highlightAll();
|
||||||
|
}
|
||||||
|
|
||||||
autoloaderScript.addEventListener('load', function () {
|
// Set up autoloading
|
||||||
global.Prism.plugins.autoloader.languages_path =
|
marked.setOptions({
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/components/';
|
highlight: function (code, language) {
|
||||||
|
if (!(language in Prism.languages)) {
|
||||||
|
var additionalLanguageScript =
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
global.highlight.update();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
document.head.appendChild(link);
|
document.head.appendChild(link);
|
||||||
@@ -94,22 +77,15 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// High Level API
|
// High Level API
|
||||||
global.highlight = global.highlight || {
|
var api = {
|
||||||
|
|
||||||
init: function () {
|
init: function () {
|
||||||
loadInitialScriptsAndStyles();
|
loadInitialScriptsAndStyles();
|
||||||
},
|
},
|
||||||
|
|
||||||
update: function () {
|
|
||||||
debouncedHighlight();
|
|
||||||
},
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Hooks
|
// Hooks
|
||||||
if ('events' in global) {
|
if ('events' in global) {
|
||||||
events.subscribe('document:loaded', global.highlight.init);
|
events.subscribe('document:loaded', api.init);
|
||||||
events.subscribe('content:rendered', global.highlight.update);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}(window));
|
}(window));
|
||||||
|
|||||||
@@ -21,13 +21,15 @@ function saveDraft() {
|
|||||||
function enableButton() {
|
function enableButton() {
|
||||||
var checkbox = $('tos');
|
var checkbox = $('tos');
|
||||||
var button = $('publish-button');
|
var button = $('publish-button');
|
||||||
button.disabled = !checkbox.checked;
|
button.disabled = !checkbox.checked;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onLoad() {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
// Hook point
|
// Hook point
|
||||||
events.publish('document:loaded');
|
events.publish('document:loaded');
|
||||||
|
});
|
||||||
|
|
||||||
|
function onLoad() {
|
||||||
$note = $("note");
|
$note = $("note");
|
||||||
$action = $("action").value;
|
$action = $("action").value;
|
||||||
$preview = $("draft");
|
$preview = $("draft");
|
||||||
|
|||||||
Reference in New Issue
Block a user