-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
refactor(post/tag): render tag before content #4171
Conversation
I am considering fixing #3346 in this PR as well since that issue is also related with post rendering process. Update: @stevenjoezhang I have added a test case for #3346. It seems that issue has been fixed. |
30fb8fd
to
c21e2d0
Compare
{% pullquote warning %}
Text
{% endpullquote %}
# Title 0
{% pullquote warning %}
Text
{% endpullquote %}
{% pullquote warning %}
Text
{% endpullquote %}
# Title 1
{% pullquote warning %}
Text
{% endpullquote %}
{% pullquote warning %}Text{% endpullquote %}
# Title 2
{% pullquote warning %}Text{% endpullquote %}
{% pullquote warning %}Text{% endpullquote %}
# Title 3
{% pullquote warning %}Text{% endpullquote %} The bug reported in #4161 was reproduced. I am confirming if this is a bug (or feature) of Nunjucks |
Thank you for this great patch.
Me, too. 😉
Generally speaking, in Markdown, any block content is separated by empty line(s) each other. And custom tags in Nunjucks is not intended for Markdown block content only. That is,
should be written as folows:
If we want to add a newline character automatically for Nunjucks custom tags in Hexo, diff --git a/lib/extend/tag.js b/lib/extend/tag.js
index 31f9f2aa..883fdb5f 100644
--- a/lib/extend/tag.js
+++ b/lib/extend/tag.js
@@ -77,7 +77,7 @@ class NunjucksBlock extends NunjucksTag {
}
run(context, args, body) {
- return this._run(context, args, trimBody(body));
+ return this._run(context, args, trimBody(body)) + '\n';
}
}
@@ -111,7 +111,7 @@ class NunjucksAsyncBlock extends NunjucksBlock {
body = () => result || '';
this._run(context, args, trimBody(body)).then(result => {
- callback(err, result);
+ callback(err, result && `${result}\n`);
});
});
} I'm not sure whether this behavior is desirable in Hexo. |
- Fix swig tag inside markdown content
c21e2d0
to
1b0819a
Compare
IMHO, it is fine to include this patch inside this PR since it is related with post rendering issue fix. Update: I have applied the patch provided by @seaoak and it seems that #4171 (comment) has been fixed. |
b770791
to
1b0819a
Compare
'{' & '}' already escaped in backtick code filter
const cache = []; | ||
|
||
const escapeContent = str => `<!--${placeholder}${cache.push(str) - 1}-->`; | ||
|
||
str = str.replace(/<pre><code.*>[\s\S]*?<\/code><\/pre>/gm, escapeContent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hexo only uses tag.render
in post.js
, which is after backtick_code_filter
is executed.
In backtick_code_filter
, {
& }
are already escape, so it is fine to render with nunjucks directly.
This comment has been minimized.
This comment has been minimized.
Apply suggestions from code review by @seaoak
@seaoak Updated. |
@@ -77,7 +75,7 @@ class NunjucksBlock extends NunjucksTag { | |||
} | |||
|
|||
run(context, args, body) { | |||
return this._run(context, args, trimBody(body)); | |||
return this._run(context, args, trimBody(body)) + '\n'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why add \n
?
Look at the following test cases, which one is expected?
const str = [
'begin',
'{% test foo bar %}',
'test content',
'{% endtest %}',
'end'
].join('\n');
const result = await tag.render(str);
// there is only one `\n` before and after
result.should.eql('begin\nfoo bar test content\nend');
// Or should there be two `\n` at the end?
result.should.eql('begin\nfoo bar test content\n\nend');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extra \n
is to separete a Nunjucks tag from immediate-after content.
So I expect two \n
at the end.
Please refer the above comment #4171 (comment)
Another issue: #4087 |
For indented code blocks there is no better ways. For backtick code blocks we already have a filter. Maybe a indented code filter? Any line started with 4 spaced or |
To escape Nunjucks tags in indented code blocks properly, I try to make a patch of such a filter: With merging it, this @SukkaW 's patch resolves the issue #4087. Do you think about this new filter? |
I'd rather not to do that. Only escape Also, you can draft a PR (without actually creating it) from your branch. |
@SukkaW Yes. I think so. |
@seaoak So we should then focus on how to make swig escaping quicker. |
What does it do?
Continuation of #3543 (comment) and #4161
Currently post rendering process is something like this: #4161 (comment). But basically, hexo will render markdown first, then the tag (using nunjucks). So hexo has to remove all swig tags before markdown rendering, and restore them after markdown rendering is finished.
In this PR, I make hexo renders tag first, then markdown, so that remove and restore swig tag are not necessary any more, resulted in 6% faster in generation speed. And the post rendering related test cases are passed as well.
I wonder why Hexo has to render tag after markdown at the beginning.
How to test
Screenshots
Pull request tasks
Issue resolved: #2821