Convert pretty.js to es6

This commit is contained in:
Yukai Huang 2017-01-05 16:19:13 +08:00
parent eb5e7ba0d1
commit 45c202172e

View file

@ -4,31 +4,33 @@ require('../css/site.css');
require('highlight.js/styles/github-gist.css'); require('highlight.js/styles/github-gist.css');
var extra = require('./extra'); import {
var md = extra.md; autoLinkify,
var finishView = extra.finishView; deduplicatedHeaderId,
var autoLinkify = extra.autoLinkify; finishView,
var deduplicatedHeaderId = extra.deduplicatedHeaderId; generateToc,
var renderTOC = extra.renderTOC; md,
var generateToc = extra.generateToc; parseMeta,
var smoothHashScroll = extra.smoothHashScroll; postProcess,
var postProcess = extra.postProcess; renderTOC,
var updateLastChange = extra.updateLastChange; scrollToHash,
var parseMeta = extra.parseMeta; smoothHashScroll,
var scrollToHash = extra.scrollToHash; updateLastChange
var preventXSS = require('./render').preventXSS; } from './extra';
var markdown = $("#doc.markdown-body"); import { preventXSS } from './render';
var text = markdown.text();
var lastMeta = md.meta; const markdown = $("#doc.markdown-body");
const text = markdown.text();
const lastMeta = md.meta;
md.meta = {}; md.meta = {};
var rendered = md.render(text); let rendered = md.render(text);
if (md.meta.type && md.meta.type === 'slide') { if (md.meta.type && md.meta.type === 'slide') {
var slideOptions = { const slideOptions = {
separator: '^(\r\n?|\n)---(\r\n?|\n)$', separator: '^(\r\n?|\n)---(\r\n?|\n)$',
verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$' verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$'
}; };
var slides = RevealMarkdown.slidify(text, slideOptions); const slides = RevealMarkdown.slidify(text, slideOptions);
markdown.html(slides); markdown.html(slides);
RevealMarkdown.initialize(); RevealMarkdown.initialize();
// prevent XSS // prevent XSS
@ -46,10 +48,11 @@ if (md.meta.type && md.meta.type === 'slide') {
} }
// prevent XSS // prevent XSS
rendered = preventXSS(rendered); rendered = preventXSS(rendered);
var result = postProcess(rendered); const result = postProcess(rendered);
markdown.html(result.html()); markdown.html(result.html());
} }
$(document.body).show(); $(document.body).show();
finishView(markdown); finishView(markdown);
autoLinkify(markdown); autoLinkify(markdown);
deduplicatedHeaderId(markdown); deduplicatedHeaderId(markdown);
@ -60,17 +63,18 @@ smoothHashScroll();
createtime = lastchangeui.time.attr('data-createtime'); createtime = lastchangeui.time.attr('data-createtime');
lastchangetime = lastchangeui.time.attr('data-updatetime'); lastchangetime = lastchangeui.time.attr('data-updatetime');
updateLastChange(); updateLastChange();
var url = window.location.pathname;
$('.ui-edit').attr('href', url + '/edit'); const url = window.location.pathname;
var toc = $('.ui-toc'); $('.ui-edit').attr('href', `${url}/edit`);
var tocAffix = $('.ui-affix-toc'); const toc = $('.ui-toc');
var tocDropdown = $('.ui-toc-dropdown'); const tocAffix = $('.ui-affix-toc');
const tocDropdown = $('.ui-toc-dropdown');
//toc //toc
tocDropdown.click(function (e) { tocDropdown.click(e => {
e.stopPropagation(); e.stopPropagation();
}); });
var enoughForAffixToc = true; let enoughForAffixToc = true;
function generateScrollspy() { function generateScrollspy() {
$(document.body).scrollspy({ $(document.body).scrollspy({
@ -89,18 +93,18 @@ function generateScrollspy() {
function windowResize() { function windowResize() {
//toc right //toc right
var paddingRight = parseFloat(markdown.css('padding-right')); const paddingRight = parseFloat(markdown.css('padding-right'));
var right = ($(window).width() - (markdown.offset().left + markdown.outerWidth() - paddingRight)); const right = ($(window).width() - (markdown.offset().left + markdown.outerWidth() - paddingRight));
toc.css('right', right + 'px'); toc.css('right', `${right}px`);
//affix toc left //affix toc left
var newbool; let newbool;
var rightMargin = (markdown.parent().outerWidth() - markdown.outerWidth()) / 2; const rightMargin = (markdown.parent().outerWidth() - markdown.outerWidth()) / 2;
//for ipad or wider device //for ipad or wider device
if (rightMargin >= 133) { if (rightMargin >= 133) {
newbool = true; newbool = true;
var affixLeftMargin = (tocAffix.outerWidth() - tocAffix.width()) / 2; const affixLeftMargin = (tocAffix.outerWidth() - tocAffix.width()) / 2;
var left = markdown.offset().left + markdown.outerWidth() - affixLeftMargin; const left = markdown.offset().left + markdown.outerWidth() - affixLeftMargin;
tocAffix.css('left', left + 'px'); tocAffix.css('left', `${left}px`);
} else { } else {
newbool = false; newbool = false;
} }
@ -109,10 +113,10 @@ function windowResize() {
generateScrollspy(); generateScrollspy();
} }
} }
$(window).resize(function () { $(window).resize(() => {
windowResize(); windowResize();
}); });
$(document).ready(function () { $(document).ready(() => {
windowResize(); windowResize();
generateScrollspy(); generateScrollspy();
setTimeout(scrollToHash, 0); setTimeout(scrollToHash, 0);
@ -120,13 +124,13 @@ $(document).ready(function () {
$('[data-toggle="tooltip"]').tooltip(); $('[data-toggle="tooltip"]').tooltip();
}); });
function scrollToTop() { export function scrollToTop() {
$('body, html').stop(true, true).animate({ $('body, html').stop(true, true).animate({
scrollTop: 0 scrollTop: 0
}, 100, "linear"); }, 100, "linear");
} }
function scrollToBottom() { export function scrollToBottom() {
$('body, html').stop(true, true).animate({ $('body, html').stop(true, true).animate({
scrollTop: $(document.body)[0].scrollHeight scrollTop: $(document.body)[0].scrollHeight
}, 100, "linear"); }, 100, "linear");
@ -134,8 +138,3 @@ function scrollToBottom() {
window.scrollToTop = scrollToTop; window.scrollToTop = scrollToTop;
window.scrollToBottom = scrollToBottom; window.scrollToBottom = scrollToBottom;
module.exports = {
scrollToBottom: scrollToBottom,
scrollToTop: scrollToTop
}