How to inject scripts in Hexo?
Hexo reads scripts that are stored under a
scripts
folder 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-next
installed.
Steps
Next
allows additional injects beside those preset incustom_file_path
from next config file. Read how custom_file_path works.Create a
scripts
folder in your root folder.Add a
any_name.js
script file. In this example I will named itinject.js
.Add following codes into
inject.js
.
1 | ; |
- Do note that I am using
hexo-theme-next
in 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
.njk
file undersource/_data/
folder. If you don’t have_data
folder, 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.