Removed jQuery Dependency from My Math Editor

Background

jQuery provides many handy functions to speed up the development, for example document.ready(), element.toggle(), element.hide(), etc. My math editor used some of them. However, I replaced them with Vanilla JavaScript at commit 4a35c45b.

Goal

Some users complained about the white background in the past versions of my editor. That had motivated me to refactor the code, so that it would be easier to maintain and to add in new features.

HTML5 input types used

Actual appearance might vary across browsers.

[Read More]

Staticman Script in Pure JS

Staticman without jQuery

The official Staticman script sample depends on jQuery. I have been using jQuery in my Staticman script for nested comments for more than six months.

Recently, in the new release of Hugo Swift Theme, in which I’m a collaborator, one has removed all jQuery methods at commit f137efa4. Here’s the structure of the script:

  1. auxiliary functions like elem, elems, pushClass, deleteClass, etc.
  2. A self-executing function comments()
    • some necessary local variables
    • handleForm() consisting merely of an event listener handling form submission.
      • formToJSON(form) for converting the comment form to a JSON string.
      • fetch(url, {...}).then(...).catch(...) for submitting a sever request and handling its response. It calls showModal(...) and it contains resetForm().
    • a conditional form ? handleForm(form) : false; to execute the above method provided the existence of the comment form.
    • closeModal() and showModal() for closing and showing the modal respectively.
    • a conditional containsClass(body, show) ? closeModal() : false;.
    • a self-executing toogleForm() function. Nesting a self-executing function inside another one limits the namespace of variables. Even though there’s no new variable defined inside this function, there’s a need to wrap it with parenthesis () because toggleForm() is not called elsewhere, unlike toggleMenu(). If the if(button){...} statement comes out directly, we will not know that it’s doing.

JavaScript Copy Button

Goal

To create a copy button for my Math.SE comment template in order to save the trouble of copying and pasting.

My first attempt

I put the boilerplate inside a Markdown codeblock to prevent them from getting interpreted by Hugo’s Markdown parser. Under each codeblock, I placed the copy button.

Comment boilerplate goes here ...

Another comment boilerplate goes here ...

My page’s original layout

$(document).ready(function() {
  $('.copyBtn').click(function() {
    copy($(this).prev().children())
j  });
});

function copy(selector) {
  var screenTop = $(document).scrollTop();
  var $temp = $("<div>");
  $("body").append($temp);
  $temp.attr("contenteditable", true)
       .html($(selector).html()).select()
       .on("focus", function() { document.execCommand('selectAll',false,null) })
       .focus();
  document.execCommand("copy");
  $temp.remove();
  $('html, body').scrollTop(screenTop);
}

static/js/copyBtn.js at Git tag copyBtn0

[Read More]

Fix Hugo Table of Contents

Removal of wrapping HTML tag in JavaScript

Background (TL;DR)

While setting up the new version of Staticman for my demo GitLab pages, I’ve read developers’ documentations, setup guide and some community blog posts so as to come up with my own guide. It’s originated and inspired from a variety of sources, and refined according to hours of testing. Consequently, despite the original intention to keep things simple, I’ve finally come up with a post with over a thousand words.

To pass my ideas in this post to visitors, it’s better that they have an overview of the contents before actually looking into the details. Therefore, a table of contents is nice-to-have feature for this blog.

[Read More]