|
@ -1,12 +1,11 @@ |
|
|
(function () { |
|
|
'use strict'; |
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
/*eslint-env browser*/ |
|
|
/*eslint-env browser*/ |
|
|
/*global $, _*/ |
|
|
/*global $, _*/ |
|
|
|
|
|
|
|
|
var mdHtml, mdSrc, permalink, scrollMap; |
|
|
var mdHtml, mdSrc, permalink, scrollMap; |
|
|
|
|
|
|
|
|
var defaults = { |
|
|
var defaults = { |
|
|
html: false, // Enable HTML tags in source
|
|
|
html: false, // Enable HTML tags in source
|
|
|
xhtmlOut: false, // Use '/' to close single tags (<br />)
|
|
|
xhtmlOut: false, // Use '/' to close single tags (<br />)
|
|
|
breaks: false, // Convert '\n' in paragraphs into <br>
|
|
|
breaks: false, // Convert '\n' in paragraphs into <br>
|
|
@ -18,9 +17,9 @@ |
|
|
_highlight: true, |
|
|
_highlight: true, |
|
|
_strict: false, |
|
|
_strict: false, |
|
|
_view: 'html' // html / src / debug
|
|
|
_view: 'html' // html / src / debug
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
defaults.highlight = function (str, lang) { |
|
|
defaults.highlight = function (str, lang) { |
|
|
if (!defaults._highlight || !window.hljs) { return ''; } |
|
|
if (!defaults._highlight || !window.hljs) { return ''; } |
|
|
|
|
|
|
|
|
var hljs = window.hljs; |
|
|
var hljs = window.hljs; |
|
@ -35,49 +34,49 @@ |
|
|
} catch (__) {} |
|
|
} catch (__) {} |
|
|
|
|
|
|
|
|
return ''; |
|
|
return ''; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
function setOptionClass(name, val) { |
|
|
function setOptionClass(name, val) { |
|
|
if (val) { |
|
|
if (val) { |
|
|
$('body').addClass('opt_' + name); |
|
|
$('body').addClass('opt_' + name); |
|
|
} else { |
|
|
} else { |
|
|
$('body').removeClass('opt_' + name); |
|
|
$('body').removeClass('opt_' + name); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function setResultView(val) { |
|
|
function setResultView(val) { |
|
|
$('body').removeClass('result-as-html'); |
|
|
$('body').removeClass('result-as-html'); |
|
|
$('body').removeClass('result-as-src'); |
|
|
$('body').removeClass('result-as-src'); |
|
|
$('body').removeClass('result-as-debug'); |
|
|
$('body').removeClass('result-as-debug'); |
|
|
$('body').addClass('result-as-' + val); |
|
|
$('body').addClass('result-as-' + val); |
|
|
defaults._view = val; |
|
|
defaults._view = val; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function mdInit() { |
|
|
function mdInit() { |
|
|
if (defaults._strict) { |
|
|
if (defaults._strict) { |
|
|
mdHtml = window.markdownit('commonmark'); |
|
|
mdHtml = window.markdownit('commonmark'); |
|
|
mdSrc = window.markdownit('commonmark'); |
|
|
mdSrc = window.markdownit('commonmark'); |
|
|
} else { |
|
|
} else { |
|
|
mdHtml = window.markdownit(defaults) |
|
|
mdHtml = window.markdownit(defaults) |
|
|
.use(window.markdownitAbbr) |
|
|
.use(require('markdown-it-abbr')) |
|
|
.use(window.markdownitContainer, 'warning') |
|
|
.use(require('markdown-it-container'), 'warning') |
|
|
.use(window.markdownitDeflist) |
|
|
.use(require('markdown-it-deflist')) |
|
|
.use(window.markdownitEmoji) |
|
|
.use(require('markdown-it-emoji')) |
|
|
.use(window.markdownitFootnote) |
|
|
.use(require('markdown-it-footnote')) |
|
|
.use(window.markdownitIns) |
|
|
.use(require('markdown-it-ins')) |
|
|
.use(window.markdownitMark) |
|
|
.use(require('markdown-it-mark')) |
|
|
.use(window.markdownitSub) |
|
|
.use(require('markdown-it-sub')) |
|
|
.use(window.markdownitSup); |
|
|
.use(require('markdown-it-sup')); |
|
|
mdSrc = window.markdownit(defaults) |
|
|
mdSrc = window.markdownit(defaults) |
|
|
.use(window.markdownitAbbr) |
|
|
.use(require('markdown-it-abbr')) |
|
|
.use(window.markdownitContainer, 'warning') |
|
|
.use(require('markdown-it-container'), 'warning') |
|
|
.use(window.markdownitDeflist) |
|
|
.use(require('markdown-it-deflist')) |
|
|
.use(window.markdownitEmoji) |
|
|
.use(require('markdown-it-emoji')) |
|
|
.use(window.markdownitFootnote) |
|
|
.use(require('markdown-it-footnote')) |
|
|
.use(window.markdownitIns) |
|
|
.use(require('markdown-it-ins')) |
|
|
.use(window.markdownitMark) |
|
|
.use(require('markdown-it-mark')) |
|
|
.use(window.markdownitSub) |
|
|
.use(require('markdown-it-sub')) |
|
|
.use(window.markdownitSup); |
|
|
.use(require('markdown-it-sup')); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Beautify output of parser for html content
|
|
|
// Beautify output of parser for html content
|
|
@ -105,17 +104,17 @@ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
mdHtml.renderer.rules.paragraph_open = mdHtml.renderer.rules.heading_open = injectLineNumbers; |
|
|
mdHtml.renderer.rules.paragraph_open = mdHtml.renderer.rules.heading_open = injectLineNumbers; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function setHighlightedlContent(selector, content, lang) { |
|
|
function setHighlightedlContent(selector, content, lang) { |
|
|
if (window.hljs) { |
|
|
if (window.hljs) { |
|
|
$(selector).html(window.hljs.highlight(lang, content).value); |
|
|
$(selector).html(window.hljs.highlight(lang, content).value); |
|
|
} else { |
|
|
} else { |
|
|
$(selector).text(content); |
|
|
$(selector).text(content); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function updateResult() { |
|
|
function updateResult() { |
|
|
var source = $('.source').val(); |
|
|
var source = $('.source').val(); |
|
|
|
|
|
|
|
|
// Update only active view to avoid slowdowns
|
|
|
// Update only active view to avoid slowdowns
|
|
@ -150,12 +149,12 @@ |
|
|
} catch (__) { |
|
|
} catch (__) { |
|
|
permalink.href = ''; |
|
|
permalink.href = ''; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Build offsets for each line (lines can be wrapped)
|
|
|
// Build offsets for each line (lines can be wrapped)
|
|
|
// That's a bit dirty to process each line everytime, but ok for demo.
|
|
|
// That's a bit dirty to process each line everytime, but ok for demo.
|
|
|
// Optimizations are required only for big texts.
|
|
|
// Optimizations are required only for big texts.
|
|
|
function buildScrollMap() { |
|
|
function buildScrollMap() { |
|
|
var i, offset, nonEmptyList, pos, a, b, lineHeightMap, linesCount, |
|
|
var i, offset, nonEmptyList, pos, a, b, lineHeightMap, linesCount, |
|
|
acc, sourceLikeDiv, textarea = $('.source'), |
|
|
acc, sourceLikeDiv, textarea = $('.source'), |
|
|
_scrollMap; |
|
|
_scrollMap; |
|
@ -225,10 +224,10 @@ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return _scrollMap; |
|
|
return _scrollMap; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Synchronize scroll position from source to result
|
|
|
// Synchronize scroll position from source to result
|
|
|
var syncResultScroll = _.debounce(function () { |
|
|
var syncResultScroll = _.debounce(function () { |
|
|
var textarea = $('.source'), |
|
|
var textarea = $('.source'), |
|
|
lineHeight = parseFloat(textarea.css('line-height')), |
|
|
lineHeight = parseFloat(textarea.css('line-height')), |
|
|
lineNo, posTo; |
|
|
lineNo, posTo; |
|
@ -239,10 +238,10 @@ |
|
|
$('.result-html').stop(true).animate({ |
|
|
$('.result-html').stop(true).animate({ |
|
|
scrollTop: posTo |
|
|
scrollTop: posTo |
|
|
}, 100, 'linear'); |
|
|
}, 100, 'linear'); |
|
|
}, 50, { maxWait: 50 }); |
|
|
}, 50, { maxWait: 50 }); |
|
|
|
|
|
|
|
|
// Synchronize scroll position from result to source
|
|
|
// Synchronize scroll position from result to source
|
|
|
var syncSrcScroll = _.debounce(function () { |
|
|
var syncSrcScroll = _.debounce(function () { |
|
|
var resultHtml = $('.result-html'), |
|
|
var resultHtml = $('.result-html'), |
|
|
scrollTop = resultHtml.scrollTop(), |
|
|
scrollTop = resultHtml.scrollTop(), |
|
|
textarea = $('.source'), |
|
|
textarea = $('.source'), |
|
@ -273,12 +272,12 @@ |
|
|
textarea.stop(true).animate({ |
|
|
textarea.stop(true).animate({ |
|
|
scrollTop: lineHeight * line |
|
|
scrollTop: lineHeight * line |
|
|
}, 100, 'linear'); |
|
|
}, 100, 'linear'); |
|
|
}, 50, { maxWait: 50 }); |
|
|
}, 50, { maxWait: 50 }); |
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
// Init on page load
|
|
|
// Init on page load
|
|
|
//
|
|
|
//
|
|
|
$(function() { |
|
|
$(function() { |
|
|
// highlight snippet
|
|
|
// highlight snippet
|
|
|
if (window.hljs) { |
|
|
if (window.hljs) { |
|
|
$('pre.code-sample code').each(function(i, block) { |
|
|
$('pre.code-sample code').each(function(i, block) { |
|
@ -403,5 +402,4 @@ |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
updateResult(); |
|
|
updateResult(); |
|
|
}); |
|
|
}); |
|
|
})(); |
|
|
|
|
|