Hexo: Nunjucks enclosed by two backtick-delimited strings fails.

Created on 13 May 2017  ·  3Comments  ·  Source: hexojs/hexo

e.g.

`backtick test1`
{% blockquote %}
Blockquoe test1
{% endblockquote %}

`backtick test2`
{% blockquote %}
Blockquoe test2
{% endblockquote %}

will get the result like:
2017-05-13 11 10 05

Any idea to solve this?

bug

Most helpful comment

I found that this issue is caused by a latent bug.

The Pull Request #2461 (commit 5134cba97bed6ea83f1695bf054f08dc3f5f72ac) made the bug easier to be realized.

To fix the bug, change a regexp to be non-greedy.

https://github.com/hexojs/hexo/blob/master/lib/extend/tag.js#L65

diff --git a/lib/extend/tag.js b/lib/extend/tag.js
index 1222b01..372bd9c 100644
--- a/lib/extend/tag.js
+++ b/lib/extend/tag.js
@@ -62,7 +62,7 @@ Tag.prototype.render = function(str, options, callback) {
   var env = this.env;

   return new Promise(function(resolve, reject) {
-    str = str.replace(/(?:<pre>)?<code.*>[\s\S]*?<\/code>(?:<\/pre>)?/gm, escapeContent);
+    str = str.replace(/(?:<pre>)?<code.*?>[\s\S]*?<\/code>(?:<\/pre>)?/gm, escapeContent);
     env.renderString(str, options, function(err, result) {
       if (err) return reject(err);
       resolve(result.replace(rPlaceholder, function() {

I will add test cases, and make a pull request.

All 3 comments

This is definitely something need to be fixed.

I encountered the same bug while using {% asset_img img.jpg %}

I found that this issue is caused by a latent bug.

The Pull Request #2461 (commit 5134cba97bed6ea83f1695bf054f08dc3f5f72ac) made the bug easier to be realized.

To fix the bug, change a regexp to be non-greedy.

https://github.com/hexojs/hexo/blob/master/lib/extend/tag.js#L65

diff --git a/lib/extend/tag.js b/lib/extend/tag.js
index 1222b01..372bd9c 100644
--- a/lib/extend/tag.js
+++ b/lib/extend/tag.js
@@ -62,7 +62,7 @@ Tag.prototype.render = function(str, options, callback) {
   var env = this.env;

   return new Promise(function(resolve, reject) {
-    str = str.replace(/(?:<pre>)?<code.*>[\s\S]*?<\/code>(?:<\/pre>)?/gm, escapeContent);
+    str = str.replace(/(?:<pre>)?<code.*?>[\s\S]*?<\/code>(?:<\/pre>)?/gm, escapeContent);
     env.renderString(str, options, function(err, result) {
       if (err) return reject(err);
       resolve(result.replace(rPlaceholder, function() {

I will add test cases, and make a pull request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Helihua1992 picture Helihua1992  ·  3Comments

hgDendi picture hgDendi  ·  3Comments

sfarzoso picture sfarzoso  ·  3Comments

jasoncheng911 picture jasoncheng911  ·  3Comments

leoli-dev picture leoli-dev  ·  3Comments