Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues caused by PJAX #1057

Merged
merged 5 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ livere_uid: # <your_uid>

# Gitalk
# Demo: https://gitalk.github.io
# For more information: https://github.com/gitalk/gitalk, https://github.com/emn178/js-md5
# For more information: https://github.com/gitalk/gitalk
gitalk:
enable: false
github_id: # GitHub repo owner
Expand Down Expand Up @@ -1084,14 +1084,12 @@ vendors:
# valine: //cdnjs.cloudflare.com/ajax/libs/valine/1.3.4/Valine.min.js
valine:

# gitalk & js-md5
# gitalk
# Example:
# gitalk_js: //cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js
# gitalk_css: //cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css
# md5: //cdn.jsdelivr.net/npm/js-md5@0/src/md5.min.js
gitalk_js:
gitalk_css:
md5:

# likely
# Example:
Expand Down
3 changes: 1 addition & 2 deletions layout/_third-party/comments/changyan.swig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
var width = window.innerWidth || document.documentElement.clientWidth;
if (width < 960) {
window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>');
}
else {
} else {
var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})});
}
})();
Expand Down
12 changes: 6 additions & 6 deletions layout/_third-party/comments/disqus.swig
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
{% endif -%}
};
function loadComments() {
if (!window.DISQUS) {
var d = document, s = d.createElement('script');
s.src = 'https://{{ theme.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', '' + +new Date());
(d.head || d.body).appendChild(s);
} else {
if (window.DISQUS) {
DISQUS.reset({
reload: true,
config: disqus_config
});
} else {
var d = document, s = d.createElement('script');
s.src = 'https://{{ theme.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', '' + +new Date());
(d.head || d.body).appendChild(s);
}
}
{%- if theme.disqus.lazyload %}
Expand Down
10 changes: 1 addition & 9 deletions layout/_third-party/comments/disqusjs.swig
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@
title: '{{ page.title | addslashes }}',
});
}

function loadDsqJS() {
var d = document, s = d.createElement('script');
s.src = '{{ disqusjs_js_url }}';
s.onload = initialDsq;
(d.head || d.body).appendChild(s);
}

window.addEventListener('load', loadDsqJS, false);
NexT.utils.getScript('{{ disqusjs_js_url }}', initialDsq, window.DisqusJS);
</script>
{%- endif %}
46 changes: 21 additions & 25 deletions layout/_third-party/comments/gitalk.swig
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
{%- if page.comments %}
{%- set gitalk_js_url = '//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js' %}
{%- if theme.vendors.gitalk_js %}
{%- set gitalk_js_url = theme.vendors.gitalk_js %}
{%- endif %}
<script src="{{ gitalk_js_url }}"></script>

{%- set gitalk_css_url = '//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css' %}
{%- if theme.vendors.gitalk_css %}
{%- set gitalk_css_url = theme.vendors.gitalk_css %}
{%- endif %}
<link rel="stylesheet" href="{{ gitalk_css_url }}">

{%- set md5_url = '//cdn.jsdelivr.net/npm/js-md5@0.7.3/src/md5.min.js' %}
{%- if theme.vendors.md5 %}
{%- set md5_url = theme.vendors.md5 %}
{%- set gitalk_js_url = '//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js' %}
{%- if theme.vendors.gitalk_js %}
{%- set gitalk_js_url = theme.vendors.gitalk_js %}
{%- endif %}
<script src="{{ md5_url }}"></script>

<script>
var gitalk = new Gitalk({
clientID: '{{ theme.gitalk.client_id }}',
clientSecret: '{{ theme.gitalk.client_secret }}',
repo: '{{ theme.gitalk.repo }}',
owner: '{{ theme.gitalk.github_id }}',
admin: ['{{ theme.gitalk.admin_user }}'],
id: md5(location.pathname),
{%- if theme.gitalk.language == '' %}
language: window.navigator.language || window.navigator.userLanguage,
{% else %}
language: '{{ theme.gitalk.language }}',
{%- endif %}
distractionFreeMode: '{{ theme.gitalk.distraction_free_mode }}'
});
gitalk.render('gitalk-container');
function initGitalk() {
var gitalk = new Gitalk({
clientID: '{{ theme.gitalk.client_id }}',
clientSecret: '{{ theme.gitalk.client_secret }}',
repo: '{{ theme.gitalk.repo }}',
owner: '{{ theme.gitalk.github_id }}',
admin: ['{{ theme.gitalk.admin_user }}'],
id: '{{ gitalk_md5(page.path) }}',
{%- if theme.gitalk.language == '' %}
language: window.navigator.language || window.navigator.userLanguage,
{% else %}
language: '{{ theme.gitalk.language }}',
{%- endif %}
distractionFreeMode: '{{ theme.gitalk.distraction_free_mode }}'
});
gitalk.render('gitalk-container');
}
NexT.utils.getScript('{{ gitalk_js_url }}', initGitalk, window.Gitalk);
</script>
{%- endif %}
6 changes: 5 additions & 1 deletion layout/_third-party/math/mathjax.swig
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@
}
});
</script>
<script src="{{ theme.math.mathjax.cdn }}"></script>
<script>
NexT.utils.getScript('{{ theme.math.mathjax.cdn }}', function() {
MathJax.Hub.Typeset();
}, window.MathJax);
</script>
12 changes: 1 addition & 11 deletions layout/_third-party/tags/mermaid.swig
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ function embedMermaid() {
});
}
if ($('body').find('pre.mermaid').length) {
if (mermaid) {
embedMermaid();
} else {
$.ajax({
type: 'GET',
url: '{{ theme.mermaid.cdn }}',
dataType: 'script',
cache: true,
success: embedMermaid
});
}
NexT.utils.getScript('{{ theme.mermaid.cdn }}', embedMermaid, window.mermaid);
}
</script>
{%- endif %}
12 changes: 1 addition & 11 deletions layout/_third-party/tags/pdf.swig
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ function embedPDF() {
});
}
if ($('body').find('div.pdf').length) {
if (PDFObject) {
embedPDF();
} else {
$.ajax({
type: 'GET',
url: '{{ theme.pdf.pdfobject.cdn }}',
dataType: 'script',
cache: true,
success: embedPDF
});
}
NexT.utils.getScript('{{ theme.pdf.pdfobject.cdn }}', embedPDF, window.PDFObject);
}
</script>
{%- endif %}
9 changes: 9 additions & 0 deletions scripts/helpers/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

'use strict';

const crypto = require('crypto');

hexo.extend.helper.register('gitalk_md5', function(path) {
var str = this.url_for(path);
str = encodeURI(str);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed due to hexojs/hexo#3708

post.path is encoded twice after Hexo 4.0.0

str.replace('index.html', '');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be str = str.replace('index.html', '');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be str = str.replace('index.html', '');

return crypto.createHash('md5').update(str).digest('hex');
});

hexo.extend.helper.register('hexo_env', type => hexo.env[type]);

hexo.extend.helper.register('next_env', type => {
Expand Down
14 changes: 14 additions & 0 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,19 @@ NexT.utils = {
? (sidebarPadding * 2) + sidebarNavHeight + sidebarOffset + this.getSidebarb2tHeight()
: (sidebarPadding * 2) + (sidebarNavHeight / 2);
return sidebarSchemePadding;
},

getScript: function(url, callback, condition) {
if (condition) {
callback();
} else {
$.ajax({
type: 'GET',
url: url,
dataType: 'script',
cache: true,
success: callback
});
}
}
};