1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-12-19 06:06:54 +00:00

Process JS files with gulp

This commit is contained in:
Cotes Chung
2021-01-23 15:07:18 +08:00
parent 0e9104a58f
commit da9f7b8218
37 changed files with 380 additions and 326 deletions

View File

@@ -7,27 +7,28 @@
*/
$(function() {
var childPrefix = "l_";
var parentPrefix = "h_";
const childPrefix = "l_";
const parentPrefix = "h_";
const collapse = $(".collapse");
/* close up top-category */
$(".collapse").on("hide.bs.collapse", function() { /* Bootstrap collapse events. */
var parentId = parentPrefix + $(this).attr("id").substring(childPrefix.length);
collapse.on("hide.bs.collapse", function () { /* Bootstrap collapse events. */
const parentId = parentPrefix + $(this).attr("id").substring(childPrefix.length);
if (parentId) {
$("#" + parentId + " .far.fa-folder-open").attr("class", "far fa-folder fa-fw");
$("#" + parentId + " i.fas").addClass("rotate");
$("#" + parentId).removeClass("hide-border-bottom");
$(`#${parentId} .far.fa-folder-open`).attr("class", "far fa-folder fa-fw");
$(`#${parentId} i.fas`).addClass("rotate");
$(`#${parentId}`).removeClass("hide-border-bottom");
}
});
/* expand the top category */
$(".collapse").on("show.bs.collapse", function() {
var parentId = parentPrefix + $(this).attr("id").substring(childPrefix.length);
collapse.on("show.bs.collapse", function() {
const parentId = parentPrefix + $(this).attr("id").substring(childPrefix.length);
if (parentId) {
$("#" + parentId + " .far.fa-folder").attr("class", "far fa-folder-open fa-fw");
$("#" + parentId + " i.fas").removeClass("rotate");
$("#" + parentId).addClass("hide-border-bottom");
$(`#${parentId} .far.fa-folder`).attr("class", "far fa-folder-open fa-fw");
$(`#${parentId} i.fas`).removeClass("rotate");
$(`#${parentId}`).addClass("hide-border-bottom");
}
});
});
});

View File

@@ -8,9 +8,6 @@
*/
$(function() {
var MARK="img-hyperlink";
const MARK = "img-hyperlink";
$("a:has(img)").addClass(MARK);
});

View File

@@ -7,11 +7,11 @@ $(function() {
const regex = new RegExp(`^${prefix}([a-z])+$`);
$(`div[class^=${prefix}`).each(function() {
let clzsses = $(this).attr("class").split(" ");
let classes = $(this).attr("class").split(" ");
clzsses.forEach((clzss) => {
if (regex.test(clzss)) {
let lang = clzss.substring(prefix.length);
classes.forEach((_class) => {
if (regex.test(_class)) {
let lang = _class.substring(prefix.length);
$(this).attr("lang", `${lang}`);
}
});

View File

@@ -1,7 +1,7 @@
/**
* Count pageviews form GA or local cache file.
* Count page views form GA or local cache file.
*
* Dependences:
* Dependencies:
* - jQuery
* - countUp.js <https://github.com/inorganik/countUp.js>
*
@@ -11,8 +11,8 @@
* MIT License
*/
var getInitStatus = (function () {
var hasInit = false;
const getInitStatus = (function () {
let hasInit = false;
return () => {
let ret = hasInit;
if (!hasInit) {
@@ -22,13 +22,26 @@ var getInitStatus = (function () {
};
}());
const PvOpts = (function () {
return {
isEnabled() {
return "true" === $("meta[name=pv-cache-enabled]").attr("content");
},
getProxyEndpoint() {
return $("meta[name=pv-proxy-endpoint]").attr("content");
},
getLocalData() {
return $("meta[name=pv-cache-data]").attr("content");
}
}
}());
var PvCache = (function () {
const PvCache = (function () {
const KEY_PV = "pv";
const KEY_CREATION = "pv_created_date";
const KEY_PV_SRC = "pv_source";
var Source = {
const Source = {
ORIGIN: "origin",
PROXY: "proxy"
};
@@ -43,17 +56,18 @@ var PvCache = (function () {
return {
getData() {
return JSON.parse(localStorage.getItem(KEY_PV) );
// get data from browser cache
return JSON.parse(localStorage.getItem(KEY_PV));
},
saveOriginCache(pv) {
set(KEY_PV, pv);
set(KEY_PV_SRC, Source.ORIGIN );
set(KEY_CREATION, new Date().toJSON() );
set(KEY_PV_SRC, Source.ORIGIN);
set(KEY_CREATION, new Date().toJSON());
},
saveProxyCache(pv) {
set(KEY_PV, pv);
set(KEY_PV_SRC, Source.PROXY );
set(KEY_CREATION, new Date().toJSON() );
set(KEY_PV_SRC, Source.PROXY);
set(KEY_CREATION, new Date().toJSON());
},
isFromOrigin() {
return get(KEY_PV_SRC) === Source.ORIGIN;
@@ -62,23 +76,23 @@ var PvCache = (function () {
return get(KEY_PV_SRC) === Source.PROXY;
},
isExpired() {
if (PvCache.isFromOrigin() ) {
if (PvCache.isFromOrigin()) {
let date = new Date(get(KEY_CREATION));
date.setDate(date.getDate() + 1); /* update origin records every day */
return Date.now() >= date.getTime();
} else if (PvCache.isFromProxy() ) {
let date = new Date(get(KEY_CREATION) );
} else if (PvCache.isFromProxy()) {
let date = new Date(get(KEY_CREATION));
date.setHours(date.getHours() + 1); /* update proxy records per hour */
return Date.now() >= date.getTime();
}
return false;
},
getAllPagevies() {
getAllPageviews() {
return PvCache.getData().totalsForAllResults["ga:pageviews"];
},
newerThan(pv) {
return PvCache.getAllPagevies() > pv.totalsForAllResults["ga:pageviews"];
return PvCache.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
},
inspectKeys() {
if (localStorage.getItem(KEY_PV) === null
@@ -91,9 +105,10 @@ var PvCache = (function () {
}()); /* PvCache */
function countUp(min, max, destId) {
if (min < max) {
var numAnim = new CountUp(destId, min, max);
let numAnim = new CountUp(destId, min, max);
if (!numAnim.error) {
numAnim.start();
} else {
@@ -104,11 +119,11 @@ function countUp(min, max, destId) {
function countPV(path, rows) {
var count = 0;
let count = 0;
if (typeof rows !== "undefined" ) {
for (var i = 0; i < rows.length; ++i) {
var gaPath = rows[parseInt(i, 10)][0];
for (let i = 0; i < rows.length; ++i) {
const gaPath = rows[parseInt(i, 10)][0];
if (gaPath === path) { /* path format see: site.permalink */
count += parseInt(rows[parseInt(i, 10)][1], 10);
break;
@@ -121,13 +136,13 @@ function countPV(path, rows) {
function tacklePV(rows, path, elem, hasInit) {
var count = countPV(path, rows);
let count = countPV(path, rows);
count = (count === 0 ? 1 : count);
if (!hasInit) {
elem.text(new Intl.NumberFormat().format(count));
} else {
var initCount = parseInt(elem.text().replace(/,/g, ""), 10);
const initCount = parseInt(elem.text().replace(/,/g, ""), 10);
if (count > initCount) {
countUp(initCount, count, elem.attr("id"));
}
@@ -140,17 +155,17 @@ function displayPageviews(data) {
return;
}
var hasInit = getInitStatus();
var rows = data.rows; /* could be undefined */
let hasInit = getInitStatus();
const rows = data.rows; /* could be undefined */
if ($("#post-list").length > 0) { /* the Home page */
$(".post-preview").each(function() {
var path = $(this).find("a").attr("href");
const path = $(this).find("a").attr("href");
tacklePV(rows, path, $(this).find(".pageviews"), hasInit);
});
} else if ($(".post").length > 0) { /* the post */
var path = window.location.pathname;
const path = window.location.pathname;
tacklePV(rows, path, $("#pv"), hasInit);
}
}
@@ -159,7 +174,7 @@ function displayPageviews(data) {
function fetchProxyPageviews() {
$.ajax({
type: "GET",
url: proxyEndpoint, /* see: /assets/js/_pv-config.js */
url: PvOpts.getProxyEndpoint(),
dataType: "jsonp",
jsonpCallback: "displayPageviews",
success: (data, textStatus, jqXHR) => {
@@ -173,9 +188,8 @@ function fetchProxyPageviews() {
function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
/* pvCacheEnabled, pvCacheData see: /assets/js/_pv-config.js */
if (pvCacheEnabled && fetchOrigin) {
fetch(pvCacheData)
if (PvOpts.isEnabled() && fetchOrigin) {
fetch(PvOpts.getLocalData())
.then((response) => response.json())
.then((data) => {
if (filterOrigin) {
@@ -196,9 +210,7 @@ function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
$(function() {
if ($(".pageviews").length > 0) {
PvCache.inspectKeys();
let cache = PvCache.getData();

View File

@@ -1,6 +1,6 @@
/*
Safari doesn't support CSS `scroll-behavior: smooth`,
so here is a compatible sollution for all browser to smooth scrolling
so here is a compatible solution for all browser to smooth scrolling
See: <https://css-tricks.com/snippets/jquery/smooth-scrolling/>
@@ -13,66 +13,67 @@ $(function() {
.not("[href='#0']")
.click(function(event) {
if (location.pathname.replace(/^\//, "") === this.pathname.replace(/^\//, "")
&& location.hostname === this.hostname) {
if (this.pathname.replace(/^\//, "") === location.pathname.replace(/^\//, "")) {
if (location.hostname === this.hostname) {
const REM = 16; /* 16px */
const REM = 16; /* 16px */
const hash = decodeURI(this.hash);
let isFnRef = RegExp(/^#fnref:/).test(hash);
let isFn = RegExp(/^#fn:/).test(hash);
let selector = hash.includes(":") ? hash.replace(/\:/, "\\:") : hash;
const target = $(selector);
const hash = decodeURI(this.hash);
let isFnRef = RegExp(/^#fnref:/).test(hash);
let isFn = RegExp(/^#fn:/).test(hash);
let selector = hash.includes(":") ? hash.replace(/\:/, "\\:") : hash;
let target = $(selector);
if (target.length) {
event.preventDefault();
if (target.length) {
event.preventDefault();
if (history.pushState) { /* add hash to URL */
history.pushState(null, null, hash);
if (history.pushState) { /* add hash to URL */
history.pushState(null, null, hash);
}
let curOffset = $(this).offset().top;
let destOffset = target.offset().top;
const scrollUp = (destOffset < curOffset);
const topbarHeight = $("#topbar-wrapper").outerHeight();
if (scrollUp && isFnRef) {
/* Avoid the top-bar covering `fnref` when scrolling up
because `fnref` has no `%anchor`(see: module.scss) style. */
destOffset -= (topbarHeight + REM / 2);
}
$("html,body").animate({
scrollTop: destOffset
}, 800, () => {
const $target = $(target);
$target.focus();
const SCROLL_MARK = "scroll-focus";
/* clean up old scroll mark */
if ($(`[${SCROLL_MARK}=true]`).length) {
$(`[${SCROLL_MARK}=true]`).attr(SCROLL_MARK, false);
}
/* Clean :target links */
if ($(":target").length) { /* element that visited by the URL with hash */
$(":target").attr(SCROLL_MARK, false);
}
/* set scroll mark to footnotes */
if (isFn || isFnRef) {
$target.attr(SCROLL_MARK, true);
}
if ($target.is(":focus")) { /* Checking if the target was focused */
return false;
} else {
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
$target.focus(); /* Set focus again */
}
});
}
let curOffset = $(this).offset().top;
let destOffset = target.offset().top;
const scrollUp = (destOffset < curOffset);
const topbarHeight = $("#topbar-wrapper").outerHeight();
if (scrollUp && isFnRef) {
/* Avoid the top-bar covering `fnref` when scrolling up
because `fnref` has no `%anchor`(see: module.scss) style. */
destOffset -= (topbarHeight + REM / 2);
}
$("html,body").animate({
scrollTop: destOffset
}, 800, () => {
var $target = $(target);
$target.focus();
const SCROLL_MARK = "scroll-focus";
/* clean up old scroll mark */
if ($(`[${ SCROLL_MARK }=true]`).length) {
$(`[${ SCROLL_MARK }=true]`).attr(SCROLL_MARK, false);
}
/* Clean :target links */
if ($(":target").length) { /* element that visited by the URL with hash */
$(":target").attr(SCROLL_MARK, false);
}
/* set scroll mark to footnotes */
if (isFn || isFnRef) {
$target.attr(SCROLL_MARK, true);
}
if ($target.is(":focus")) { /* Checking if the target was focused */
return false;
} else {
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
$target.focus(); /* Set focus again */
}
});
}
}

View File

@@ -1,5 +1,5 @@
/*
* Caculate the Timeago
* Calculate the Timeago
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2019 Cotes Chung
@@ -8,9 +8,11 @@
$(function() {
var toRefresh = $(".timeago").length;
const timeagoElem = $(".timeago");
var intervalId = void 0;
let toRefresh = timeagoElem.length;
let intervalId = void 0;
function timeago(iso, isLastmod) {
let now = new Date();
@@ -57,10 +59,10 @@ $(function() {
function updateTimeago() {
$(".timeago").each(function() {
if ($(this).children("i").length > 0) {
var basic = $(this).text();
var isLastmod = $(this).hasClass("lastmod");
var node = $(this).children("i");
var date = node.text(); /* ISO Date: "YYYY-MM-DDTHH:MM:SSZ" */
$(this).text();
let isLastmod = $(this).hasClass("lastmod");
let node = $(this).children("i");
let date = node.text(); /* ISO Date: "YYYY-MM-DDTHH:MM:SSZ" */
$(this).text(timeago(date, isLastmod));
$(this).append(node);
}

View File

@@ -1,10 +0,0 @@
/**
* Initial Bootstrap Tooltip.
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2019 Cotes Chung
* MIT License
*/
$(function () {
$("[data-toggle=\"tooltip\"]").tooltip();
});