Skip to content

Commit

Permalink
Merge branch 'master' of github.com:hexojs/hexo
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomingplus committed Mar 16, 2019
2 parents ca38c7c + 6b9acc0 commit 9ec56ac
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 10 deletions.
23 changes: 22 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
Thank you for creating a pull request to contribute to Hexo code! Before you open the request please review the following guidelines and tips to help it be more easily integrated:
<!--
Thank you for creating a pull request to contribute to Hexo code! Before you open the request please answer the following questions to help it be more easily integrated. Please check the boxes "[ ]" with "[x]" when done too.
-->

## What does it do?



## How to test

```sh
git clone -b BRANCH https://github.com/USER/hexo.git
cd hexo
npm install
npm test
```

## Screenshots



## Pull request tasks

- [ ] Add test cases for the changes.
- [ ] Passed the CI test.
4 changes: 3 additions & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ daysUntilClose: 7
exemptLabels:
- bug
- discussion
- "help wanted"
- need-verify
- need-investigation
- pending-reply
- proposal
- feature-request
- enhancement
- question
- documentation
# Label to use when marking as stale
staleLabel: stale
Expand Down
3 changes: 3 additions & 0 deletions lib/hexo/load_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function loadModuleList(ctx) {
// Ignore plugins whose name is not started with "hexo-"
if (!/^hexo-|^@[^/]+\/hexo-/.test(name)) return false;

// Ignore typescript definition file that is started with "@types/"
if (/^@types\//.test(name)) return false;

// Make sure the plugin exists
const path = ctx.resolvePlugin(name);
return fs.exists(path);
Expand Down
4 changes: 1 addition & 3 deletions lib/plugins/helper/partial.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { dirname, join } = require('path');
const chalk = require('chalk');

module.exports = ctx => function partial(name, locals, options = {}) {
if (typeof name !== 'string') throw new TypeError('name must be a string!');
Expand All @@ -14,8 +13,7 @@ module.exports = ctx => function partial(name, locals, options = {}) {
const viewLocals = { layout: false };

if (!view) {
ctx.log.warn('Partial %s does not exist. %s', chalk.magenta(name), chalk.gray(`(in ${currentView})`));
return '';
throw new Error(`Partial ${name} does not exist. (in ${currentView})`);
}

if (options.only) {
Expand Down
10 changes: 9 additions & 1 deletion lib/plugins/helper/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ function tocHelper(str, options = {}) {
let firstLevel = 0;
let lastLevel = 0;

function getId(ele) {
const id = $(ele).attr('id');
const $parent = $(ele).parent();
return id ||
($parent.length < 1 ? null :
getId($parent));
}

headings.each(function() {
const level = +this.name[1];
const id = $(this).attr('id');
const id = getId(this);
const text = escape($(this).text());

lastNumber[level - 1]++;
Expand Down
16 changes: 15 additions & 1 deletion lib/plugins/tag/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const { highlight } = require('hexo-util');

const rCaptionTitleFile = /(.*)?(?:\s+|^)(\/*\S+)/;
const rLang = /\s*lang:(\w+)/i;
const rFrom = /\s*from:(\d+)/i;
const rTo = /\s*to:(\d+)/i;

/**
* Include code tag
Expand All @@ -28,6 +30,16 @@ module.exports = ctx => function includeCodeTag(args) {
lang = _lang;
return '';
});
let from = 0;
arg = arg.replace(rFrom, (match, _from) => {
from = _from - 1;
return '';
});
let to = Number.MAX_VALUE;
arg = arg.replace(rTo, (match, _to) => {
to = _to;
return '';
});

const match = arg.match(rCaptionTitleFile);

Expand Down Expand Up @@ -59,7 +71,9 @@ module.exports = ctx => function includeCodeTag(args) {
}).then(code => {
if (!code) return;

code = stripIndent(code).trim();
code = stripIndent(code);
const lines = code.split('\n');
code = lines.slice(from, to).join('\n').trim();

if (!config.enable) {
return `<pre><code>${code}</code></pre>`;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"husky": "^1.1.3",
"istanbul": "^0.4.3",
"lint-staged": "^8.1.0",
"mocha": "^5.0.5",
"mocha": "^6.0.0",
"rewire": "^4.0.1",
"sinon": "^7.1.1"
},
Expand Down
12 changes: 10 additions & 2 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ describe('partial', () => {
const hexo = new Hexo(pathFn.join(__dirname, 'partial_test'), {silent: true});
const themeDir = pathFn.join(hexo.base_dir, 'themes', 'test');
const viewDir = pathFn.join(themeDir, 'layout') + pathFn.sep;
const viewName = 'article.swig';

const ctx = {
site: hexo.locals,
config: hexo.config,
view_dir: viewDir,
filename: pathFn.join(viewDir, 'post', 'article.swig'),
filename: pathFn.join(viewDir, 'post', viewName),
foo: 'foo',
cache: true
};
Expand Down Expand Up @@ -43,7 +44,14 @@ describe('partial', () => {
partial('widget/tag').should.eql('tag widget');

// not found
partial('foo').should.eql('');
try {
partial('foo');
} catch (err) {
err.should.have.property(
'message',
`Partial foo does not exist. (in ${pathFn.join('post', viewName)})`
);
}
});

it('locals', () => {
Expand Down
43 changes: 43 additions & 0 deletions test/scripts/tags/include_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,49 @@ describe('include_code', () => {
});
});

it('from', () => {
const fixture = [
'}'
].join('\n');
const expected = highlight(fixture, {
lang: 'js',
caption: '<span>Hello world</span><a href="/downloads/code/test.js">view raw</a>'
});

return code('Hello world lang:js from:3 test.js').then(result => {
result.should.eql(expected);
});
});

it('to', () => {
const fixture = [
'if (tired && night){',
' sleep();'
].join('\n');
const expected = highlight(fixture, {
lang: 'js',
caption: '<span>Hello world</span><a href="/downloads/code/test.js">view raw</a>'
});

return code('Hello world lang:js to:2 test.js').then(result => {
result.should.eql(expected);
});
});

it('from and to', () => {
const fixture = [
'sleep();'
].join('\n');
const expected = highlight(fixture, {
lang: 'js',
caption: '<span>Hello world</span><a href="/downloads/code/test.js">view raw</a>'
});

return code('Hello world lang:js from:2 to:2 test.js').then(result => {
result.should.eql(expected);
});
});

it('file not found', () => code('nothing').then(result => {
should.not.exist(result);
}));
Expand Down

0 comments on commit 9ec56ac

Please sign in to comment.