This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,86 @@ | ||
(function () { | ||
'use strict'; | ||
'use strict'; | ||
|
||
var style = document.createElement('style'); | ||
var style = document.createElement('style'); | ||
document.body.appendChild(style); | ||
|
||
var base = $('link[type*="rss+xml"]') | ||
.attr('href').replace(/\/(feed|rss)\/?$/i, '') || ''; | ||
|
||
$.base = base; | ||
|
||
var defReplace = { | ||
rand: function () { | ||
return Math.floor(Math.random() * 10000000); | ||
}, | ||
base: $.base | ||
}; | ||
/** | ||
* The default replacement rules, including: | ||
* - rand: random number generator | ||
* - base: blog base url | ||
*/ | ||
var defReplace = { | ||
rand: function () { | ||
return Math.floor(Math.random() * 10000000); | ||
}, | ||
base: $.base | ||
}; | ||
|
||
/** | ||
* Default configurations | ||
*/ | ||
var _default = { | ||
tocName: 'Contents', | ||
tocComments: 'Comments' | ||
tocName: 'Contents', | ||
tocComments: 'Comments' | ||
}; | ||
|
||
/** | ||
* Theme extension methods | ||
*/ | ||
$.j = { | ||
/** | ||
* A simple string template(variable replacement) function. | ||
* | ||
* @param {string} str Source string | ||
* @param {object} replacement Variable replacement rules | ||
*/ | ||
m_replaceVariable: function (str, replacement) { | ||
var rules = $.extend({}, defReplace, replacement); | ||
return str.replace(/%(?:\{\s*(\w+)\s*\}|(\w+))/g, function (z, a, b) { | ||
var variable = a || b; | ||
var r = rules[variable]; | ||
if (!r) return z; | ||
var rules = $.extend({}, defReplace, replacement); | ||
return str.replace(/%(?:\{\s*(\w+)\s*\}|(\w+))/g, function (z, a, b) { | ||
var variable = a || b; | ||
var r = rules[variable]; | ||
if (!r) { | ||
return z; | ||
} | ||
|
||
if ($.isFunction(r)) { | ||
return r(variable); | ||
} | ||
if ($.isFunction(r)) { | ||
return r(variable); | ||
} | ||
|
||
return r; | ||
}); | ||
}, | ||
return r; | ||
}); | ||
}, | ||
|
||
/** | ||
* The configuration. | ||
*/ | ||
m_config: $.extend({}, _default, $('#theme-config').data()), | ||
|
||
/** | ||
* Inject style text to an existing style tag in current page. | ||
* @param {string} styleText the cssText to be injected. | ||
*/ | ||
m_addStyle: function (styleText) { | ||
style.textContent += styleText; | ||
}, | ||
|
||
/** | ||
* Decode emoji unicodes to emoji string. | ||
* @param {string} [args] string of encoded emoji | ||
* | ||
* encode: encodeURIComponent(str).replace(/%/g, '') | ||
*/ | ||
m_emoji: function m_emoji () { | ||
return decodeURIComponent(Array.prototype.reduce.call(arguments, function (acc, item) { | ||
return acc + item.replace(/../g, function(z) { | ||
return '%' + z; | ||
}); | ||
}, '')); | ||
} | ||
}; | ||
})(); |