Skip to content

Commit

Permalink
⚡ 重构引入 script 和 css 的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zkqiang committed Feb 4, 2022
1 parent 060f703 commit b5413a7
Show file tree
Hide file tree
Showing 28 changed files with 368 additions and 292 deletions.
4 changes: 2 additions & 2 deletions layout/_partials/comments/twikoo.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
path: '<%= theme.twikoo.path %>',
onCommentLoaded: function() {
var imgSelector = '#twikoo .tk-content img:not(.tk-owo-emotion)';
Fluid.plugins.initImageCaption(imgSelector);
Fluid.plugins.initFancyBox(imgSelector);
Fluid.plugins.imageCaption(imgSelector);
Fluid.plugins.fancyBox(imgSelector);
}
}
)
Expand Down
4 changes: 2 additions & 2 deletions layout/_partials/comments/valine.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
new Valine(options);
Fluid.utils.waitElementVisible('#valine .vcontent', () => {
var imgSelector = '#valine .vcontent img:not(.vemoji)';
Fluid.plugins.initImageCaption(imgSelector);
Fluid.plugins.initFancyBox(imgSelector);
Fluid.plugins.imageCaption(imgSelector);
Fluid.plugins.fancyBox(imgSelector);
})
});
});
Expand Down
4 changes: 2 additions & 2 deletions layout/_partials/comments/waline.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
new Waline(options);
Fluid.utils.waitElementVisible('#waline .vcontent', () => {
var imgSelector = '#waline .vcontent img:not(.vemoji)';
Fluid.plugins.initImageCaption(imgSelector);
Fluid.plugins.initFancyBox(imgSelector);
Fluid.plugins.imageCaption(imgSelector);
Fluid.plugins.fancyBox(imgSelector);
})
});
});
Expand Down
35 changes: 4 additions & 31 deletions layout/_partials/css.ejs
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
<%- css_ex(theme.static_prefix.bootstrap, 'css/bootstrap.min.css') %>

<% if (is_post() || is_page()) { %>
<%- css_ex(theme.static_prefix.github_markdown, 'github-markdown.min.css') %>
<%- css_ex(theme.static_prefix.hint, 'hint.min.css') %>
<% if (theme.code.highlight.enable) { %>
<% var cssName %>
<% if (theme.code.highlight.lib === 'highlightjs') { %>
<% cssName = theme.code.highlight.highlightjs.style.toLowerCase().replace(/([^0-9])\s([^0-9])/g, '$1-$2').replace(/\s/g, '') %>
<%- css_ex(theme.static_prefix.highlightjs, `/styles/${ cssName }.min.css`) %>
<% } else if (theme.code.highlight.lib === 'prismjs') { %>
<% var style = theme.code.highlight.prismjs.style %>
<% if (/\.css$/.test(style)) { %>
<%- css(style) %>
<% } else { %>
<% if (!style || style === 'default') { %>
<% cssName = '' %>
<% } else { %>
<% cssName = style.toLowerCase().replace(/[\s\-]/g, '') %>
<% if (cssName === 'prism') { cssName = '' } %>
<% if (cssName === 'tomorrownight') { cssName = 'tomorrow' } %>
<% } %>
<%- css_ex(theme.static_prefix.prismjs, `/themes/${ cssName ? 'prism-' + cssName : 'prism' }.min.css`) %>
<% } %>
<% if (theme.code.highlight.line_number) { %>
<%- css_ex(theme.static_prefix.prismjs, '/plugins/line-numbers/prism-line-numbers.min.css') %>
<% } %>
<% } %>
<% } %>
<% if (theme.post.image_zoom.enable && page.image_zoom !== false) { %>
<%- css_ex(theme.static_prefix.fancybox, 'jquery.fancybox.min.css') %>
<% if (page.css_snippets) { %>
<% for (var idx = 0; idx < page.css_snippets.length; idx++) { %>
<%- page.css_snippets[idx] %>
<% } %>
<% page.css_snippets = [] %>
<% } %>

<!-- 主题依赖的图标库,不要自行修改 -->
Expand Down
12 changes: 12 additions & 0 deletions layout/_partials/footer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@
<% } %>
</div>
</footer>

<% if ((theme.footer.statistics.enable && theme.footer.statistics.source === 'busuanzi')
|| (page.meta !== false && theme.post.meta.views.enable && theme.post.meta.views.source === 'busuanzi')) { %>
<% import_js(theme.static_prefix.busuanzi, 'busuanzi.pure.mini.js', 'defer') %>
<% } %>
<% if (theme.web_analytics.enable && theme.web_analytics.leancloud.app_id && theme.web_analytics.leancloud.app_key) { %>
<% import_js(theme.static_prefix.internal_js, 'leancloud.js', 'defer') %>
<% } else if (theme.web_analytics.leancloud.app_id && theme.web_analytics.leancloud.app_key &&
((theme.footer.statistics.enable && theme.footer.statistics.source === 'leancloud')
|| (page.meta !== false && theme.post.meta.views.enable && theme.post.meta.views.source === 'leancloud'))) { %>
<% import_js(theme.static_prefix.internal_js, 'leancloud.js', 'defer') %>
<% } %>
1 change: 1 addition & 0 deletions layout/_partials/header/navigation.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
&nbsp;<i class="iconfont icon-search"></i>&nbsp;
</a>
</li>
<% import_js(theme.static_prefix.internal_js, 'local-search.js') %>
<% } %>
<% if(theme.dark_mode && theme.dark_mode.enable) { %>
<li class="nav-item" id="color-toggle-btn">
Expand Down
24 changes: 24 additions & 0 deletions layout/_partials/plugins/anchorjs.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%
import_script(`
<script>
Fluid.utils.createScript('${ url_join(theme.static_prefix.anchor, 'anchor.min.js') }', function() {
window.anchors.options = {
placement: CONFIG.anchorjs.placement,
visible : CONFIG.anchorjs.visible
};
if (CONFIG.anchorjs.icon) {
window.anchors.options.icon = CONFIG.anchorjs.icon;
}
var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
var res = [];
for (var item of el) {
res.push('.markdown-body > ' + item.trim());
}
if (CONFIG.anchorjs.placement === 'left') {
window.anchors.options.class = 'anchorjs-link-left';
}
window.anchors.add(res.join(', '));
});
</script>
`)
%>
76 changes: 76 additions & 0 deletions layout/_partials/plugins/code-widget.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<%
import_script(`<script>
(function() {
var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
var enableCopy = CONFIG.copy_btn;
if (!enableLang && !enableCopy) {
return;
}
function getBgClass(ele) {
if (ele.length === 0) {
return 'code-widget-light';
}
var rgbArr = ele.css('background-color').replace(/rgba*\\(/, '').replace(')', '').split(',');
var color = (0.213 * rgbArr[0]) + (0.715 * rgbArr[1]) + (0.072 * rgbArr[2]) > 255 / 2;
return color ? 'code-widget-light' : 'code-widget-dark';
}
var copyTmpl = '';
copyTmpl += '<div class="code-widget">';
copyTmpl += 'LANG';
copyTmpl += '</div>';
jQuery('.markdown-body pre').each(function() {
var $pre = jQuery(this);
if ($pre.find('code.mermaid').length > 0) {
return;
}
if ($pre.find('span.line').length > 0) {
return;
}
var lang = '';
if (enableLang) {
lang = CONFIG.code_language.default;
if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
lang = $pre[0].children[0].classList[1];
} else if ($pre[0].getAttribute('data-language')) {
lang = $pre[0].getAttribute('data-language');
} else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
lang = $pre[0].children[0].classList[1];
$pre.parent().addClass('code-wrapper');
} else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
$pre.wrap('<div class="code-wrapper"></div>');
}
lang = lang.toUpperCase().replace(/(LIVECODESERVER|NONE)/, CONFIG.code_language.default);
}
$pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
getBgClass($pre) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));
if (enableCopy) {
Fluid.utils.createScript('${url_join(theme.static_prefix.clipboard, 'clipboard.min.js')}', function() {
var clipboard = new window.ClipboardJS('.copy-btn', {
target: function(trigger) {
var nodes = trigger.parentNode.childNodes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].tagName === 'CODE') {
return nodes[i];
}
}
}
});
clipboard.on('success', function(e) {
e.clearSelection();
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
setTimeout(function() {
e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
}, 2000);
});
});
}
});
})();
</script>
`)
%>
11 changes: 11 additions & 0 deletions layout/_partials/plugins/fancybox.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%
import_css(theme.static_prefix.fancybox, 'jquery.fancybox.min.css')
import_script(`
<script>
Fluid.utils.createScript('${url_join(theme.static_prefix.fancybox, 'jquery.fancybox.min.js')}', function() {
Fluid.plugins.fancyBox();
});
</script>
`)
%>
39 changes: 39 additions & 0 deletions layout/_partials/plugins/highlight.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<%
var cssName
if (theme.code.highlight.lib === 'highlightjs') {
cssName = theme.code.highlight.highlightjs.style.toLowerCase().replace(/([^0-9])\s([^0-9])/g, '$1-$2').replace(/\s/g, '')
if (cssName === 'github-gist' && require_version(theme.static_prefix.highlightjs, '11.0.0')) {
cssName = 'github'
}
import_css(theme.static_prefix.highlightjs, `/styles/${cssName}.min.css`)
} else if (theme.code.highlight.lib === 'prismjs') {
var style = theme.code.highlight.prismjs.style
if (/\.css$/.test(style)) {
import_css(theme.static_prefix.prismjs, style)
} else {
if (!style || style === 'default') {
cssName = ''
} else {
cssName = style.toLowerCase().replace(/[\s\-]/g, '')
if (cssName === 'prism') {
cssName = ''
}
if (cssName === 'tomorrownight') {
cssName = 'tomorrow'
}
}
import_css(theme.static_prefix.prismjs, `/themes/${cssName ? 'prism-' + cssName : 'prism'}.min.css`)
}
if (!theme.code.highlight.prismjs.preprocess) {
import_js(theme.static_prefix.prismjs, 'components/prism-core.min.js')
import_js(theme.static_prefix.prismjs, 'plugins/autoloader/prism-autoloader.min.js')
}
if (theme.code.highlight.line_number) {
import_css(theme.static_prefix.prismjs, '/plugins/line-numbers/prism-line-numbers.min.css')
import_js(theme.static_prefix.prismjs, '/plugins/line-numbers/prism-line-numbers.min.js')
}
}
%>
46 changes: 46 additions & 0 deletions layout/_partials/plugins/math.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<% if(theme.post.math.engine === 'mathjax') { %>
<%
var lazy = theme.lazyload.enable && require_version(theme.static_prefix.mathjax, '3.2.0')
import_script(`
<script>
MathJax = {
tex : {
inlineMath: [['$', '$'], ['\\(', '\\)']]
},
loader : {
${ lazy ? 'load: \[\'ui/lazy\'\]' : '' }
},
options: {
renderActions: {
findScript : [10, doc => {
document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
const display = !!node.type.match(/; *mode=display/);
const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
const text = document.createTextNode('');
node.parentNode.replaceChild(text, node);
math.start = { node: text, delim: '', n: 0 };
math.end = { node: text, delim: '', n: 0 };
doc.math.push(math);
});
}, '', false],
insertedScript: [200, () => {
document.querySelectorAll('mjx-container').forEach(node => {
let target = node.parentNode;
if (target.nodeName.toLowerCase() === 'li') {
target.parentNode.classList.add('has-jax');
}
});
}, '', false]
}
}
};
</script>
`)
import_js(theme.static_prefix.mathjax, 'tex-svg.js')
%>
<% } else if (theme.post.math.engine === 'katex') { %>
<% import_css(theme.static_prefix.katex, 'katex.min.css') %>
<% } %>
File renamed without changes.
54 changes: 0 additions & 54 deletions layout/_partials/post/math.ejs

This file was deleted.

30 changes: 30 additions & 0 deletions layout/_partials/post/toc.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,33 @@
<p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;<%- __('post.toc') %></p>
<div class="toc-body" id="toc-body"></div>
</div>

<%
import_script(`
<script>
Fluid.utils.createScript('${url_join(theme.static_prefix.tocbot, 'tocbot.min.js')}', function() {
var toc = jQuery('#toc');
if (toc.length === 0 || !window.tocbot) { return; }
var boardCtn = jQuery('#board-ctn');
var boardTop = boardCtn.offset().top;
window.tocbot.init({
tocSelector : '#toc-body',
contentSelector : '.markdown-body',
headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
linkClass : 'tocbot-link',
activeLinkClass : 'tocbot-active-link',
listClass : 'tocbot-list',
isCollapsedClass: 'tocbot-is-collapsed',
collapsibleClass: 'tocbot-is-collapsible',
collapseDepth : CONFIG.toc.collapseDepth || 0,
scrollSmooth : true,
headingsOffset : -boardTop
});
if (toc.find('.toc-list-item').length > 0) {
toc.css('visibility', 'visible');
}
});
</script>
`)
%>
Loading

0 comments on commit b5413a7

Please sign in to comment.