Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tag): show source of the error & beautify #4420

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});
});