Skip to content

Commit

Permalink
merge(#4420): from SukkaW/tag-error-source
Browse files Browse the repository at this point in the history
feat(tag): show source of the error & beautify
  • Loading branch information
SukkaW authored Jul 23, 2020
2 parents 3a56d29 + 821ef12 commit a34a7e2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { stripIndent } = require('hexo-util');
const { cyan } = require('chalk');
const { cyan, magenta, red } = require('chalk');
const { Environment } = require('nunjucks');
const Promise = require('bluebird');
const placeholder = '\uFFFC';
Expand Down Expand Up @@ -129,7 +129,7 @@ const LINES_OF_CONTEXT = 5;

const getContext = (lines, errLine, location, type) => {
const message = [
location + ' ' + type,
location + ' ' + red(type),
cyan(' ===== Context Dump ====='),
cyan(' === (line number probably different from source) ===')
];
Expand Down Expand Up @@ -158,14 +158,14 @@ const getContext = (lines, errLine, location, type) => {
* @param {string} str string input for Nunjucks
* @return {Error} New error object with embedded context
*/
const formatNunjucksError = (err, input) => {
const formatNunjucksError = (err, input, 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)', '').split('\n');
const splited = err.message.replace('(unknown path)', source ? magenta(source) : '').split('\n');

const e = new Error();
e.name = 'Nunjucks Error';
Expand Down Expand Up @@ -222,20 +222,23 @@ class Tag {
if (env.hasExtension(name)) env.removeExtension(name);
}

render(str, options, callback) {
render(str, options = {}, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
}

// Get path of post from source
const source = options.source || '';

const cache = [];

const escapeContent = str => `<!--${placeholder}${cache.push(str) - 1}-->`;

str = str.replace(/<pre><code.*?>[\s\S]*?<\/code><\/pre>/gm, escapeContent);

return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); })
.catch(err => Promise.reject(formatNunjucksError(err, str)))
.catch(err => Promise.reject(formatNunjucksError(err, str, source)))
.then(result => result.replace(rPlaceholder, (_, index) => cache[index]))
.asCallback(callback);
}
Expand Down
22 changes: 22 additions & 0 deletions test/scripts/extend/tag_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,26 @@ describe('Tag Errors', () => {
assertNunjucksError(err, 2, 'expected variable end');
}
});

it('source file path', async () => {
const source = '_posts/hello-world.md';
const tag = new Tag();

tag.register('test',
(args, content) => {},
{ ends: true });

const body = [
'{% test %}',
' {{docker ps -aq | map docker inspect -f "{{.Name}} {{.Mounts}}"}}',
'{% endtest %}'
].join('\n');

try {
// Add { source } as option
await tag.render(body, { source });
} catch (err) {
err.message.should.contains(source);
}
});
});

0 comments on commit a34a7e2

Please sign in to comment.