2015-07-01 16:10:20 +00:00
|
|
|
//auto update last change
|
2016-04-20 10:03:55 +00:00
|
|
|
var createtime = null;
|
2015-07-01 16:10:20 +00:00
|
|
|
var lastchangetime = null;
|
2016-01-12 14:01:42 +00:00
|
|
|
var lastchangeui = {
|
2016-04-20 10:03:55 +00:00
|
|
|
status: $(".ui-status-lastchange"),
|
2016-01-12 14:01:42 +00:00
|
|
|
time: $(".ui-lastchange"),
|
|
|
|
user: $(".ui-lastchangeuser"),
|
|
|
|
nouser: $(".ui-no-lastchangeuser")
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
|
|
|
function updateLastChange() {
|
2016-04-20 10:03:55 +00:00
|
|
|
if (!lastchangeui) return;
|
|
|
|
if (createtime) {
|
|
|
|
if (createtime && !lastchangetime) {
|
|
|
|
lastchangeui.status.text('created');
|
|
|
|
} else {
|
|
|
|
lastchangeui.status.text('changed');
|
|
|
|
}
|
|
|
|
var time = lastchangetime || createtime;
|
|
|
|
lastchangeui.time.html(moment(time).fromNow());
|
|
|
|
lastchangeui.time.attr('title', moment(time).format('llll'));
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setInterval(updateLastChange, 60000);
|
|
|
|
|
2016-03-15 02:45:02 +00:00
|
|
|
var lastchangeuser = null;
|
|
|
|
var lastchangeuserprofile = null;
|
|
|
|
function updateLastChangeUser() {
|
|
|
|
if (lastchangeui) {
|
|
|
|
if (lastchangeuser && lastchangeuserprofile) {
|
|
|
|
var icon = lastchangeui.user.children('i');
|
|
|
|
icon.attr('title', lastchangeuserprofile.name).tooltip('fixTitle');
|
|
|
|
icon.attr('style', 'background-image:url(' + lastchangeuserprofile.photo + ')');
|
|
|
|
lastchangeui.user.show();
|
|
|
|
lastchangeui.nouser.hide();
|
|
|
|
} else {
|
|
|
|
lastchangeui.user.hide();
|
|
|
|
lastchangeui.nouser.show();
|
|
|
|
}
|
2016-01-12 14:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
//get title
|
|
|
|
function getTitle(view) {
|
|
|
|
var h1s = view.find("h1");
|
|
|
|
var title = "";
|
2015-07-01 16:10:20 +00:00
|
|
|
if (h1s.length > 0) {
|
2015-05-04 07:53:29 +00:00
|
|
|
title = h1s.first().text();
|
|
|
|
} else {
|
|
|
|
title = null;
|
|
|
|
}
|
|
|
|
return title;
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
//render title
|
|
|
|
function renderTitle(view) {
|
|
|
|
var title = getTitle(view);
|
2015-07-01 16:10:20 +00:00
|
|
|
if (title) {
|
2015-05-04 07:53:29 +00:00
|
|
|
title += ' - HackMD';
|
|
|
|
} else {
|
|
|
|
title = 'HackMD - Collaborative notes';
|
|
|
|
}
|
|
|
|
return title;
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
//render filename
|
|
|
|
function renderFilename(view) {
|
|
|
|
var filename = getTitle(view);
|
2015-07-01 16:10:20 +00:00
|
|
|
if (!filename) {
|
2015-05-04 07:53:29 +00:00
|
|
|
filename = 'Untitled';
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2015-07-01 16:10:20 +00:00
|
|
|
function slugifyWithUTF8(text) {
|
|
|
|
var newText = S(text.toLowerCase()).trim().stripTags().dasherize().s;
|
|
|
|
newText = newText.replace(/([\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\[\\\]\^\`\{\|\}\~])/g, '');
|
|
|
|
return newText;
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:01:42 +00:00
|
|
|
//parse meta
|
2016-02-16 01:13:17 +00:00
|
|
|
function parseMeta(md, edit, view, toc, tocAffix) {
|
2016-01-12 14:01:42 +00:00
|
|
|
var robots = null;
|
|
|
|
var lang = null;
|
|
|
|
var dir = null;
|
|
|
|
var breaks = true;
|
2016-04-20 10:09:36 +00:00
|
|
|
var spellcheck = false;
|
2016-01-12 14:01:42 +00:00
|
|
|
if (md && md.meta) {
|
|
|
|
var meta = md.meta;
|
|
|
|
robots = meta.robots;
|
|
|
|
lang = meta.lang;
|
|
|
|
dir = meta.dir;
|
|
|
|
breaks = meta.breaks;
|
2016-04-20 10:09:36 +00:00
|
|
|
spellcheck = meta.spellcheck;
|
2016-01-12 14:01:42 +00:00
|
|
|
}
|
|
|
|
//robots meta
|
|
|
|
var robotsMeta = $('meta[name=robots]');
|
|
|
|
if (robots) {
|
|
|
|
if (robotsMeta.length > 0)
|
|
|
|
robotsMeta.attr('content', robots);
|
|
|
|
else
|
|
|
|
$('head').prepend('<meta name="robots" content="' + robots + '">')
|
|
|
|
}
|
|
|
|
else
|
|
|
|
robotsMeta.remove();
|
|
|
|
//text language
|
|
|
|
if (lang) {
|
|
|
|
view.attr('lang', lang);
|
|
|
|
toc.attr('lang', lang);
|
|
|
|
tocAffix.attr('lang', lang);
|
2016-02-16 01:13:17 +00:00
|
|
|
if (edit)
|
|
|
|
edit.attr('lang', lang);
|
2016-01-12 14:01:42 +00:00
|
|
|
} else {
|
|
|
|
view.removeAttr('lang');
|
|
|
|
toc.removeAttr('lang');
|
|
|
|
tocAffix.removeAttr('lang');
|
2016-02-16 01:13:17 +00:00
|
|
|
if (edit)
|
|
|
|
edit.removeAttr('lang', lang);
|
2016-01-12 14:01:42 +00:00
|
|
|
}
|
|
|
|
//text direction
|
|
|
|
if (dir) {
|
|
|
|
view.attr('dir', dir);
|
|
|
|
toc.attr('dir', dir);
|
|
|
|
tocAffix.attr('dir', dir);
|
|
|
|
} else {
|
|
|
|
view.removeAttr('dir');
|
|
|
|
toc.removeAttr('dir');
|
|
|
|
tocAffix.removeAttr('dir');
|
|
|
|
}
|
|
|
|
//breaks
|
|
|
|
if (typeof breaks === 'boolean' && !breaks) {
|
|
|
|
md.options.breaks = false;
|
|
|
|
} else {
|
|
|
|
md.options.breaks = true;
|
|
|
|
}
|
2016-04-20 10:09:36 +00:00
|
|
|
// spell check
|
|
|
|
if (typeof editor === 'object') {
|
|
|
|
var mode = null;
|
|
|
|
if (typeof spellcheck === 'boolean' && spellcheck) {
|
|
|
|
mode = 'spell-checker';
|
|
|
|
} else {
|
|
|
|
mode = 'gfm';
|
|
|
|
}
|
|
|
|
if (mode && mode !== editor.getOption('mode')) {
|
|
|
|
editor.setOption('mode', mode);
|
|
|
|
}
|
|
|
|
}
|
2016-01-12 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
var viewAjaxCallback = null;
|
|
|
|
|
2015-09-25 09:41:15 +00:00
|
|
|
//regex for extra tags
|
2015-07-01 16:10:20 +00:00
|
|
|
var spaceregex = /\s*/;
|
|
|
|
var notinhtmltagregex = /(?![^<]*>|[^<>]*<\/)/;
|
|
|
|
var coloregex = /\[color=([#|\(|\)|\s|\,|\w]*?)\]/;
|
|
|
|
coloregex = new RegExp(coloregex.source + notinhtmltagregex.source, "g");
|
|
|
|
var nameregex = /\[name=(.*?)\]/;
|
|
|
|
var timeregex = /\[time=([:|,|+|-|\(|\)|\s|\w]*?)\]/;
|
|
|
|
var nameandtimeregex = new RegExp(nameregex.source + spaceregex.source + timeregex.source + notinhtmltagregex.source, "g");
|
|
|
|
nameregex = new RegExp(nameregex.source + notinhtmltagregex.source, "g");
|
|
|
|
timeregex = new RegExp(timeregex.source + notinhtmltagregex.source, "g");
|
|
|
|
|
2015-09-25 09:41:15 +00:00
|
|
|
function replaceExtraTags(html) {
|
|
|
|
html = html.replace(coloregex, '<span class="color" data-color="$1"></span>');
|
|
|
|
html = html.replace(nameandtimeregex, '<small><i class="fa fa-user"></i> $1 <i class="fa fa-clock-o"></i> $2</small>');
|
|
|
|
html = html.replace(nameregex, '<small><i class="fa fa-user"></i> $1</small>');
|
|
|
|
html = html.replace(timeregex, '<small><i class="fa fa-clock-o"></i> $1</small>');
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
//dynamic event or object binding here
|
|
|
|
function finishView(view) {
|
2015-09-25 09:41:15 +00:00
|
|
|
//todo list
|
|
|
|
var lis = view.find('li.raw').removeClass("raw").sortByDepth().toArray();
|
|
|
|
for (var i = 0; i < lis.length; i++) {
|
|
|
|
var li = lis[i];
|
|
|
|
var html = $(li).clone()[0].innerHTML;
|
|
|
|
var p = $(li).children('p');
|
|
|
|
if (p.length == 1) {
|
|
|
|
html = p.html();
|
|
|
|
li = p[0];
|
|
|
|
}
|
|
|
|
html = replaceExtraTags(html);
|
|
|
|
li.innerHTML = html;
|
|
|
|
var disabled = 'disabled';
|
|
|
|
if(typeof editor !== 'undefined' && havePermission())
|
|
|
|
disabled = '';
|
|
|
|
if (/^\s*\[[x ]\]\s*/.test(html)) {
|
|
|
|
li.innerHTML = html.replace(/^\s*\[ \]\s*/, '<input type="checkbox" class="task-list-item-checkbox "' + disabled + '><label></label>')
|
|
|
|
.replace(/^\s*\[x\]\s*/, '<input type="checkbox" class="task-list-item-checkbox" checked ' + disabled + '><label></label>');
|
|
|
|
lis[i].setAttribute('class', 'task-list-item');
|
|
|
|
}
|
|
|
|
if (typeof editor !== 'undefined' && havePermission())
|
|
|
|
$(li).find('input').change(toggleTodoEvent);
|
|
|
|
//color tag in list will convert it to tag icon with color
|
|
|
|
var tag_color = $(li).closest('ul').find(".color");
|
|
|
|
tag_color.each(function (key, value) {
|
|
|
|
$(value).addClass('fa fa-tag').css('color', $(value).attr('data-color'));
|
|
|
|
});
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
//youtube
|
2015-07-01 16:10:20 +00:00
|
|
|
view.find(".youtube.raw").removeClass("raw")
|
|
|
|
.click(function () {
|
|
|
|
imgPlayiframe(this, '//www.youtube.com/embed/');
|
|
|
|
});
|
2015-05-04 07:53:29 +00:00
|
|
|
//vimeo
|
2015-07-01 16:10:20 +00:00
|
|
|
view.find(".vimeo.raw").removeClass("raw")
|
2015-05-04 07:53:29 +00:00
|
|
|
.click(function () {
|
|
|
|
imgPlayiframe(this, '//player.vimeo.com/video/');
|
|
|
|
})
|
|
|
|
.each(function (key, value) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2016-02-11 09:45:13 +00:00
|
|
|
url: '//vimeo.com/api/v2/video/' + $(value).attr('data-videoid') + '.json',
|
2015-05-04 07:53:29 +00:00
|
|
|
jsonp: 'callback',
|
|
|
|
dataType: 'jsonp',
|
|
|
|
success: function (data) {
|
|
|
|
var thumbnail_src = data[0].thumbnail_large;
|
2015-09-25 10:55:56 +00:00
|
|
|
var image = '<img src="' + thumbnail_src + '" />';
|
|
|
|
$(value).prepend(image);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
//gist
|
2015-07-01 16:10:20 +00:00
|
|
|
view.find("code[data-gist-id]").each(function (key, value) {
|
|
|
|
if ($(value).children().length == 0)
|
2015-05-15 04:58:13 +00:00
|
|
|
$(value).gist(viewAjaxCallback);
|
2015-05-04 07:53:29 +00:00
|
|
|
});
|
|
|
|
//emojify
|
2016-01-31 21:29:20 +00:00
|
|
|
try {
|
|
|
|
emojify.run(view[0]);
|
|
|
|
} catch (err) {
|
2016-03-04 15:19:49 +00:00
|
|
|
console.warn(err);
|
2016-01-31 21:29:20 +00:00
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
//mathjax
|
2015-07-01 16:10:20 +00:00
|
|
|
var mathjaxdivs = view.find('.mathjax.raw').removeClass("raw").toArray();
|
2015-05-04 07:53:29 +00:00
|
|
|
try {
|
2016-02-24 02:57:33 +00:00
|
|
|
if (mathjaxdivs.length > 1) {
|
|
|
|
MathJax.Hub.Queue(["Typeset", MathJax.Hub, mathjaxdivs]);
|
|
|
|
MathJax.Hub.Queue(viewAjaxCallback);
|
|
|
|
} else if (mathjaxdivs.length > 0) {
|
|
|
|
MathJax.Hub.Queue(["Typeset", MathJax.Hub, mathjaxdivs[0]]);
|
|
|
|
MathJax.Hub.Queue(viewAjaxCallback);
|
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
} catch (err) {}
|
2015-05-04 07:53:29 +00:00
|
|
|
//sequence diagram
|
2015-12-24 07:01:02 +00:00
|
|
|
var sequences = view.find(".sequence-diagram.raw").removeClass("raw");
|
|
|
|
sequences.each(function (key, value) {
|
|
|
|
try {
|
2016-03-04 15:26:27 +00:00
|
|
|
var $value = $(value);
|
|
|
|
var $ele = $(value).parent().parent();
|
|
|
|
|
|
|
|
var sequence = $value;
|
2015-12-24 07:01:02 +00:00
|
|
|
sequence.sequenceDiagram({
|
|
|
|
theme: 'simple'
|
|
|
|
});
|
2016-03-04 15:26:27 +00:00
|
|
|
|
|
|
|
$ele.addClass('sequence-diagram');
|
|
|
|
$value.children().unwrap().unwrap();
|
2015-12-24 07:01:02 +00:00
|
|
|
} catch (err) {
|
2016-03-04 15:19:49 +00:00
|
|
|
console.warn(err);
|
2015-12-24 07:01:02 +00:00
|
|
|
}
|
|
|
|
});
|
2015-05-04 07:53:29 +00:00
|
|
|
//flowchart
|
2015-07-01 16:10:20 +00:00
|
|
|
var flow = view.find(".flow-chart.raw").removeClass("raw");
|
2015-05-04 07:53:29 +00:00
|
|
|
flow.each(function (key, value) {
|
|
|
|
try {
|
2016-03-04 15:26:27 +00:00
|
|
|
var $value = $(value);
|
|
|
|
var $ele = $(value).parent().parent();
|
|
|
|
|
|
|
|
var chart = flowchart.parse($value.text());
|
|
|
|
$value.html('');
|
2015-05-04 07:53:29 +00:00
|
|
|
chart.drawSVG(value, {
|
|
|
|
'line-width': 2,
|
|
|
|
'fill': 'none',
|
|
|
|
'font-size': '16px',
|
|
|
|
'font-family': "'Andale Mono', monospace"
|
|
|
|
});
|
2016-03-04 15:26:27 +00:00
|
|
|
|
|
|
|
$ele.addClass('flow-chart');
|
|
|
|
$value.children().unwrap().unwrap();
|
2015-07-01 16:10:20 +00:00
|
|
|
} catch (err) {
|
2016-03-04 15:19:49 +00:00
|
|
|
console.warn(err);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
});
|
2016-01-12 13:56:29 +00:00
|
|
|
//graphviz
|
|
|
|
var graphvizs = view.find(".graphviz.raw").removeClass("raw");
|
|
|
|
graphvizs.each(function (key, value) {
|
|
|
|
try {
|
2016-03-04 15:26:27 +00:00
|
|
|
var $value = $(value);
|
|
|
|
var $ele = $(value).parent().parent();
|
|
|
|
|
|
|
|
var graphviz = Viz($value.text());
|
|
|
|
$value.html(graphviz);
|
|
|
|
|
|
|
|
$ele.addClass('graphviz');
|
|
|
|
$value.children().unwrap().unwrap();
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//mermaid
|
|
|
|
var mermaids = view.find(".mermaid.raw").removeClass("raw");
|
|
|
|
mermaids.each(function (key, value) {
|
|
|
|
try {
|
|
|
|
var $value = $(value);
|
|
|
|
var $ele = $(value).parent().parent();
|
|
|
|
|
|
|
|
var mermaidError = null;
|
|
|
|
mermaid.parseError = function (err, hash) {
|
|
|
|
mermaidError = err;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mermaidAPI.parse($value.text())) {
|
|
|
|
$ele.addClass('mermaid');
|
|
|
|
$ele.html($value.text());
|
|
|
|
mermaid.init(undefined, $ele);
|
|
|
|
} else {
|
|
|
|
console.warn(mermaidError);
|
|
|
|
}
|
2016-01-12 13:56:29 +00:00
|
|
|
} catch (err) {
|
2016-03-04 15:19:49 +00:00
|
|
|
console.warn(err);
|
2016-01-12 13:56:29 +00:00
|
|
|
}
|
|
|
|
});
|
2015-07-01 16:10:20 +00:00
|
|
|
//image href new window(emoji not included)
|
2015-07-02 12:29:31 +00:00
|
|
|
var images = view.find("img.raw[src]").removeClass("raw");
|
2015-07-01 16:10:20 +00:00
|
|
|
images.each(function (key, value) {
|
2016-03-04 15:09:50 +00:00
|
|
|
// if it's already wrapped by link, then ignore
|
|
|
|
var $value = $(value);
|
|
|
|
if ($value.parent()[0].nodeName === 'A') return;
|
2015-07-01 16:10:20 +00:00
|
|
|
var src = $(value).attr('src');
|
|
|
|
var a = $('<a>');
|
|
|
|
if (src) {
|
|
|
|
a.attr('href', src);
|
|
|
|
a.attr('target', "_blank");
|
|
|
|
}
|
2016-03-04 15:09:50 +00:00
|
|
|
var clone = $value.clone();
|
2015-07-02 12:29:31 +00:00
|
|
|
clone[0].onload = function (e) {
|
|
|
|
if(viewAjaxCallback)
|
|
|
|
viewAjaxCallback();
|
|
|
|
};
|
|
|
|
a.html(clone);
|
2016-03-04 15:09:50 +00:00
|
|
|
$value.replaceWith(a);
|
2015-07-01 16:10:20 +00:00
|
|
|
});
|
|
|
|
//blockquote
|
|
|
|
var blockquote = view.find("blockquote.raw").removeClass("raw");
|
|
|
|
var blockquote_p = blockquote.find("p");
|
|
|
|
blockquote_p.each(function (key, value) {
|
|
|
|
var html = $(value).html();
|
2015-09-25 09:41:15 +00:00
|
|
|
html = replaceExtraTags(html);
|
2015-07-01 16:10:20 +00:00
|
|
|
$(value).html(html);
|
|
|
|
});
|
2015-09-25 09:41:15 +00:00
|
|
|
//color tag in blockquote will change its left border color
|
2015-07-01 16:10:20 +00:00
|
|
|
var blockquote_color = blockquote.find(".color");
|
|
|
|
blockquote_color.each(function (key, value) {
|
|
|
|
$(value).closest("blockquote").css('border-left-color', $(value).attr('data-color'));
|
|
|
|
});
|
2015-12-22 23:51:07 +00:00
|
|
|
//slideshare
|
|
|
|
view.find(".slideshare.raw").removeClass("raw")
|
|
|
|
.each(function (key, value) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2016-02-11 09:45:13 +00:00
|
|
|
url: '//www.slideshare.net/api/oembed/2?url=http://www.slideshare.net/' + $(value).attr('data-slideshareid') + '&format=json',
|
2015-12-22 23:51:07 +00:00
|
|
|
jsonp: 'callback',
|
|
|
|
dataType: 'jsonp',
|
|
|
|
success: function (data) {
|
2016-01-17 15:55:12 +00:00
|
|
|
var $html = $(data.html);
|
|
|
|
var iframe = $html.closest('iframe');
|
|
|
|
var caption = $html.closest('div');
|
|
|
|
var inner = $('<div class="inner"></div>').append(iframe);
|
|
|
|
var height = iframe.attr('height');
|
|
|
|
var width = iframe.attr('width');
|
|
|
|
var ratio = (height / width) * 100;
|
|
|
|
inner.css('padding-bottom', ratio + '%');
|
|
|
|
$(value).html(inner).append(caption);
|
2015-12-22 23:51:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
//speakerdeck
|
|
|
|
view.find(".speakerdeck.raw").removeClass("raw")
|
|
|
|
.each(function (key, value) {
|
2016-02-11 09:45:13 +00:00
|
|
|
var url = 'https://speakerdeck.com/oembed.json?url=https%3A%2F%2Fspeakerdeck.com%2F' + encodeURIComponent($(value).attr('data-speakerdeckid'));
|
2015-12-22 23:51:07 +00:00
|
|
|
//use yql because speakerdeck not support jsonp
|
|
|
|
$.ajax({
|
|
|
|
url: 'https://query.yahooapis.com/v1/public/yql',
|
|
|
|
data: {
|
|
|
|
q: "select * from json where url ='" + url + "'",
|
|
|
|
format: "json"
|
|
|
|
},
|
|
|
|
dataType: "jsonp",
|
|
|
|
success: function (data) {
|
2016-03-16 04:46:29 +00:00
|
|
|
if (!data.query || !data.query.results) return;
|
2015-12-22 23:51:07 +00:00
|
|
|
var json = data.query.results.json;
|
|
|
|
var html = json.html;
|
|
|
|
var ratio = json.height / json.width;
|
|
|
|
$(value).html(html);
|
|
|
|
var iframe = $(value).children('iframe');
|
|
|
|
var src = iframe.attr('src');
|
|
|
|
if (src.indexOf('//') == 0)
|
|
|
|
iframe.attr('src', 'https:' + src);
|
2016-01-17 15:55:12 +00:00
|
|
|
var inner = $('<div class="inner"></div>').append(iframe);
|
|
|
|
var height = iframe.attr('height');
|
|
|
|
var width = iframe.attr('width');
|
|
|
|
var ratio = (height / width) * 100;
|
|
|
|
inner.css('padding-bottom', ratio + '%');
|
|
|
|
$(value).html(inner);
|
2015-12-22 23:51:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-05-04 07:53:29 +00:00
|
|
|
//render title
|
|
|
|
document.title = renderTitle(view);
|
|
|
|
}
|
2015-06-01 10:04:25 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
//only static transform should be here
|
|
|
|
function postProcess(code) {
|
|
|
|
var result = $('<div>' + code + '</div>');
|
2015-09-25 10:18:21 +00:00
|
|
|
//link should open in new window or tab
|
2016-01-31 21:28:52 +00:00
|
|
|
result.find('a:not([href^="#"]):not([target])').attr('target', '_blank');
|
2015-10-22 09:15:44 +00:00
|
|
|
//update continue line numbers
|
|
|
|
var linenumberdivs = result.find('.gutter.linenumber').toArray();
|
|
|
|
for (var i = 0; i < linenumberdivs.length; i++) {
|
|
|
|
if ($(linenumberdivs[i]).hasClass('continue')) {
|
|
|
|
var startnumber = linenumberdivs[i - 1] ? parseInt($(linenumberdivs[i - 1]).find('> span').last().attr('data-linenumber')) : 0;
|
|
|
|
$(linenumberdivs[i]).find('> span').each(function(key, value) {
|
|
|
|
$(value).attr('data-linenumber', startnumber + key + 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 10:18:21 +00:00
|
|
|
return result;
|
|
|
|
}
|
2015-09-25 11:09:43 +00:00
|
|
|
|
|
|
|
//extract markdown body to html and compile to template
|
|
|
|
function exportToHTML(view) {
|
|
|
|
var title = renderTitle(ui.area.markdown);
|
|
|
|
var filename = renderFilename(ui.area.markdown) + '.html';
|
|
|
|
var src = view.clone();
|
|
|
|
var eles = src.find('*');
|
|
|
|
//remove syncscroll parts
|
|
|
|
eles.removeClass('part');
|
|
|
|
src.find('*[class=""]').removeAttr('class');
|
|
|
|
eles.removeAttr('data-startline data-endline');
|
|
|
|
eles.find("a[href^='#'][smoothhashscroll]").removeAttr('smoothhashscroll');
|
|
|
|
//remove gist content
|
|
|
|
src.find("code[data-gist-id]").children().remove();
|
|
|
|
//disable todo list
|
|
|
|
src.find("input.task-list-item-checkbox").attr('disabled', '');
|
|
|
|
//replace emoji image path
|
|
|
|
src.find("img.emoji").each(function (key, value) {
|
|
|
|
var name = $(value).attr('alt');
|
|
|
|
name = name.substr(1);
|
|
|
|
name = name.slice(0, name.length - 1);
|
|
|
|
$(value).attr('src', 'https://www.tortue.me/emoji/' + name + '.png');
|
|
|
|
});
|
|
|
|
//replace video to iframe
|
2016-02-11 09:45:13 +00:00
|
|
|
src.find("div[data-videoid]").each(function (key, value) {
|
|
|
|
var id = $(value).attr('data-videoid');
|
2015-09-25 11:09:43 +00:00
|
|
|
var style = $(value).attr('style');
|
|
|
|
var url = null;
|
|
|
|
if ($(value).hasClass('youtube')) {
|
|
|
|
url = 'https://www.youtube.com/embed/';
|
|
|
|
} else if ($(value).hasClass('vimeo')) {
|
|
|
|
url = 'https://player.vimeo.com/video/';
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
2015-09-25 11:09:43 +00:00
|
|
|
if (url) {
|
|
|
|
var iframe = $('<iframe frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>');
|
|
|
|
iframe.attr('src', url + id);
|
|
|
|
iframe.attr('style', style);
|
|
|
|
$(value).html(iframe);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2015-09-25 11:09:43 +00:00
|
|
|
});
|
|
|
|
//generate toc
|
|
|
|
var toc = $('#toc').clone();
|
|
|
|
toc.find('*').removeClass('active');
|
|
|
|
var tocAffix = $('#toc-affix').clone();
|
|
|
|
tocAffix.find('*').removeClass('active');
|
|
|
|
//generate html via template
|
2016-02-17 04:08:44 +00:00
|
|
|
$.get(serverurl + '/css/html.min.css', function (css) {
|
|
|
|
$.get(serverurl + '/views/html.hbs', function (data) {
|
2015-09-25 11:09:43 +00:00
|
|
|
var template = Handlebars.compile(data);
|
|
|
|
var context = {
|
2016-02-17 04:08:44 +00:00
|
|
|
url: serverurl,
|
2015-09-25 11:09:43 +00:00
|
|
|
title: title,
|
|
|
|
css: css,
|
|
|
|
html: src[0].outerHTML,
|
|
|
|
toc: toc.html(),
|
2016-01-12 14:01:42 +00:00
|
|
|
'toc-affix': tocAffix.html(),
|
|
|
|
robots: (md && md.meta && md.meta.robots) ? '<meta name="robots" content="' + md.meta.robots + '">' : null,
|
|
|
|
lang: (md && md.meta && md.meta.lang) ? 'lang="' + md.meta.lang + '"' : null,
|
|
|
|
dir: (md && md.meta && md.meta.dir) ? 'dir="' + md.meta.dir + '"' : null
|
2015-09-25 11:09:43 +00:00
|
|
|
};
|
|
|
|
var html = template(context);
|
|
|
|
// console.log(html);
|
|
|
|
var blob = new Blob([html], {
|
|
|
|
type: "text/html;charset=utf-8"
|
|
|
|
});
|
|
|
|
saveAs(blob, filename);
|
|
|
|
});
|
|
|
|
});
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//jQuery sortByDepth
|
|
|
|
$.fn.sortByDepth = function () {
|
|
|
|
var ar = this.map(function () {
|
|
|
|
return {
|
|
|
|
length: $(this).parents().length,
|
|
|
|
elt: this
|
|
|
|
}
|
|
|
|
}).get(),
|
|
|
|
result = [],
|
|
|
|
i = ar.length;
|
|
|
|
ar.sort(function (a, b) {
|
|
|
|
return a.length - b.length;
|
2015-06-01 10:04:25 +00:00
|
|
|
});
|
2015-07-01 16:10:20 +00:00
|
|
|
while (i--) {
|
|
|
|
result.push(ar[i].elt);
|
|
|
|
}
|
|
|
|
return $(result);
|
|
|
|
};
|
|
|
|
|
2015-09-25 09:41:15 +00:00
|
|
|
function toggleTodoEvent(e) {
|
|
|
|
var startline = $(this).closest('li').attr('data-startline') - 1;
|
|
|
|
var line = editor.getLine(startline);
|
|
|
|
var matches = line.match(/^[>\s]*[\-\+\*]\s\[([x ])\]/);
|
|
|
|
if (matches && matches.length >= 2) {
|
|
|
|
var checked = null;
|
|
|
|
if (matches[1] == 'x')
|
|
|
|
checked = true;
|
|
|
|
else if (matches[1] == ' ')
|
|
|
|
checked = false;
|
|
|
|
var replacements = matches[0].match(/(^[>\s]*[\-\+\*]\s\[)([x ])(\])/);
|
|
|
|
editor.replaceRange(checked ? ' ' : 'x', {
|
|
|
|
line: startline,
|
|
|
|
ch: replacements[1].length
|
|
|
|
}, {
|
|
|
|
line: startline,
|
|
|
|
ch: replacements[1].length + 1
|
|
|
|
}, '+input');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 16:10:20 +00:00
|
|
|
//remove hash
|
|
|
|
function removeHash() {
|
|
|
|
history.pushState("", document.title, window.location.pathname + window.location.search);
|
|
|
|
}
|
|
|
|
|
|
|
|
//toc
|
|
|
|
function generateToc(id) {
|
|
|
|
var target = $('#' + id);
|
|
|
|
target.html('');
|
|
|
|
new Toc('doc', {
|
|
|
|
'level': 3,
|
|
|
|
'top': -1,
|
|
|
|
'class': 'toc',
|
2015-12-20 17:28:54 +00:00
|
|
|
'ulClass': 'nav',
|
2015-07-01 16:10:20 +00:00
|
|
|
'targetId': id
|
2015-06-01 10:04:25 +00:00
|
|
|
});
|
2015-09-25 06:22:45 +00:00
|
|
|
if (target.text() == 'undefined')
|
2015-07-01 16:10:20 +00:00
|
|
|
target.html('');
|
|
|
|
var backtotop = $('<a class="back-to-top" href="#">Back to top</a>');
|
|
|
|
var gotobottom = $('<a class="go-to-bottom" href="#">Go to bottom</a>');
|
|
|
|
backtotop.click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
if (scrollToTop)
|
|
|
|
scrollToTop();
|
|
|
|
removeHash();
|
|
|
|
});
|
|
|
|
gotobottom.click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
if (scrollToBottom)
|
|
|
|
scrollToBottom();
|
|
|
|
removeHash();
|
|
|
|
});
|
|
|
|
target.append(backtotop).append(gotobottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
//smooth all hash trigger scrolling
|
|
|
|
function smoothHashScroll() {
|
|
|
|
var hashElements = $("a[href^='#']:not([smoothhashscroll])").toArray();
|
|
|
|
for (var i = 0; i < hashElements.length; i++) {
|
|
|
|
var element = hashElements[i];
|
|
|
|
var $element = $(element);
|
|
|
|
var hash = element.hash;
|
|
|
|
if (hash) {
|
|
|
|
$element.on('click', function (e) {
|
|
|
|
// store hash
|
2015-12-06 17:25:05 +00:00
|
|
|
var hash = decodeURIComponent(this.hash);
|
2015-07-01 16:10:20 +00:00
|
|
|
if ($(hash).length <= 0) return;
|
|
|
|
// prevent default anchor click behavior
|
|
|
|
e.preventDefault();
|
|
|
|
// animate
|
2015-09-25 06:22:45 +00:00
|
|
|
$('body, html').stop(true, true).animate({
|
2015-07-01 16:10:20 +00:00
|
|
|
scrollTop: $(hash).offset().top
|
|
|
|
}, 100, "linear", function () {
|
|
|
|
// when done, add hash to url
|
|
|
|
// (default click behaviour)
|
|
|
|
window.location.hash = hash;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$element.attr('smoothhashscroll', '');
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function imgPlayiframe(element, src) {
|
2016-02-11 09:45:13 +00:00
|
|
|
if (!$(element).attr("data-videoid")) return;
|
2015-05-04 07:53:29 +00:00
|
|
|
var iframe = $("<iframe frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>");
|
2016-02-11 09:45:13 +00:00
|
|
|
$(iframe).attr("src", src + $(element).attr("data-videoid") + '?autoplay=1');
|
2016-01-17 15:55:12 +00:00
|
|
|
$(element).find('img').css('visibility', 'hidden');
|
|
|
|
$(element).append(iframe);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var anchorForId = function (id) {
|
|
|
|
var anchor = document.createElement("a");
|
2016-04-20 10:22:18 +00:00
|
|
|
anchor.className = "anchor hidden-xs";
|
2015-05-04 07:53:29 +00:00
|
|
|
anchor.href = "#" + id;
|
2016-04-20 10:22:18 +00:00
|
|
|
anchor.innerHTML = "<span class=\"octicon octicon-link\"></span>";
|
2015-07-01 16:10:20 +00:00
|
|
|
anchor.title = id;
|
2015-05-04 07:53:29 +00:00
|
|
|
return anchor;
|
|
|
|
};
|
|
|
|
|
|
|
|
var linkifyAnchors = function (level, containingElement) {
|
|
|
|
var headers = containingElement.getElementsByTagName("h" + level);
|
|
|
|
for (var h = 0; h < headers.length; h++) {
|
|
|
|
var header = headers[h];
|
2016-04-20 10:22:18 +00:00
|
|
|
if (header.getElementsByClassName("anchor").length == 0) {
|
2015-07-01 16:10:20 +00:00
|
|
|
if (typeof header.id == "undefined" || header.id == "") {
|
|
|
|
//to escape characters not allow in css and humanize
|
|
|
|
var id = slugifyWithUTF8(header.innerHTML);
|
|
|
|
header.id = id;
|
|
|
|
}
|
2016-04-20 10:22:18 +00:00
|
|
|
header.insertBefore(anchorForId(header.id), header.firstChild);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function autoLinkify(view) {
|
|
|
|
var contentBlock = view[0];
|
|
|
|
if (!contentBlock) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var level = 1; level <= 6; level++) {
|
|
|
|
linkifyAnchors(level, contentBlock);
|
|
|
|
}
|
2015-09-25 06:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function deduplicatedHeaderId(view) {
|
2016-02-11 20:35:25 +00:00
|
|
|
var headers = view.find(':header.raw').removeClass('raw').toArray();
|
2015-09-25 06:27:23 +00:00
|
|
|
for (var i = 0; i < headers.length; i++) {
|
|
|
|
var id = $(headers[i]).attr('id');
|
|
|
|
if (!id) continue;
|
2016-02-11 20:35:25 +00:00
|
|
|
var duplicatedHeaders = view.find(':header[id="' + id + '"]').toArray();
|
2015-09-25 06:27:23 +00:00
|
|
|
for (var j = 0; j < duplicatedHeaders.length; j++) {
|
|
|
|
if (duplicatedHeaders[j] != headers[i]) {
|
|
|
|
var newId = id + j;
|
|
|
|
var $duplicatedHeader = $(duplicatedHeaders[j]);
|
|
|
|
$duplicatedHeader.attr('id', newId);
|
|
|
|
var $headerLink = $duplicatedHeader.find('> .header-link');
|
|
|
|
$headerLink.attr('href', '#' + newId);
|
|
|
|
$headerLink.attr('title', newId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2015-12-20 17:28:54 +00:00
|
|
|
function renderTOC(view) {
|
|
|
|
var tocs = view.find('.toc').toArray();
|
|
|
|
for (var i = 0; i < tocs.length; i++) {
|
|
|
|
var toc = $(tocs[i]);
|
|
|
|
var id = 'toc' + i;
|
|
|
|
toc.attr('id', id);
|
|
|
|
var target = $('#' + id);
|
|
|
|
target.html('');
|
|
|
|
new Toc('doc', {
|
|
|
|
'level': 3,
|
|
|
|
'top': -1,
|
|
|
|
'class': 'toc',
|
|
|
|
'targetId': id
|
|
|
|
});
|
|
|
|
if (target.text() == 'undefined')
|
|
|
|
target.html('');
|
|
|
|
target.replaceWith(target.html());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
function scrollToHash() {
|
|
|
|
var hash = location.hash;
|
|
|
|
location.hash = "";
|
|
|
|
location.hash = hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
function highlightRender(code, lang) {
|
|
|
|
if (!lang || /no(-?)highlight|plain|text/.test(lang))
|
|
|
|
return;
|
2015-07-01 16:10:20 +00:00
|
|
|
if (lang == 'sequence') {
|
|
|
|
return '<div class="sequence-diagram raw">' + code + '</div>';
|
|
|
|
} else if (lang == 'flow') {
|
|
|
|
return '<div class="flow-chart raw">' + code + '</div>';
|
2016-01-12 13:56:29 +00:00
|
|
|
} else if (lang == 'graphviz') {
|
|
|
|
return '<div class="graphviz raw">' + code + '</div>';
|
2016-03-04 15:26:27 +00:00
|
|
|
} else if (lang == 'mermaid') {
|
|
|
|
return '<div class="mermaid raw">' + code + '</div>';
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2015-10-22 09:15:44 +00:00
|
|
|
var reallang = lang.replace(/\=$|\=\d+$|\=\+$/, '');
|
2015-05-04 07:53:29 +00:00
|
|
|
var languages = hljs.listLanguages();
|
|
|
|
if (languages.indexOf(reallang) == -1) {
|
|
|
|
var result = hljs.highlightAuto(code);
|
|
|
|
} else {
|
|
|
|
var result = hljs.highlight(reallang, code);
|
|
|
|
}
|
2015-10-22 09:15:44 +00:00
|
|
|
var showlinenumbers = /\=$|\=\d+$|\=\+$/.test(lang);
|
2015-10-07 15:04:31 +00:00
|
|
|
if (showlinenumbers) {
|
|
|
|
var startnumber = 1;
|
|
|
|
var matches = lang.match(/\=(\d+)$/);
|
|
|
|
if (matches)
|
|
|
|
startnumber = parseInt(matches[1]);
|
2015-05-04 07:53:29 +00:00
|
|
|
var lines = result.value.split('\n');
|
|
|
|
var linenumbers = [];
|
2015-06-01 10:04:25 +00:00
|
|
|
for (var i = 0; i < lines.length - 1; i++) {
|
2015-10-07 15:04:31 +00:00
|
|
|
linenumbers[i] = "<span data-linenumber='" + (startnumber + i) + "'></span>";
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2015-10-22 09:15:44 +00:00
|
|
|
var continuelinenumber = /\=\+$/.test(lang);
|
|
|
|
var linegutter = "<div class='gutter linenumber" + (continuelinenumber ? " continue" : "") + "'>" + linenumbers.join('\n') + "</div>";
|
2015-05-04 07:53:29 +00:00
|
|
|
result.value = "<div class='wrapper'>" + linegutter + "<div class='code'>" + result.value + "</div></div>";
|
|
|
|
}
|
|
|
|
return result.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
emojify.setConfig({
|
2016-01-31 21:29:20 +00:00
|
|
|
blacklist: {
|
|
|
|
elements: ['script', 'textarea', 'a', 'pre', 'code', 'svg'],
|
|
|
|
classes: ['no-emojify']
|
|
|
|
},
|
2016-02-17 04:08:44 +00:00
|
|
|
img_dir: serverurl + '/vendor/emojify/images',
|
2015-05-04 07:53:29 +00:00
|
|
|
ignore_emoticons: true
|
|
|
|
});
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
var md = window.markdownit('default', {
|
2015-05-04 07:53:29 +00:00
|
|
|
html: true,
|
2015-07-01 16:10:20 +00:00
|
|
|
breaks: true,
|
|
|
|
langPrefix: "",
|
2015-05-04 07:53:29 +00:00
|
|
|
linkify: true,
|
|
|
|
typographer: true,
|
|
|
|
highlight: highlightRender
|
|
|
|
});
|
2016-02-22 00:58:06 +00:00
|
|
|
md.use(window.markdownitAbbr);
|
|
|
|
md.use(window.markdownitFootnote);
|
|
|
|
md.use(window.markdownitDeflist);
|
|
|
|
md.use(window.markdownitMark);
|
|
|
|
md.use(window.markdownitIns);
|
|
|
|
md.use(window.markdownitSub);
|
|
|
|
md.use(window.markdownitSup);
|
2016-02-24 02:03:35 +00:00
|
|
|
md.use(window.markdownitMathjax);
|
2016-03-15 02:56:53 +00:00
|
|
|
function renderContainer(tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('role', 'alert');
|
|
|
|
tokens[idx].attrJoin('class', 'alert');
|
|
|
|
tokens[idx].attrJoin('class', 'alert-' + tokens[idx].info.trim());
|
|
|
|
return self.renderToken.apply(self, arguments);
|
|
|
|
}
|
|
|
|
md.use(window.markdownitContainer, 'success', { render: renderContainer });
|
|
|
|
md.use(window.markdownitContainer, 'info', { render: renderContainer });
|
|
|
|
md.use(window.markdownitContainer, 'warning', { render: renderContainer });
|
|
|
|
md.use(window.markdownitContainer, 'danger', { render: renderContainer });
|
2016-02-22 00:58:06 +00:00
|
|
|
|
|
|
|
md.renderer.rules.image = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
|
|
|
return self.renderToken.apply(self, arguments);
|
|
|
|
};
|
|
|
|
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-07-01 16:10:20 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2015-07-01 16:10:20 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('class', 'raw');
|
|
|
|
return self.renderToken.apply(self, arguments);
|
2016-02-11 20:35:25 +00:00
|
|
|
};
|
2015-07-01 16:10:20 +00:00
|
|
|
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
|
2016-02-22 00:58:06 +00:00
|
|
|
var token = tokens[idx],
|
|
|
|
info = token.info ? md.utils.unescapeAll(token.info).trim() : '',
|
|
|
|
langName = '',
|
|
|
|
highlighted;
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
langName = info.split(/\s+/g)[0];
|
|
|
|
token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, ''));
|
|
|
|
token.attrJoin('class', 'hljs');
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.highlight) {
|
2016-02-22 00:58:06 +00:00
|
|
|
highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content);
|
2015-07-01 16:10:20 +00:00
|
|
|
} else {
|
2016-02-22 00:58:06 +00:00
|
|
|
highlighted = md.utils.escapeHtml(token.content);
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 00:58:06 +00:00
|
|
|
if (highlighted.indexOf('<pre') === 0) {
|
|
|
|
return highlighted + '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<pre><code' + self.renderAttrs(token) + '>'
|
|
|
|
+ highlighted
|
|
|
|
+ '</code></pre>\n';
|
2015-07-01 16:10:20 +00:00
|
|
|
};
|
2016-02-22 00:58:06 +00:00
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
//youtube
|
|
|
|
var youtubePlugin = new Plugin(
|
|
|
|
// regexp to match
|
|
|
|
/{%youtube\s*([\d\D]*?)\s*%}/,
|
|
|
|
|
|
|
|
// this function will be called when something matches
|
|
|
|
function (match, utils) {
|
|
|
|
var videoid = match[1];
|
|
|
|
if (!videoid) return;
|
2015-07-01 16:10:20 +00:00
|
|
|
var div = $('<div class="youtube raw"></div>');
|
2016-02-11 09:45:13 +00:00
|
|
|
div.attr('data-videoid', videoid);
|
2015-09-25 10:55:56 +00:00
|
|
|
var thumbnail_src = '//img.youtube.com/vi/' + videoid + '/hqdefault.jpg';
|
|
|
|
var image = '<img src="' + thumbnail_src + '" />';
|
|
|
|
div.append(image);
|
2015-05-04 07:53:29 +00:00
|
|
|
var icon = '<i class="icon fa fa-youtube-play fa-5x"></i>';
|
|
|
|
div.append(icon);
|
|
|
|
return div[0].outerHTML;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
//vimeo
|
|
|
|
var vimeoPlugin = new Plugin(
|
|
|
|
// regexp to match
|
|
|
|
/{%vimeo\s*([\d\D]*?)\s*%}/,
|
|
|
|
|
|
|
|
// this function will be called when something matches
|
|
|
|
function (match, utils) {
|
|
|
|
var videoid = match[1];
|
|
|
|
if (!videoid) return;
|
2015-07-01 16:10:20 +00:00
|
|
|
var div = $('<div class="vimeo raw"></div>');
|
2016-02-11 09:45:13 +00:00
|
|
|
div.attr('data-videoid', videoid);
|
2015-05-04 07:53:29 +00:00
|
|
|
var icon = '<i class="icon fa fa-vimeo-square fa-5x"></i>';
|
|
|
|
div.append(icon);
|
|
|
|
return div[0].outerHTML;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
//gist
|
|
|
|
var gistPlugin = new Plugin(
|
|
|
|
// regexp to match
|
|
|
|
/{%gist\s*([\d\D]*?)\s*%}/,
|
|
|
|
|
|
|
|
// this function will be called when something matches
|
|
|
|
function (match, utils) {
|
|
|
|
var gistid = match[1];
|
|
|
|
var code = '<code data-gist-id="' + gistid + '"/>';
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
);
|
2015-12-20 17:28:54 +00:00
|
|
|
//TOC
|
|
|
|
var tocPlugin = new Plugin(
|
|
|
|
// regexp to match
|
|
|
|
/^\[TOC\]$/,
|
|
|
|
|
|
|
|
// this function will be called when something matches
|
|
|
|
function (match, utils) {
|
|
|
|
return '<div class="toc"></div>';
|
|
|
|
}
|
|
|
|
);
|
2015-12-22 23:51:07 +00:00
|
|
|
//slideshare
|
|
|
|
var slidesharePlugin = new Plugin(
|
|
|
|
// regexp to match
|
|
|
|
/{%slideshare\s*([\d\D]*?)\s*%}/,
|
|
|
|
|
|
|
|
// this function will be called when something matches
|
|
|
|
function (match, utils) {
|
|
|
|
var slideshareid = match[1];
|
|
|
|
var div = $('<div class="slideshare raw"></div>');
|
2016-02-11 09:45:13 +00:00
|
|
|
div.attr('data-slideshareid', slideshareid);
|
2015-12-22 23:51:07 +00:00
|
|
|
return div[0].outerHTML;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
//speakerdeck
|
|
|
|
var speakerdeckPlugin = new Plugin(
|
|
|
|
// regexp to match
|
|
|
|
/{%speakerdeck\s*([\d\D]*?)\s*%}/,
|
|
|
|
|
|
|
|
// this function will be called when something matches
|
|
|
|
function (match, utils) {
|
|
|
|
var speakerdeckid = match[1];
|
|
|
|
var div = $('<div class="speakerdeck raw"></div>');
|
2016-02-11 09:45:13 +00:00
|
|
|
div.attr('data-speakerdeckid', speakerdeckid);
|
2015-12-22 23:51:07 +00:00
|
|
|
return div[0].outerHTML;
|
|
|
|
}
|
|
|
|
);
|
2016-01-12 14:01:42 +00:00
|
|
|
|
|
|
|
//yaml meta, from https://github.com/eugeneware/remarkable-meta
|
|
|
|
function get(state, line) {
|
|
|
|
var pos = state.bMarks[line];
|
|
|
|
var max = state.eMarks[line];
|
|
|
|
return state.src.substr(pos, max - pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
function meta(state, start, end, silent) {
|
|
|
|
if (start !== 0 || state.blkIndent !== 0) return false;
|
|
|
|
if (state.tShift[start] < 0) return false;
|
|
|
|
if (!get(state, start).match(/^---$/)) return false;
|
|
|
|
|
|
|
|
var data = [];
|
|
|
|
for (var line = start + 1; line < end; line++) {
|
|
|
|
var str = get(state, line);
|
|
|
|
if (str.match(/^(\.{3}|-{3})$/)) break;
|
|
|
|
if (state.tShift[line] < 0) break;
|
|
|
|
data.push(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line >= end) return false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
md.meta = jsyaml.safeLoad(data.join('\n')) || {};
|
|
|
|
} catch(err) {
|
2016-03-04 15:19:49 +00:00
|
|
|
console.warn(err);
|
2016-01-12 14:01:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.line = line + 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function metaPlugin(md) {
|
|
|
|
md.meta = md.meta || {};
|
|
|
|
md.block.ruler.before('code', 'meta', meta, {
|
|
|
|
alt: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
md.use(metaPlugin);
|
2015-05-04 07:53:29 +00:00
|
|
|
md.use(youtubePlugin);
|
|
|
|
md.use(vimeoPlugin);
|
|
|
|
md.use(gistPlugin);
|
2015-12-22 23:51:07 +00:00
|
|
|
md.use(tocPlugin);
|
|
|
|
md.use(slidesharePlugin);
|
|
|
|
md.use(speakerdeckPlugin);
|