Source: How to Add JavaScript to Your Obsidian Publish Site | by Hani Sarji | Medium
You can add JavaScript to your Obsidian Publish site in one file (publish.js). To add this file to your Obsidian Publish site, follow the steps below.
Step 1: Set up a custom domain
Set up a custom domain for Obsidian Publish. See https://help.obsidian.md/Obsidian+Publish/Set+up+a+custom+domain You cannot skip this step as publish.js only works if you’re using a custom domain.
Step 2: Create the **publish.js** file
Create the publish.js file in the root of your Obsidian vault using a text editor.
- Create theÂ
publish.js file using a text editor other than Obsidian. Creating this file from Obsidian results in a file with anÂmd extension (publish.js.md), which you don’t want. - Using the other text editor (like TextEdit on a Mac) open the Obsidian vault that you’re using for your Obsidian Publish files. Create a file with the file nameÂ
publish.js in the root of the vault, which means don’t place this file in any other folder in that vault.
Step 3: Add code
With the open publish.js file, copy/paste your JavaScript code. Don’t use the <script> tag in this file, since you’re already in a JavaScript file and not an HTML file.
To edit publish.js (and publish.css), I typically use Textastic (which is a text editor) on iOS and Mac, but I’ve edited these files using TextEdit and VS Code as well.
Step 4: Upload
Go back to Obsidian and upload the publish.jsfile by using Obsidian to publish it, as you would publish any other .md file.
Step 5: Repeat steps 3–4, as needed
To “sync” future edits to publish.js to your site, publish the changes again as you would for other files in Obsidian Publish.