Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ppwwyyxx authored Aug 26, 2022
2 parents ff0cfca + a2fc8c0 commit 218b5f9
Show file tree
Hide file tree
Showing 25 changed files with 275 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Please take extra precaution not to attach any secret environment variables (lik
Please check followings before submitting a new issue.

- [ ] I have already confirmed other issue template and they are not different my want
- [ ] Not a question, feature-request, bug
- Please submit to [discussion](https://github.com/hexojs/hexo/discussions) if your issue is a question.

## Information

Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/commenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ name: Commenter

on: [pull_request_target]

permissions:
contents: read

jobs:
commenter:
permissions:
pull-requests: write # for marocchino/sticky-pull-request-comment to create or update PR comment
runs-on: ubuntu-latest
steps:
- name: Comment PR
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Linter

on: [push, pull_request]

permissions:
contents: read

jobs:
linter:
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ on:
branches:
- master

permissions:
contents: read

jobs:
update_release_draft:
permissions:
contents: write # for release-drafter/release-drafter to create a github release
pull-requests: write # for release-drafter/release-drafter to add label to PR
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/tester.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Tester

on: [push, pull_request]

permissions:
contents: read

jobs:
tester:
runs-on: ${{ matrix.os }}
Expand All @@ -23,6 +26,9 @@ jobs:
env:
CI: true
coverage:
permissions:
checks: write # for coverallsapp/github-action to create new checks
contents: read # for actions/checkout to fetch code
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand Down
8 changes: 6 additions & 2 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ const getContext = (lines, errLine, location, type) => {
* @return {Error} New error object with embedded context
*/
const formatNunjucksError = (err, input, source = '') => {
err.message = err.message.replace('(unknown path)', source ? magenta(source) : '');

const match = err.message.match(/Line (\d+), Column \d+/);
if (!match) return err;
const errLine = parseInt(match[1], 10);
if (isNaN(errLine)) return err;

// trim useless info from Nunjucks Error
const splited = err.message.replace('(unknown path)', source ? magenta(source) : '').split('\n');
const splited = err.message.split('\n');

const e = new Error();
e.name = 'Nunjucks Error';
Expand Down Expand Up @@ -243,7 +245,9 @@ class Tag {
options,
cb
);
}).catch(err => Promise.reject(formatNunjucksError(err, str, source)))
}).catch(err => {
return Promise.reject(formatNunjucksError(err, str, source));
})
.asCallback(callback);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Hexo extends EventEmitter {
const dbPath = args.output || base;

if (/^(init|new|g|publish|s|deploy|render|migrate)/.test(this.env.cmd)) {
this.log.d(`Writing database to ${dbPath}/db.json`);
this.log.d(`Writing database to ${join(dbPath, 'db.json')}`);
}

this.database = new Database({
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = ctx => {
const is = require('./is');
helper.register('is_current', is.current);
helper.register('is_home', is.home);
helper.register('is_home_first_page', is.home_first_page);
helper.register('is_post', is.post);
helper.register('is_page', is.page);
helper.register('is_archive', is.archive);
Expand Down
5 changes: 5 additions & 0 deletions lib/plugins/helper/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function isHomeHelper() {
return Boolean(this.page.__index);
}

function isHomeFirstPageHelper() {
return Boolean(this.page.__index) && this.page.current === 1;
}

function isPostHelper() {
return Boolean(this.page.__post);
}
Expand Down Expand Up @@ -79,6 +83,7 @@ function isTagHelper(tag) {

exports.current = isCurrentHelper;
exports.home = isHomeHelper;
exports.home_first_page = isHomeFirstPageHelper;
exports.post = isPostHelper;
exports.page = isPageHelper;
exports.archive = isArchiveHelper;
Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/helper/mail_to.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { htmlTag } = require('hexo-util');
const qs = require('querystring');
const { default: moize } = require('moize');

function mailToHelper(path, text, options = {}) {
Expand All @@ -28,7 +27,7 @@ function mailToHelper(path, text, options = {}) {
}
});

const querystring = qs.stringify(data);
const querystring = new URLSearchParams(data).toString();
if (querystring) attrs.href += `?${querystring}`;

return htmlTag('a', attrs, text);
Expand Down
16 changes: 2 additions & 14 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const { parse, resolve } = require('url');
const { isMoment, isDate } = require('moment');
const { encodeURL, prettyUrls, htmlTag, stripHTML, escapeHTML } = require('hexo-util');
const { default: moize } = require('moize');
Expand Down Expand Up @@ -56,7 +55,6 @@ const og = (name, content, escape) => {
};

function openGraphHelper(options = {}) {

const { config, page } = this;
const { content } = page;
let images = options.image || options.images || page.photos || [];
Expand Down Expand Up @@ -117,15 +115,7 @@ function openGraphHelper(options = {}) {
result += og('og:locale', localeToTerritory(language), false);
}

images = images.map(path => {
if (!parse(path).host) {
// resolve `path`'s absolute path relative to current page's url
// `path` can be both absolute (starts with `/`) or relative.
return resolve(url || config.url, path);
}

return path;
});
images = images.map(path => new URL(path, url || config.url).toString());

images.forEach(path => {
result += og('og:image', path, false);
Expand Down Expand Up @@ -161,9 +151,7 @@ function openGraphHelper(options = {}) {

if (options.twitter_image) {
let twitter_image = options.twitter_image;
if (!parse(twitter_image).host) {
twitter_image = resolve(url || config.url, twitter_image);
}
twitter_image = new URL(twitter_image, url || config.url);
result += meta('twitter:image', twitter_image, false);
} else if (images.length) {
result += meta('twitter:image', images[0], false);
Expand Down
38 changes: 29 additions & 9 deletions lib/plugins/helper/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ const createLink = (options, ctx) => {

const createPageTag = (options, ctx) => {
const link = createLink(options, ctx);
const { current, escape, transform } = options;
const {
current,
escape,
transform,
page_class: pageClass,
current_class: currentClass
} = options;

return i => {
if (i === current) {
return htmlTag('span', { class: 'page-number current' }, transform ? transform(i) : i, escape);
return htmlTag('span', { class: pageClass + ' ' + currentClass }, transform ? transform(i) : i, escape);
}
return htmlTag('a', { class: 'page-number', href: link(i) }, transform ? transform(i) : i, escape);
return htmlTag('a', { class: pageClass, href: link(i) }, transform ? transform(i) : i, escape);
};
};

Expand All @@ -36,14 +42,15 @@ const pagenasionPartShow = (tags, options, ctx) => {
total,
space,
end_size: endSize,
mid_size: midSize
mid_size: midSize,
space_class: spaceClass
} = options;

const leftEnd = Math.min(endSize, current - 1);
const rightEnd = Math.max(total - endSize + 1, current + 1);
const leftMid = Math.max(leftEnd + 1, current - midSize);
const rightMid = Math.min(rightEnd - 1, current + midSize);
const spaceHtml = htmlTag('span', { class: 'space' }, space, false);
const spaceHtml = htmlTag('span', { class: spaceClass }, space, false);

const pageTag = createPageTag(options, ctx);

Expand Down Expand Up @@ -93,7 +100,13 @@ function paginatorHelper(options = {}) {
next_text: 'Next',
prev_text: 'Prev',
prev_next: true,
escape: true
escape: true,
page_class: 'page-number',
current_class: 'current',
space_class: 'space',
prev_class: 'extend prev',
next_class: 'extend next',
force_prev_next: false
}, options);

const {
Expand All @@ -102,7 +115,10 @@ function paginatorHelper(options = {}) {
prev_text: prevText,
next_text: nextText,
prev_next: prevNext,
escape
escape,
prev_class: prevClass,
next_class: nextClass,
force_prev_next: forcePrevNext
} = options;

if (!current) return '';
Expand All @@ -113,7 +129,9 @@ function paginatorHelper(options = {}) {

// Display the link to the previous page
if (prevNext && current > 1) {
tags.push(htmlTag('a', { class: 'extend prev', rel: 'prev', href: link(current - 1)}, prevText, escape));
tags.push(htmlTag('a', { class: prevClass, rel: 'prev', href: link(current - 1)}, prevText, escape));
} else if (forcePrevNext) {
tags.push(htmlTag('span', { class: prevClass, rel: 'prev' }, prevText, escape));
}

if (options.show_all) {
Expand All @@ -124,7 +142,9 @@ function paginatorHelper(options = {}) {

// Display the link to the next page
if (prevNext && current < total) {
tags.push(htmlTag('a', { class: 'extend next', rel: 'next', href: link(current + 1) }, nextText, escape));
tags.push(htmlTag('a', { class: nextClass, rel: 'next', href: link(current + 1) }, nextText, escape));
} else if (forcePrevNext) {
tags.push(htmlTag('span', { class: nextClass, rel: 'next' }, nextText, escape));
}

return tags.join('');
Expand Down
24 changes: 18 additions & 6 deletions lib/plugins/helper/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ function tocHelper(str, options = {}) {
min_depth: 1,
max_depth: 6,
class: 'toc',
class_item: '',
class_link: '',
class_text: '',
class_child: '',
class_number: '',
class_level: '',
list_number: true
}, options);

Expand All @@ -15,6 +21,12 @@ function tocHelper(str, options = {}) {
if (!data.length) return '';

const className = escapeHTML(options.class);
const itemClassName = escapeHTML(options.class_item || options.class + '-item');
const linkClassName = escapeHTML(options.class_link || options.class + '-link');
const textClassName = escapeHTML(options.class_text || options.class + '-text');
const childClassName = escapeHTML(options.class_child || options.class + '-child');
const numberClassName = escapeHTML(options.class_number || options.class + '-number');
const levelClassName = escapeHTML(options.class_level || options.class + '-level');
const listNumber = options.list_number;

let result = `<ol class="${className}">`;
Expand Down Expand Up @@ -42,23 +54,23 @@ function tocHelper(str, options = {}) {
}

if (level > lastLevel) {
result += `<ol class="${className}-child">`;
result += `<ol class="${childClassName}">`;
} else {
result += '</li>';
}
} else {
firstLevel = level;
}

result += `<li class="${className}-item ${className}-level-${level}">`;
result += `<li class="${itemClassName} ${levelClassName}-${level}">`;
if (href) {
result += `<a class="${className}-link" href="${href}">`;
result += `<a class="${linkClassName}" href="${href}">`;
} else {
result += `<a class="${className}-link">`;
result += `<a class="${linkClassName}">`;
}

if (listNumber && !el.unnumbered) {
result += `<span class="${className}-number">`;
result += `<span class="${numberClassName}">`;

for (let i = firstLevel - 1; i < level; i++) {
result += `${lastNumber[i]}.`;
Expand All @@ -67,7 +79,7 @@ function tocHelper(str, options = {}) {
result += '</span> ';
}

result += `<span class="${className}-text">${text}</span></a>`;
result += `<span class="${textClassName}">${text}</span></a>`;

lastLevel = level;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { exists, readFile } = require('hexo-fs');
const { basename, extname, join } = require('path');
const { basename, extname, join, posix } = require('path');

// Lazy require highlight.js & prismjs
let highlight, prismHighlight;
Expand Down Expand Up @@ -55,7 +55,7 @@ module.exports = ctx => function includeCodeTag(args) {

// If the title is not defined, use file name instead
const title = match[1] || basename(path);
const caption = `<span>${title}</span><a href="${ctx.config.root}${codeDir}${path}">view raw</a>`;
const caption = `<span>${title}</span><a href="${posix.join(ctx.config.root, codeDir, path)}">view raw</a>`;

const hljsCfg = ctx.config.highlight || {};
const prismjsCfg = ctx.config.prismjs || {};
Expand Down
9 changes: 6 additions & 3 deletions lib/plugins/tag/post_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const { postFindOneFactory } = require('./');
*/
module.exports = ctx => {
return function postLinkTag(args) {
const error = `<a href="#">Post not found: ${args.join(' ') || 'Invalid post_link'}</a>`;
const slug = args.shift();
if (!slug) return error;
if (!slug) {
throw new Error(`Post not found: "${slug}" doesn't exist for {% post_link %}`);
}

let escape = args[args.length - 1];
if (escape === 'true' || escape === 'false') {
Expand All @@ -24,7 +25,9 @@ module.exports = ctx => {
}

const post = postFindOneFactory(ctx)({ slug });
if (!post) return error;
if (!post) {
throw new Error(`Post not found: post_link ${slug}.`);
}

let title = args.length ? args.join(' ') : post.title;
const attrTitle = escapeHTML(title);
Expand Down
Loading

0 comments on commit 218b5f9

Please sign in to comment.