HackMD/public/js/extra.js

479 lines
15 KiB
JavaScript
Raw Normal View History

2015-07-01 16:10:20 +00:00
//auto update last change
var lastchangetime = null;
var lastchangeui = null;
function updateLastChange() {
if (lastchangetime && lastchangeui) {
lastchangeui.html('&nbsp;<i class="fa fa-clock-o"></i> change ' + moment(lastchangetime).fromNow());
lastchangeui.attr('title', moment(lastchangetime).format('llll'));
}
}
setInterval(updateLastChange, 60000);
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;
}
2015-05-15 04:58:13 +00:00
var viewAjaxCallback = null;
2015-07-01 16:10:20 +00:00
//regex for blockquote
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-05-04 07:53:29 +00:00
//dynamic event or object binding here
function finishView(view) {
//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',
2015-05-15 04:58:13 +00:00
url: '//vimeo.com/api/v2/video/' + $(value).attr('videoid') + '.json',
2015-05-04 07:53:29 +00:00
jsonp: 'callback',
dataType: 'jsonp',
success: function (data) {
var thumbnail_src = data[0].thumbnail_large;
$(value).css('background-image', 'url(' + thumbnail_src + ')');
}
});
});
//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
emojify.run(view[0]);
//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 {
for (var i = 0; i < mathjaxdivs.length; i++) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, mathjaxdivs[i].innerHTML]);
2015-06-01 10:04:25 +00:00
MathJax.Hub.Queue(viewAjaxCallback);
2015-05-04 07:53:29 +00:00
}
2015-07-01 16:10:20 +00:00
} catch (err) {}
2015-05-04 07:53:29 +00:00
//sequence diagram
2015-07-01 16:10:20 +00:00
var sequence = view.find(".sequence-diagram.raw").removeClass("raw");
2015-05-04 07:53:29 +00:00
try {
sequence.sequenceDiagram({
theme: 'simple'
});
sequence.parent().parent().replaceWith(sequence);
2015-07-01 16:10:20 +00:00
} catch (err) {
2015-05-04 07:53:29 +00:00
console.error(err);
}
//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 {
var chart = flowchart.parse($(value).text());
$(value).html('');
chart.drawSVG(value, {
'line-width': 2,
'fill': 'none',
'font-size': '16px',
'font-family': "'Andale Mono', monospace"
});
$(value).parent().parent().replaceWith(value);
2015-07-01 16:10:20 +00:00
} catch (err) {
2015-05-04 07:53:29 +00:00
console.error(err);
}
});
2015-07-01 16:10:20 +00:00
//image href new window(emoji not included)
var images = view.find("img.raw[src]").removeClass("raw");
2015-07-01 16:10:20 +00:00
images.each(function (key, value) {
var src = $(value).attr('src');
var a = $('<a>');
if (src) {
a.attr('href', src);
a.attr('target', "_blank");
}
var clone = $(value).clone();
clone[0].onload = function (e) {
if(viewAjaxCallback)
viewAjaxCallback();
};
a.html(clone);
2015-07-01 16:10:20 +00:00
$(value).replaceWith(a);
});
//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();
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>');
$(value).html(html);
});
var blockquote_color = blockquote.find(".color");
blockquote_color.each(function (key, value) {
$(value).closest("blockquote").css('border-left-color', $(value).attr('data-color'));
});
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>');
//prevent XSS
result.find("script").replaceWith(function () {
return "<noscript>" + $(this).html() + "</noscript>"
});
result.find("iframe").replaceWith(function () {
return "<noiframe>" + $(this).html() + "</noiframe>"
});
//todo list
2015-07-01 16:10:20 +00:00
var lis = result.find('li.raw').removeClass("raw").sortByDepth().toArray();
2015-05-04 07:53:29 +00:00
for (var i = 0; i < lis.length; i++) {
2015-07-01 16:10:20 +00:00
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];
}
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>');
2015-05-04 07:53:29 +00:00
lis[i].setAttribute('class', 'task-list-item');
}
}
2015-07-01 16:10:20 +00:00
return result;
}
//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);
};
//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',
'targetId': id
2015-06-01 10:04:25 +00:00
});
2015-07-01 16:10:20 +00:00
if(target.text() == 'undefined')
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
var hash = this.hash;
if ($(hash).length <= 0) return;
// prevent default anchor click behavior
e.preventDefault();
// animate
$('html, body').animate({
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 setSizebyAttr(element, target) {
var width = $(element).attr("width") ? $(element).attr("width") : '100%';
var height = $(element).attr("height") ? $(element).attr("height") : '360px';
$(target).width(width);
$(target).height(height);
}
function imgPlayiframe(element, src) {
if (!$(element).attr("videoid")) return;
var iframe = $("<iframe frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>");
$(iframe).attr("src", src + $(element).attr("videoid") + '?autoplay=1');
setSizebyAttr(element, iframe);
$(element).html(iframe);
}
var anchorForId = function (id) {
var anchor = document.createElement("a");
2015-07-01 16:10:20 +00:00
anchor.className = "header-link hidden-xs";
2015-05-04 07:53:29 +00:00
anchor.href = "#" + id;
2015-07-01 16:10:20 +00:00
anchor.innerHTML = "<span class=\"sr-only\"></span><i class=\"fa fa-link\"></i>";
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];
2015-07-01 16:10:20 +00:00
if (header.getElementsByClassName("header-link").length == 0) {
if (typeof header.id == "undefined" || header.id == "") {
//to escape characters not allow in css and humanize
var id = slugifyWithUTF8(header.innerHTML);
header.id = id;
}
header.appendChild(anchorForId(header.id));
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);
}
};
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>';
2015-05-04 07:53:29 +00:00
}
var reallang = lang.replace('=', '');
var languages = hljs.listLanguages();
if (languages.indexOf(reallang) == -1) {
var result = hljs.highlightAuto(code);
} else {
var result = hljs.highlight(reallang, code);
}
if (/\=$/.test(lang)) {
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-05-04 07:53:29 +00:00
linenumbers[i] = "<div class='linenumber'>" + (i + 1) + "</div>";
}
var linegutter = "<div class='gutter'>" + linenumbers.join('\n') + "</div>";
result.value = "<div class='wrapper'>" + linegutter + "<div class='code'>" + result.value + "</div></div>";
}
return result.value;
}
emojify.setConfig({
img_dir: '/vendor/emojify/images',
ignore_emoticons: true
});
var md = new Remarkable('full', {
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
});
2015-07-01 16:10:20 +00:00
md.renderer.rules.list_item_open = function (/* tokens, idx, options, env */) {
return '<li class="raw">';
};
md.renderer.rules.blockquote_open = function (tokens, idx /*, options, env */ ) {
return '<blockquote class="raw">\n';
};
md.renderer.rules.hardbreak = function (tokens, idx, options /*, env */ ) {
return md.options.xhtmlOut ? '<br /><br />' : '<br><br>';
};
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
var token = tokens[idx];
var langClass = '';
var langPrefix = options.langPrefix;
var langName = '',
fenceName;
var highlighted;
if (token.params) {
//
// ```foo bar
//
// Try custom renderer "foo" first. That will simplify overwrite
// for diagrams, latex, and any other fenced block with custom look
//
fenceName = token.params.split(/\s+/g)[0];
if (Remarkable.utils.has(self.rules.fence_custom, fenceName)) {
return self.rules.fence_custom[fenceName](tokens, idx, options, env, self);
}
langName = Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(Remarkable.utils.unescapeMd(fenceName)));
langClass = ' class="' + langPrefix + langName.replace('=', '') + ' hljs"';
}
if (options.highlight) {
highlighted = options.highlight(token.content, langName) || Remarkable.utils.escapeHtml(token.content);
} else {
highlighted = Remarkable.utils.escapeHtml(token.content);
}
return '<pre><code' + langClass + '>' + highlighted + '</code></pre>' + md.renderer.getBreak(tokens, idx);
};
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>');
2015-05-04 07:53:29 +00:00
setSizebyAttr(div, div);
div.attr('videoid', videoid);
var icon = '<i class="icon fa fa-youtube-play fa-5x"></i>';
div.append(icon);
var thumbnail_src = '//img.youtube.com/vi/' + videoid + '/hqdefault.jpg';
div.css('background-image', 'url(' + thumbnail_src + ')');
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>');
2015-05-04 07:53:29 +00:00
setSizebyAttr(div, div);
div.attr('videoid', videoid);
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;
}
);
//mathjax
var mathjaxPlugin = new Plugin(
// regexp to match
/^\$\$\n([\d\D]*?)\n\$\$$|\$([\d\D]*?)\$/,
// this function will be called when something matches
function (match, utils) {
//var code = $(match).text();
2015-07-01 16:10:20 +00:00
return '<span class="mathjax raw">' + match[0] + '</span>';
2015-05-04 07:53:29 +00:00
}
);
md.use(youtubePlugin);
md.use(vimeoPlugin);
md.use(gistPlugin);
md.use(mathjaxPlugin);