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

内置于html文件中的js代码段,为何要使用"代替单引号? #284

Closed
3 tasks done
kuretru opened this issue Jun 2, 2021 · 8 comments · Fixed by #294
Closed
3 tasks done

内置于html文件中的js代码段,为何要使用"代替单引号? #284

kuretru opened this issue Jun 2, 2021 · 8 comments · Fixed by #294
Labels
Bug Something isn't working Solved
Milestone

Comments

@kuretru
Copy link

kuretru commented Jun 2, 2021

如题,在这个6b23d42930658f5b8bf20a2c3a32f6a516fa8976commit后,开始发生此情况,作为一个强迫症患者,在hexo g以后,我还会使用gulp-beautif来格式化渲染后的html、css、js文件,但是其无法很好的识别转义后的标点符号,因此格式化后无法正常显示网页。另外转义后会导致代码的可读性下降,不利于开发维护。

Please follow this Issue template to provide relevant information, such as source code repository, website URL and screenshots, which will help us investigate.
请按照此 Issue 模版提供相关信息,例如源码仓库、网站链接和屏幕截图,这将有助于我们进行调查。

Issue Checklist


Expected behavior

在hexo-theme-next@v8.3.0及以前,内置的js代码会被正常的渲染,例如:

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {
      "hostname": "localhost",
      "root": "/",
      ......
    }
</script>

Actual behavior

在hexo-theme-next@v8.4.0及以后,内置的js代码会被转义,例如:

<script class="next-config" data-name="main" type="application/json">{&quot;hostname&quot;:&quot;localhost&quot;,&quot;root&quot;:&quot;&#x2F;&quot;,......}</script>
@kuretru kuretru added the Bug Something isn't working label Jun 2, 2021
@PaperStrike
Copy link
Member

PaperStrike commented Jun 2, 2021

这段 8.3.0 及之前的 hexo-configurations,8.4.0 及之后的 next-config,只是用来向前端传递用户配置的,Hexo 生成时序列化、前端反序列化。为了防止用户配置里出现元素标签这类东西,最好要加一个转义。8.4.0 之前直接以 script 形式传递,加不了转义。8.4.0 及之后以 json 形式传递,转义就可行了。为了方便就用了 Hexo 提供的转义,Hexo 提供的转义比较广。

可读性下降、不利于开发维护这个不至于,Hexo 所生成网页源码就不是设计成给人读的开发时几乎用不到。

@stevenjoezhang 你觉得这个转义要不要去掉还是化简一下呢。 👀 <script> 在 HTML 解析时走的 text insertion mode 似乎只要转义各种 end tag 就好了

@njzjz
Copy link
Member

njzjz commented Jun 2, 2021

补充一句,这不是js,script的属性被设置为type="application/json"。js的属性是text/javascript。因此浏览器不会将其识别为js。

@kuretru
Copy link
Author

kuretru commented Jun 2, 2021

感谢各位对hexo-theme-next的贡献及回复

@stevenjoezhang
Copy link
Member

@njzjz
Copy link
Member

njzjz commented Jun 3, 2021

@stevenjoezhang
Copy link
Member

gulp-beautify 已经超过两年没有更新,建议使用 Hexo 的插件替代。支持代码压缩和格式化的 Hexo 插件可以在这里查找: https://hexo.io/plugins/

@PaperStrike
Copy link
Member

PaperStrike commented Jun 13, 2021

我觉得应该使得格式化 application/json 可行。问题在于现有的转义上,转义后文本格式已然不是 json 了。晚些时候我提个 PR 删掉这个转义好了,单独转义 end tag 想了想挺麻烦,不好整(更新:想多了)。

另外我不太清楚规范或者浏览器是怎么判断 end tag 的,有可能对于合规的 JSON 根本不需要这个转义?(更新:不行)

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. It is possible issue was solved or at least outdated. Feel free to open new for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Something isn't working Solved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants