Skip to content

Commit

Permalink
feat(layout): code block style fix and add folding
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoffice committed Aug 13, 2019
1 parent d150e55 commit ede5d0b
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 38 deletions.
2 changes: 1 addition & 1 deletion includes/common/ConfigValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function compareVersion(ver1, ver2) {
}

function isBreakingChange(base, ver) {
return base.major !== ver.major;
return base.major !== ver.major || base.minor !== ver.minor;
}


Expand Down
20 changes: 17 additions & 3 deletions includes/specs/article.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@ module.exports = {
[type]: 'object',
[doc]: 'Article display settings',
highlight: {
[type]: 'string',
[doc]: 'Code highlight theme\nhttps://github.com/highlightjs/highlight.js/tree/master/src/styles',
[defaultValue]: 'atom-one-light'
[type]: 'object',
[doc]: 'Code highlight settings',
theme: {
[type]: 'string',
[doc]: 'Code highlight themes\nhttps://github.com/highlightjs/highlight.js/tree/master/src/styles',
[defaultValue]: 'atom-one-light'
},
clipboard: {
[type]: 'boolean',
[doc]: 'Show code copying button',
[defaultValue]: true
},
fold: {
[type]: 'string',
[doc]: 'Default folding status of the code blocks. Can be "", "folded", "unfolded"',
[defaultValue]: 'unfolded'
}
},
thumbnail: {
[type]: 'boolean',
Expand Down
5 changes: 0 additions & 5 deletions includes/specs/plugins.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ module.exports = {
[doc]: 'Show a loading progress bar at top of the page',
[defaultValue]: true
},
clipboard: {
[type]: 'boolean',
[doc]: 'Show the copy button in the highlighted code area',
[defaultValue]: true
},
busuanzi: {
[type]: 'boolean',
[doc]: 'BuSuanZi site/page view counter\nhttps://busuanzi.ibruce.info',
Expand Down
2 changes: 1 addition & 1 deletion layout/common/head.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<%- _css(cdn('bulma', '0.7.2', 'css/bulma.css')) %>
<%- _css(iconcdn()) %>
<%- _css(fontcdn('Ubuntu:400,600|Source+Code+Pro')) %>
<%- _css(cdn('highlight.js', '9.12.0', 'styles/' + get_config('article.highlight') + '.css')) %>
<%- _css(cdn('highlight.js', '9.12.0', 'styles/' + get_config('article.highlight.theme') + '.css')) %>

<% if (has_config('plugins')) { %>
<% for (let plugin in get_config('plugins')) { %>
Expand Down
15 changes: 15 additions & 0 deletions layout/common/scripts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<%- _js(cdn('moment', '2.22.2', 'min/moment-with-locales.min.js')) %>
<script>moment.locale("<%= get_config('language', 'en') %>");</script>

<script>
var IcarusThemeSettings = {
article: {
highlight: {
clipboard: <%= get_config('article.highlight.clipboard', true) %>,
fold: '<%= get_config('article.highlight.fold', true) %>'
}
}
};
</script>

<% if (get_config('article.highlight.clipboard')) { %>
<%- _js(cdn('clipboard', '2.0.4', 'dist/clipboard.min.js'), true) %>
<% } %>

<% if (has_config('plugins')) { %>
<% for (let plugin in get_config('plugins')) { %>
<%- partial('plugin/' + plugin, { head: false, plugin: get_config('plugins')[plugin] }) %>
Expand Down
6 changes: 0 additions & 6 deletions layout/plugin/clipboard.ejs

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "hexo-theme-icarus",
"version": "2.3.0",
"version": "2.6.0",
"private": true
}
30 changes: 19 additions & 11 deletions source/css/style.styl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ figure.highlight
width: 100%
position: relative
margin: 1em 0 1em !important
&.folded
.highlight-body
display: none

pre,
table tr:hover
Expand All @@ -381,15 +384,30 @@ figure.highlight
text-align: left
font-style: normal
font-size: .8em
&:after
clear: both
content: " "
display: table

span
font-weight: 500
font-family: family-mono

.fold
a
float: right
color: #9a9a9a

a
float: right
margin-left: 0.5em

.fold
margin-right: 0.5em
cursor: pointer

.highlight-body
overflow: auto

.gutter
text-align: right

Expand All @@ -408,16 +426,6 @@ figure.highlight
min-width: inherit
border-radius: inherit

.copy
display: none
position: absolute
bottom: 0
right: 0
color: white
background: rgba(0, 0, 0, 0.5)
&:hover .copy
display: block

/* ---------------------------------
* Overflow Table
* --------------------------------- */
Expand Down
10 changes: 0 additions & 10 deletions source/js/clipboard.js

This file was deleted.

45 changes: 45 additions & 0 deletions source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,51 @@
adjustNavbar();
$(window).resize(adjustNavbar);

$('figure.highlight table').wrap('<div class="highlight-body">');
if (typeof (IcarusThemeSettings) !== 'undefined' &&
typeof (IcarusThemeSettings.article) !== 'undefined' &&
typeof (IcarusThemeSettings.article.highlight) !== 'undefined') {
if (typeof (ClipboardJS) !== 'undefined' && IcarusThemeSettings.article.highlight.clipboard) {
$('figure.highlight').each(function () {
var id = 'code-' + Date.now() + (Math.random() * 1000 | 0);
var button = '<a href="javascript:;" class="copy" title="Copy" data-clipboard-target="#' + id + ' .code"><i class="fas fa-copy"></i></a>';
$(this).attr('id', id);
if ($(this).find('figcaption').length) {
$(this).find('figcaption').prepend(button);
} else {
$(this).prepend('<figcaption>' + button + '</figcaption>');
}
});
new ClipboardJS('.highlight .copy');
}
var fold = IcarusThemeSettings.article.highlight.fold;
if (fold.trim()) {
var button = '<span class="fold">' + (fold === 'unfolded' ? '<i class="fas fa-angle-down"></i>' : '<i class="fas fa-angle-right"></i>') + '</span>';
$('figure.highlight').each(function () {
if ($(this).find('figcaption').length) {
$(this).find('figcaption').prepend(button);
} else {
$(this).prepend('<figcaption>' + button + '</figcaption>');
}
});

function toggleFold(codeBlock, isFolded) {
var $toggle = $(codeBlock).find('.fold i');
!isFolded ? $(codeBlock).removeClass('folded') : $(codeBlock).addClass('folded');
!isFolded ? $toggle.removeClass('fa-angle-right') : $toggle.removeClass('fa-angle-down');
!isFolded ? $toggle.addClass('fa-angle-down') : $toggle.addClass('fa-angle-right');
}

$('figure.highlight').each(function () {
toggleFold(this, fold === 'folded');
});
$('figure.highlight figcaption .fold').click(function () {
var $code = $(this).closest('figure.highlight');
toggleFold($code.eq(0), !$code.hasClass('folded'));
});
}
}

var $toc = $('#toc');
if ($toc.length > 0) {
var $mask = $('<div>');
Expand Down

2 comments on commit ede5d0b

@ppoffice
Copy link
Owner

Choose a reason for hiding this comment

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

@ppoffice
Copy link
Owner

Choose a reason for hiding this comment

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

Please sign in to comment.