Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
refactor(style): load highlight.js styles from CDN instead of importi…
Browse files Browse the repository at this point in the history
…ng it to scss
  • Loading branch information
ppoffice committed Jul 24, 2018
1 parent 903749c commit 76312d4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
2 changes: 2 additions & 0 deletions _config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ logo:
article:
# Show word count and estimated reading time.
readtime: true
# Code highlight theme, please see https://highlightjs.org/static/demo/
highlight: atom-one-light

# Navigation bar menu links.
menu:
Expand Down
3 changes: 3 additions & 0 deletions layout/common/head.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/justifiedGallery/3.6.5/css/justifiedGallery.min.css">
<% } %>

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/<%= get_config('article.highlight', 'atom-one-light') %>.min.css">

<%- css('css/style') %>

<script defer src="//use.fontawesome.com/releases/v5.0.8/js/all.js"></script>
2 changes: 1 addition & 1 deletion scripts/99_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ hexo.extend.helper.register('get_config', function (configName, defaultValue = n
this.config,
getThemeConfig(this.page.lang),
!excludePage ? this.page : {}), configName);
return configName === null ? defaultValue : config;
return config === null ? defaultValue : config;
});
34 changes: 25 additions & 9 deletions scripts/99_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getMomentLocale(language) {
}

function injectMomentLocale(func) {
return function() {
return function () {
let language = getMomentLocale(getPageLanguage(this.page));
moment.locale(language);
const args = Array.prototype.slice.call(arguments).map(arg => {
Expand All @@ -52,7 +52,7 @@ hexo.extend.helper.register('is_tags', function () {
/**
* Generate html head title based on page type
*/
hexo.extend.helper.register('page_title', function() {
hexo.extend.helper.register('page_title', function () {
const page = this.page;
let title = page.title;

Expand All @@ -75,34 +75,34 @@ hexo.extend.helper.register('page_title', function() {

const getConfig = hexo.extend.helper.get('get_config').bind(this);

return [title, getConfig('title', '', true)].filter(str => typeof(str) !== 'undefined' && str.trim() !== '').join(' - ');
return [title, getConfig('title', '', true)].filter(str => typeof (str) !== 'undefined' && str.trim() !== '').join(' - ');
});

/**
* Format date to string without year.
*/
hexo.extend.helper.register('format_date', injectMomentLocale(function(date) {
hexo.extend.helper.register('format_date', injectMomentLocale(function (date) {
return moment(date).format('MMM D');
}));

/**
* Format date to string with year.
*/
hexo.extend.helper.register('format_date_full', injectMomentLocale(function(date) {
hexo.extend.helper.register('format_date_full', injectMomentLocale(function (date) {
return moment(date).format('MMM D YYYY');
}));

/**
* Get moment.js supported page locale
*/
hexo.extend.helper.register('momentjs_locale', function() {
hexo.extend.helper.register('momentjs_locale', function () {
return getMomentLocale(getPageLanguage(this.page));
});

/**
* Export moment.duration
*/
hexo.extend.helper.register('duration', injectMomentLocale(function() {
hexo.extend.helper.register('duration', injectMomentLocale(function () {
return moment.duration.apply(null, arguments);
}));

Expand Down Expand Up @@ -140,7 +140,7 @@ hexo.extend.helper.register('toc_list', (content) => {
return tocList;
}
const headings = $(levelTags.join(','));
headings.each(function() {
headings.each(function () {
const level = levelTags.indexOf(this.name);
const id = $(this).attr('id');
const text = _.escape($(this).text());
Expand All @@ -161,4 +161,20 @@ hexo.extend.helper.register('toc_list', (content) => {
tocList.push([levels.slice(0, level + 1).join('.'), id, text, level + 1]);
});
return tocList;
});
});

/**
* Add .hljs class name to the code blocks and code elements
*/
hexo.extend.filter.register('after_post_render', function (data) {
const $ = cheerio.load(data.content);
$('figure.highlight').addClass('hljs');
$('figure.highlight .code .line > span').each(function () {
const classes = $(this).attr('class').split(' ');
if (classes.length === 1) {
$(this).addClass('hljs-' + classes[0]);
}
});
data.content = $.html();
return data;
});
13 changes: 0 additions & 13 deletions source/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ $family-serif: "Ovo", "Georgia", "STZhongsong", "Microsoft YaHei", serif;
$family-mono: "Source Code Pro", monospace, "Microsoft YaHei";
$border-color: #dbdbdb;

@import "../../../../node_modules/highlight.js/styles/atom-one-light";

body, button, input, select, textarea {
font-family: $family-serif;
}
Expand Down Expand Up @@ -435,27 +433,16 @@ a.tag.article-tag {
}
}

$hljs-classes: "comment" "quote" "variable" "template" "attribute" "tag" "name" "regexp" "link" "name" "selector"
"selector" "number" "meta" "built_in" "builtin" "literal" "type" "params" "string" "symbol" "bullet"
"title" "section" "keyword" "selector" "deletion" "addition" "deletion" "addition" "emphasis" "strong";

code, pre {
font-size: 0.85rem;
font-family: $family-mono;
}

figure.highlight {
@extend .hljs;
padding: 0;
width: 100%;
margin: 0 0 1.5rem;

@each $class in $hljs-classes {
.#{$class} {
@extend .hljs-#{$class} !optional;
}
}

pre,
table tr:hover {
color: inherit;
Expand Down

1 comment on commit 76312d4

@ppoffice
Copy link
Owner

Choose a reason for hiding this comment

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

#44

Please sign in to comment.