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

Hexo altering Javascript files on generate #1591

Closed
ricardojmendez opened this issue Nov 15, 2015 · 11 comments · Fixed by #5118
Closed

Hexo altering Javascript files on generate #1591

ricardojmendez opened this issue Nov 15, 2015 · 11 comments · Fixed by #5118

Comments

@ricardojmendez
Copy link

On generate, Hexo is altering highlight.pack.js unnecessarily, breaking it.

To reproduce:

  • Copy the file to your source folder
  • hexo generate

You'll see that line 31, which on the original file is

return value.replace(/&/gm, '&amp;').replace(/</gm, '&lt;').replace(/>/gm, '&gt;');

Is converted to:

return value.replace(/&/gm, '&amp;').replace(//gm, '&gt;');

Installed packages:

{
  "name": "hexo-site",
  "hexo": {
    "version": "3.1.1"
  },
  "private": true,
  "dependencies": {
    "hexo": "^3.1.1",
    "hexo-generator-archive": "^0.1.1",
    "hexo-generator-category": "^0.1.1",
    "hexo-generator-feed": "^1.0.2",
    "hexo-generator-index": "^0.1.0",
    "hexo-generator-minify": "^0.1.0",
    "hexo-generator-tag": "^0.1.0",
    "hexo-renderer-ejs": "*",
    "hexo-renderer-marked": "^0.2.5",
    "hexo-renderer-stylus": "*",
    "hexo-server": "^0.1.2"
  }
}

I've yet to determine if it's one of the renderers that's causing the issue.

Attaching .js file as a .txt, since Github does not support .js attachments.

highlight.pack.js.txt

@ricardojmendez ricardojmendez changed the title Hexo altering highlight.pack.js Hexo altering Javascript files on generate Nov 15, 2015
@ricardojmendez
Copy link
Author

I found another case of an altered .js file - it's also being mangled at a <.

diff public/sketches/circles.js source/sketches/circles.js
146c146,172
<         for (num = _i = 0; _i
\ No newline at end of file
---
>         for (num = _i = 0; _i <= 100; num = ++_i) {
>           _results.push(random_circle());
>         }
>         return _results;
>       })();
>       sketch.background(240);
>       sketch.frameRate(60);
>       sketch.smooth;
>       sketch.strokeWeight(1);
>       return sketch.fill(150, 50);
>     };
>     return sketch.draw = function() {
>       var c, _i, _len;
>       sketch.fill(240, 2);
>       sketch.noStroke();
>       sketch.rect(0, 0, sketch.width, sketch.height);
>       for (_i = 0, _len = circles.length; _i < _len; _i++) {
>         c = circles[_i];
>         update_circle_position(c);
>       }
>       return draw_intersections(calculate_intersections(circles));
>     };
>   };
>
>   myp5 = new p5(mysketch);
>
> }).call(this);

Attaching as circles.js.txt.

circles.js.txt

@ricardojmendez
Copy link
Author

Basic repro:

  • Created a blank site with hexo init test-js
  • cd test-js
  • npm install
  • Copied the circles.js file mentioned above
  • hexo generate

The file will be mangled on copy. This happens even if I reduce the package.json file to

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "hexo": {
    "version": "3.1.1"
  },
  "dependencies": {
    "hexo": "^3.0.0",
    "hexo-server": "^0.1.2"
  }
}

@PhiLhoSoft
Copy link

Out of curiosity, why do you put JS files in the source folder?
I am only a beginner at Hexo, so perhaps my question is stupid.

Also I noticed a skip_render property in the config file. Perhaps you can use it to avoid such conversion?

@ricardojmendez
Copy link
Author

@PhiLhoSoft: in this case, because they're part of the content, not the template.

From the documentation:

Assets are non-post files in the source folder, such as images, CSS or JavaScript files.

Since I have some .js files that are post-specific, they fit the bill.

Thanks for the skip_render mention, I'll check it out.

@ricardojmendez
Copy link
Author

skip_render does the trick, thanks for mentioning it @PhiLhoSoft.

Keeping the issue open since there's no clear mention that Hexo would mangle JS files placed elsewhere in assets - not sure if it's a documentation issue or a bug.

@leesei leesei added the enhancement New feature or request label Dec 8, 2015
@leesei
Copy link
Member

leesei commented Dec 8, 2015

Hexo will transform every file in source/ unless excluded with skip_render.
We'll update the documentation.

@JLHwung
Copy link
Collaborator

JLHwung commented Oct 30, 2017

Removed enhancement label since skip_render is mentioned in https://hexo.io/zh-cn/docs/configuration.html#目录

@JLHwung JLHwung removed the enhancement New feature or request label Oct 30, 2017
@leesei
Copy link
Member

leesei commented Oct 30, 2017

My enhancement label was meant for the docs.
I think we should point out this somewhere in the docs:

Hexo will transform every file in source/ unless excluded with skip_render.

@JLHwung
Copy link
Collaborator

JLHwung commented Oct 30, 2017

@leesei If it is a documentation issue, I have raised hexojs/site#581. Will leave stale bot to close it.

@mohsenkhanpour
Copy link

It is also tampering with my .js files:

   function pointInRect(point, rect) {
        return point.x <= rect.right &&
            point.x >= rect.left &&
            point.y <= rect.bottom &&
            point.y >= rect.top;
    }

was changed to

function pointInRect(point, rect) {
                    return point.x <= rect.right = "" && = ""
                    point.x = "" >= rect.left &&
                        point.y <= rect.bottom = "" && = ""
                    point.y = "" >= rect.top;
                }

It took me two days to understand what was breaking my pages, finally I had to take diff between my styles and scripts to sort this out. I guess the English docs are somehow confusing.

@stevenjoezhang
Copy link
Member

I'm closing this issue. Please join the discussion here hexojs/site#581 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants