Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Oct 31, 2019
2 parents cded126 + ccfc8e2 commit 1be1351
Show file tree
Hide file tree
Showing 69 changed files with 1,149 additions and 15,699 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: node_js
node_js:
- "8"
- "9"
- "10"
- "11"
- "12"
git:
depth: 5
quiet: true
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
> 现代 · 强大 · 简洁
<p align="center">
<img alt="Version" src="https://img.shields.io/badge/Version-1.3.3-5755d9.svg?style=flat-square"/>
<img alt="Version" src="https://img.shields.io/github/v/release/sukkaw/hexo-theme-suka?color=%235755d9&include_prereleases&label=version&style=flat-square">
<a href="https://skk.moe" target="_blank"><img alt="Author" src="https://img.shields.io/badge/Author-Sukka-b68469.svg?style=flat-square"/></a>
<a href="https://hexo.io" target="_blank"><img alt="Hexo" src="https://img.shields.io/badge/hexo-3.7+-0e83cd.svg?style=flat-square"/></a>
<a href="https://nodejs.org/" target="_blank"><img alt="node.js" src="https://img.shields.io/badge/node.js-10.0%2B-43853d.svg?style=flat-square"/></a>
<a href="https://nodejs.org/" target="_blank"><img alt="node.js" src="https://img.shields.io/badge/node.js-8.6%2B-43853d.svg?style=flat-square"/></a>
<img alt="GitHub repo size" src="https://img.shields.io/github/repo-size/sukkaw/hexo-theme-suka?style=flat-square">
<br>
<a href="https://travis-ci.org/SukkaW/hexo-theme-suka"><img alt="Build Status" src="https://img.shields.io/travis/SukkaW/hexo-theme-suka.svg?style=flat-square"/></a>
<a href="https://www.codacy.com/app/SukkaW/hexo-theme-suka" target="_blank"><img alt="Codacy Badge" src="https://img.shields.io/codacy/grade/273f45610882413aad88396e06bfa6ec.svg?style=flat-square"></a>
Expand Down
6 changes: 2 additions & 4 deletions _config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,11 @@ vendors:
# You can check current the Suka Version Code at the end of theme config.
suka:
style_css:
lazyload_img:
local_search_js:
hanabi_browser_js:
highlight_theme:
gallery_css:
gallery_js:
# You can fill in any suka built in highlight.js theme you want here, and it will override the prism theme config before.
lazyload_img:
# Spectre.css
# https://picturepan2.github.io/spectre/
# Version: 0.5.3
Expand Down Expand Up @@ -425,4 +423,4 @@ old_verison:
- 1.1.1
- 1.2.0
- 1.3.0
- 1.3.2
- 1.3.2
85 changes: 85 additions & 0 deletions includes/filter/prism.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* hexo-prism-plugin
* author: ele828
* license: MIT
* patched by SukkaW for hexo-theme-suka
*/

'use strict';

module.exports = function (hexo) {
// Plugin settings
const config = hexo.config.suka_theme.prism;

if (config.enable !== true) {
return;
}

const Prism = require('node-prismjs');

const map = {
'&#39;': '\'',
'&amp;': '&',
'&gt;': '>',
'&lt;': '<',
'&quot;': '"'
};

const regex = /<pre><code class="(.*)?">([\s\S]*?)<\/code><\/pre>/igm;
const captionRegex = /<p><code>(?![\s\S]*<code)(.*?)\s(.*?)\n([\s\S]*)<\/code><\/p>/igm;

const line_number = config.line_number || true;

/**
* Unescape from Marked escape
* @param {String} str
* @return {String}
*/
function unescape(str) {
if (!str || str === null) return '';
const re = new RegExp('(' + Object.keys(map).join('|') + ')', 'g');
return String(str).replace(re, (match) => map[match]);
}

/**
* Code transform for prism plugin.
* @param {Object} data
* @return {Object}
*/
function PrismPlugin(data) {
// Patch for caption support
if (captionRegex.test(data.content)) {
// Attempt to parse the code
data.content = data.content.replace(captionRegex, (origin, lang, caption, code) => {
if (!lang || !caption || !code) return origin;
return `<figcaption>${caption}</figcaption><pre><code class="${lang}">${code}</code></pre>`;
});
}

data.content = data.content.replace(regex, (origin, lang, code) => {
const lineNumbers = line_number ? 'line-numbers' : '';
const startTag = `<pre class="${lineNumbers} highlight language-${lang}"><code class="language-${lang}">`;
const endTag = `</code></pre>`;
code = unescape(code);
let parsedCode = '';
if (Prism.languages[lang]) {
parsedCode = Prism.highlight(code, Prism.languages[lang]);
} else {
parsedCode = code;
}
if (line_number) {
const match = parsedCode.match(/\n(?!$)/g);
const linesNum = match ? match.length + 1 : 1;
let lines = new Array(linesNum + 1);
lines = lines.join('<span></span>');
const startLine = '<span aria-hidden="true" class="line-numbers-rows">';
const endLine = '</span>';
parsedCode += startLine + lines + endLine;
}
return startTag + parsedCode + endTag;
});

return data;
}

hexo.extend.filter.register('after_post_render', PrismPlugin);
};
82 changes: 82 additions & 0 deletions includes/generator/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module.exports = function (hexo) {
if (hexo.config.suka_theme.search.enable !== true) {
return;
}

const pathFn = require('path');
const { stripHTML } = require('hexo-util');

let config = hexo.config.suka_theme.search;

// Set default search path
if (!config.path) config.path = 'search.json';

function searchGenerator(locals = {}) {
const url_for = hexo.extend.helper.get('url_for').bind(this);

const parse = (item) => {
let _item = {};
if (item.title) _item.title = item.title;
if (item.date) _item.date = item.date;
if (item.path) _item.url = url_for(item.path);
if (item.tags && item.tags.length > 0) {
_item.tags = [];
item.tags.forEach((tag) => {
_item.tags.push(tag.name);
});
}
if (item.categories && item.categories.length > 0) {
_item.categories = [];
item.categories.forEach((cate) => {
_item.categories.push(cate.name);
});
}
if (item._content) {
_item.content = stripHTML(item.content.trim().replace(/<pre(.*?)\<\/pre\>/gs, ''))
.replace(/\n/g, ' ').replace(/\s+/g, ' ')
.replace(new RegExp('(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]', 'g'), '');
}
return _item;
};

const searchfield = config.field;

let posts,
pages;

if (searchfield) {
if (searchfield === 'post') {
posts = locals.posts.sort('-date');
} else if (searchfield === 'page') {
pages = locals.pages;
} else {
posts = locals.posts.sort('-date');
pages = locals.pages;
}
} else {
posts = locals.posts.sort('-date');
}

let res = [];

if (posts) {
posts.each((post) => {
res.push(parse(post));
});
}
if (pages) {
pages.each((page) => {
res.push(parse(page));
});
}

return {
path: config.path,
data: JSON.stringify(res)
};
}

if (pathFn.extname(config.path) === '.json') {
hexo.extend.generator.register('json', searchGenerator);
}
};
42 changes: 42 additions & 0 deletions includes/helpers/favicon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { htmlTag } = require('hexo-util');

module.exports = function (hexo) {
hexo.extend.helper.register('favicon', function () {
const url_for = hexo.extend.helper.get('url_for').bind(this);
const { favicon } = this.theme.head;

let html = '';

if (favicon.ico) {
html += htmlTag('link', { rel: 'icon', type: 'image/ico', href: url_for(favicon.ico) });
}
if (favicon.apple_touch_icon) {
html += htmlTag('link', { rel: 'apple-touch-icon', sizes: '180x180', href: url_for(favicon.apple_touch_icon) });
}
if (favicon.large) {
html += htmlTag('link', { rel: 'icon', typt: 'image/png', sizes: '192x192', href: url_for(favicon.large) });
}
if (favicon.medium) {
html += htmlTag('link', { rel: 'icon', typt: 'image/png', sizes: '32x32', href: url_for(favicon.medium) });
}
if (favicon.small) {
html += htmlTag('link', { rel: 'icon', typt: 'image/png', sizes: '16x16', href: url_for(favicon.small) });
}

return html;
});

hexo.extend.helper.register('site_logo', function () {
const full_url_for = hexo.extend.helper.get('full_url_for').bind(this);
const { favicon } = this.theme.head;
const getFavicon = (type) => full_url_for(favicon[type]);

if (favicon.large) return getFavicon('large');
if (favicon.apple_touch_icon) return getFavicon('apple_touch_icon');
if (favicon.medium) return getFavicon('medium');
if (favicon.small) return getFavicon('small');
if (favicon.ico) return getFavicon('ico');

return 'https://theme-suka.skk.moe/demo/img/suka-favicon.png';
});
};
72 changes: 72 additions & 0 deletions includes/helpers/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Generate title string based on page type
* @example
* <%- page_title(page) %>
* <%- page_descr(page) %>
* <%- page_tags(page) %>
*/

const { stripHTML } = require('hexo-util');

module.exports = function (hexo) {
hexo.extend.helper.register('page_title', function (page = null) {
page = (page === null) ? this.page : page;

let title = page.title;

if (this.is_archive()) {
title = this.__('archive');
if (this.is_month()) {
title += `: ${page.year}/${page.month}`;
} else if (this.is_year()) {
title += `: ${page.year}`;
}
} else if (this.is_category()) {
title = `${this.__('category')}: ${page.category}`;
} else if (this.is_tag()) {
title = `${this.__('tag')}: ${page.tag}`;
}

return [title, hexo.config.title].filter((str) => typeof (str) !== 'undefined' && str.trim() !== '').join(' | ');
});

hexo.extend.helper.register('page_descr', function (page = null) {
page = (page === null) ? this.page : page;

let description = page.description || page.excerpt || page.content || hexo.config.description ;

description = stripHTML(description).trim() // Remove prefixing/trailing spaces
.replace(/^s*/, '').replace(/s*$/, '')
.substring(0, 200)
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/\n/g, ' '); // Replace new lines by spaces

return [description, hexo.config.author, hexo.config.title].filter((str) => typeof (str) !== 'undefined' && str.trim() !== '').join(' - ');
});

hexo.extend.helper.register('page_tags', function (page = null) {
page = (page === null) ? this.page : page;
const { config } = this;

let page_tags = page.keywords || page.tags,
site_tags = config.keywords || this.theme.head.keywords;

const parse = (tags) => {
let result = [];
if (tags) {
if (typeof tags === 'string') {
result.push(tags);
} else if (tags.length) {
result.push(tags.map(tag => tag.name ? tag.name : tag).filter(tags => !!tags).join(', '));
}
}
return result;
};

return [parse(page_tags), parse(site_tags)].filter(tags => tags.length && tags.length !== 0).join(', ');
});
};
25 changes: 25 additions & 0 deletions includes/helpers/qrcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const qrImage = require('qr-image');

module.exports = function (hexo) {
hexo.extend.helper.register('qrcode', (url, option) => {
const qrConfig = Object.assign(
{
size: 6,
margin: 0
},
option || {}
);

const qrUrl = url.replace('index.html', '');

const buffer = qrImage.imageSync(
qrUrl,
{
type: 'png',
size: qrConfig.size,
margin: qrConfig.margin
}
);
return `data:image/png;base64,${buffer.toString('base64')}`;
});
};
Loading

0 comments on commit 1be1351

Please sign in to comment.