Skip to content

Commit

Permalink
opti: utils to js file
Browse files Browse the repository at this point in the history
  • Loading branch information
kitian616 committed Apr 7, 2018
1 parent d163367 commit 4d2b610
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 127 deletions.
6 changes: 3 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"env": {
"browser": true,
"jquery": true

"jquery": true,
"node": true
},
"rules": {
"no-console": "off",
"no-console": "error",
"semi": ["error", "always"],
"quotes": ["error", "single"],
"comma-dangle": ["error", "never"],
Expand Down
19 changes: 19 additions & 0 deletions _includes/components/comment-disqus.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{%- if site.disqus.shortname and page.key -%}
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
var disqus_config = function () {
this.page.url = '{%- include snippets/page-url.html -%}';
this.page.identifier = '{{ page.key }}';
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://{{ site.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
{%- endif -%}
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions _includes/scripts/components/chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* global Chart */
(function () {
var SOURCES = window.TEXT_VARIABLES.sources;
var PAGE = window.TEXT_VARIABLES.page;
if (PAGE.chart) {
window.Lazyload.js([SOURCES.jquery, SOURCES.chart], function() {
var $canvas = null, $this = null, _ctx = null, _text = '';
$('.language-chart').each(function(){
$this = $(this);
$canvas = $('<canvas></canvas>');
_text = $this.text();
$this.text('').append($canvas);
_ctx = $canvas.get(0).getContext('2d');
(_ctx && _text) && (new Chart(_ctx, JSON.parse(_text)) && $this.attr('data-processed', true));
});
});
}
})();
15 changes: 15 additions & 0 deletions _includes/scripts/components/google-analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* global ga */
(function() {
var ENVIRONMENT = window.TEXT_VARIABLES.jekyll.environment;
var GA_TRACKING_ID = window.TEXT_VARIABLES.site.ga_tracking_id;
if(GA_TRACKING_ID && ENVIRONMENT !== 'development') {
/* eslint-disable */
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
/* eslint-enable */
ga('create', GA_TRACKING_ID, 'auto');
ga('send', 'pageview');
}
})();
14 changes: 14 additions & 0 deletions _includes/scripts/components/mathjax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(function () {
var SOURCES = window.TEXT_VARIABLES.sources;
var PAGE = window.TEXT_VARIABLES.page;
if (PAGE.mathjax) {
var _config = { tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']]
}};
if (PAGE.mathjax_autoNumber == true) {
_config.TeX = { equationNumbers: { autoNumber: 'all' } };
}
window.MathJax = _config;
window.Lazyload.js(SOURCES.mathjax);
}
})();
11 changes: 11 additions & 0 deletions _includes/scripts/components/mermaid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* global mermaid */
(function () {
var SOURCES = window.TEXT_VARIABLES.sources;
var PAGE = window.TEXT_VARIABLES.page;
if (PAGE.mermaid) {
window.Lazyload.js(SOURCES.mermaid, function() {
mermaid.init(undefined, '.language-mermaid');
mermaid.initialize({ startOnLoad: true });
});
}
})();
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
})
);
$window.on('resize', window.throttle(function() {
console.log('toc', disabled);
if (!disabled) {
rendered || render();
calc();
Expand Down
File renamed without changes.
11 changes: 6 additions & 5 deletions _includes/scripts/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

$window.on('resize', window.throttle(function() {
tocDisabled = disabled();
console.log(tocDisabled);
toc && toc.setOptions({
disabled: tocDisabled
});
Expand Down Expand Up @@ -49,17 +48,19 @@
LEANCLOUD.app_id &&
LEANCLOUD.app_key &&
LEANCLOUD.app_class &&
ENVIRONMENT !== 'development') {
'{{ page.key }}' &&
ENVIRONMENT !== 'development' &&
ENVIRONMENT !== 'beta'
) {
window.Lazyload.js([SOURCES.jquery, SOURCES.leancloud_js_sdk], function() {
var AV = window.AV;

/* global AV */
var pageview = window.pageview(AV, {
appId: LEANCLOUD.app_id,
appKey: LEANCLOUD.app_key,
appClass: LEANCLOUD.app_class
});
var key = '{{ page.key }}';
var title = window.decodeUrl('{{ page.title | url_encode }}');
var title = '{{ page.title }}';

pageview.increase(key, title, function(view) {
$('#post-key-{{ page.key }}').text(view);
Expand Down
43 changes: 41 additions & 2 deletions _includes/scripts/variables.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<script>
(function() {
window.TEXT_VARIABLES = {
var TEXT_VARIABLES = {
jekyll: {
environment: '{{ jekyll.environment }}'
},
Expand All @@ -26,8 +26,47 @@
},
toc: {
selectors: '{{ site.toc.selectors | default: site.data.variables.default.toc.selectors }}'
},
ga_tracking_id: '{{ site.ga_tracking_id }}',
chart: '{{ site.chart }}',
mermaid: '{{ site.mermaid }}',
mathjax: '{{ site.mathjax }}',
mathjax_autoNumber: '{{ site.mathjax_autoNumber }}',
},
page_origin: {
chart: '{{ page.chart }}',
mermaid: '{{ page.mermaid }}',
mathjax: '{{ page.mathjax }}',
mathjax_autoNumber: '{{ page.mathjax_autoNumber }}',
},
};
var assignKeys = ['chart', 'mermaid', 'mathjax', 'mathjax_autoNumber'];
var toBooleanKeys = ['chart', 'mermaid', 'mathjax', 'mathjax_autoNumber'];

function assign(obj1, obj2, keys) {
var i, key;
var assignObj = {};
if (keys && keys.length > 0) {
for (i = 0; i < keys.length; i++) {
key = keys[i];
if (obj2[key] === '' || obj2 === undefined) {
assignObj[key] = obj1[key];
} else {
assignObj[key] = obj2[key];
}
}
}
};
return assignObj;
}
function toBoolean(obj, keys) {
var i, key;
for (i = 0; i < keys.length; i++) {
key = keys[i];
obj[key] = obj[key] === 'true' ? true : false
}
return obj;
}
TEXT_VARIABLES.page = toBoolean(assign(TEXT_VARIABLES.site, TEXT_VARIABLES.page_origin, assignKeys), toBooleanKeys);
window.TEXT_VARIABLES = TEXT_VARIABLES;
})();
</script>
7 changes: 7 additions & 0 deletions _includes/snippets/to-boolean.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{%- if __value == false -%}
{%- assign __value = false -%}
{%- elsif __value == true -%}
{%- assign __value = true -%}
{%- else -%}
{%- assign __value = '' -%}
{%- endif -%}
17 changes: 0 additions & 17 deletions _includes/utils/chart.html

This file was deleted.

19 changes: 0 additions & 19 deletions _includes/utils/comment-disqus.html

This file was deleted.

11 changes: 0 additions & 11 deletions _includes/utils/google-analytics.html

This file was deleted.

14 changes: 0 additions & 14 deletions _includes/utils/mathjax.html

This file was deleted.

10 changes: 0 additions & 10 deletions _includes/utils/mermaid.html

This file was deleted.

24 changes: 12 additions & 12 deletions _layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@
{%- include scripts/variables.html -%}
{%- if page.layout == "home" -%}
<script>
{%- include scripts/lib/pageview.js -%}
{%- include scripts/components/pageview.js -%}
{%- include scripts/home.js -%}
</script>
{%- elsif page.layout == "page" -%}
<script>
{%- include scripts/page.js -%}
{%- include scripts/page-post.js -%}
</script>
{%- elsif page.layout == "post" -%}
<script>
{%- include scripts/page.js -%}
{%- include scripts/lib/pageview.js -%}
{%- include scripts/lib/affix.js -%}
{%- include scripts/lib/toc.js -%}
{%- include scripts/page-post.js -%}
{%- include scripts/components/pageview.js -%}
{%- include scripts/components/affix.js -%}
{%- include scripts/components/toc.js -%}
{%- include scripts/post.js -%}
</script>
{%- elsif page.layout == "all" -%}
<script>
{%- include scripts/all.js -%}
</script>
{%- endif -%}
{%- include utils/mathjax.html -%}
{%- include utils/mermaid.html -%}
{%- include utils/chart.html -%}
{%- if jekyll.environment != "development" -%}
{%- include utils/google-analytics.html -%}
{%- endif -%}
<script>
{%- include scripts/components/mathjax.js -%}
{%- include scripts/components/mermaid.js -%}
{%- include scripts/components/chart.js -%}
{%- include scripts/components/google-analytics.js -%}
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions _layouts/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<div class="main-header"><h1>{{ __return }}</h1></div>
<div class="m-article-content" itemprop="articleBody">{{ content }}</div>
{%- if jekyll.environment != "development" and page.comment != false -%}
<section>{%- include utils/comment-disqus.html -%}</section>
<section>{%- include utils/comment-gitalk.html -%}</section>
<section>{%- include components/comment-disqus.html -%}</section>
<section>{%- include components/comment-gitalk.html -%}</section>
{%- endif -%}
</article>
</div>
4 changes: 2 additions & 2 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ <h1 itemprop="headline" itemprop="name headline">{{ __return }}</h1>
{%- endif -%}
</div>
{%- if jekyll.environment != "development" and page.comment != false -%}
<section>{%- include utils/comment-disqus.html -%}</section>
<section>{%- include utils/comment-gitalk.html -%}</section>
<section>{%- include components/comment-disqus.html -%}</section>
<section>{%- include components/comment-gitalk.html -%}</section>
{%- endif -%}
</div>
</div>
Expand Down
Loading

0 comments on commit 4d2b610

Please sign in to comment.