Browse Source

Use browserify to build demo

pull/104/head
Vitaly Puzrin 10 years ago
parent
commit
858475af7a
  1. 13
      Makefile
  2. 10
      support/demo_template/index.jade
  3. 106
      support/demo_template/index.js

13
Makefile

@ -24,19 +24,8 @@ demo: lint
> ./demo/index.css > ./demo/index.css
rm -rf ./support/demo_template/sample.json rm -rf ./support/demo_template/sample.json
browserify ./ -s markdownit > ./demo/markdown-it.js browserify ./ -s markdownit > ./demo/markdown-it.js
#cp ./dist/markdown-it.js ./demo/ browserify ./support/demo_template/index.js > ./demo/index.js
cp ./support/demo_template/index.js ./demo/
cp ./support/demo_template/README.md ./demo/ cp ./support/demo_template/README.md ./demo/
mkdir ./demo/plugins
cp ./node_modules/markdown-it-abbr/dist/* ./demo/plugins
cp ./node_modules/markdown-it-container/dist/* ./demo/plugins
cp ./node_modules/markdown-it-deflist/dist/* ./demo/plugins
cp ./node_modules/markdown-it-emoji/dist/* ./demo/plugins
cp ./node_modules/markdown-it-footnote/dist/* ./demo/plugins
cp ./node_modules/markdown-it-ins/dist/* ./demo/plugins
cp ./node_modules/markdown-it-mark/dist/* ./demo/plugins
cp ./node_modules/markdown-it-sub/dist/* ./demo/plugins
cp ./node_modules/markdown-it-sup/dist/* ./demo/plugins
gh-demo: demo gh-demo: demo
touch ./demo/.nojekyll touch ./demo/.nojekyll

10
support/demo_template/index.jade

@ -20,16 +20,6 @@ html
script(src='markdown-it.js') script(src='markdown-it.js')
script(src='https://twemoji.maxcdn.com/twemoji.min.js') script(src='https://twemoji.maxcdn.com/twemoji.min.js')
script(src='plugins/markdown-it-abbr.js')
script(src='plugins/markdown-it-container.js')
script(src='plugins/markdown-it-deflist.js')
script(src='plugins/markdown-it-emoji.js')
script(src='plugins/markdown-it-footnote.js')
script(src='plugins/markdown-it-ins.js')
script(src='plugins/markdown-it-mark.js')
script(src='plugins/markdown-it-sub.js')
script(src='plugins/markdown-it-sup.js')
link(rel='stylesheet' href='index.css') link(rel='stylesheet' href='index.css')
script(src='index.js') script(src='index.js')

106
support/demo_template/index.js

@ -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();
}); });
})();

Loading…
Cancel
Save