How to inject scripts in Hexo?
Hexo reads scripts that are stored under a
scriptsfolder in your root folder.
The Reason why I adding this tutorial:
Note:
I am not sure, but seems like njk files fromsource/_data/only support first<script>tag.
Ofcourse you can have only one<script>tag in the njk file, and from the just use one script to run multiple scripts. But for me it is not an elegant way.
Prerequisite:
hexo-theme-nextinstalled.
Steps
Nextallows additional injects beside those preset incustom_file_pathfrom next config file. Read how custom_file_path works.Create a
scriptsfolder in your root folder.Add a
any_name.jsscript file. In this example I will named itinject.js.Add following codes into
inject.js.
1 | ; |
- Do note that I am using
hexo-theme-nextin my project. So the following code only works for next theme. If you are working with other theme, you should be able to directly inject script into hexo.
- Add a new
.njkfile undersource/_data/folder. If you don’t have_datafolder, just create one.
1 | <script type="text/javascript" src="any_script.js"></script> |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.