# Raw embed of javascript into a code block?

**URL:** <https://forum.obsidian.md/t/raw-embed-of-javascript-into-a-code-block/47950>\
**Category:** Help\
**Created:** [November 19, 2022, 12:57pm UTC](https://forum.obsidian.md/t/raw-embed-of-javascript-into-a-code-block/47950 "2022-11-19T12:57:53Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 20, 2022, 12:37am UTC](https://forum.obsidian.md/t/raw-embed-of-javascript-into-a-code-block/47950/3 "2022-11-20T00:37:22Z")

</div>

Where there is a will, there is often a way, @obsequious ! 😃 And as stated before, I do know VS Code, but I kind of want to stay within Obsidian when doing Obsidian stuff.

With the combination of the plugins [Buttons](https://github.com/shabegom/buttons) and [Templater](https://github.com/shabegom/buttons), I found a way to do this.

First, make a template like the following, which I named `srcInputFile`:

```nohighlight
<%* 
const fs = require("fs")
const basePath = app.vault.adapter.basePath
const inputFile = tp.frontmatter.input_file
const language = inputFile.split('.').pop()
// console.log("\n", basePath, inputFile, language)
const text = fs.readFileSync(basePath + inputFile)
tR += `Source: <code>${inputFile}</code>\n ~~~~${language}\n${text}\n~~~~ \n`
%>

```

Then make a note with a button, like my `Source viewer.md` file:

````nohighlight
---
input_file: /_jsTemplater/foo.js
---
```button
name Reload sourcecode
type line(11) template
replace [11, 10000]
action srcInputFile
```
%% Line 10 %%
%% Line 11 %%
%% Line 12 %%

````

Change the filed `input_file` to some other file accessible in your vault, and watch the magic unfold.

Some caveats with this script. Firstly if you add or change the header make sure also to change the line number where the new content goes, and where it removes stuff from, aka `11` to something else. To get the correct number, I suggest adding some more numbers on the original file, increase the number, and see where it removes the lines. Better safe, then sorry.

This does indeed depend on reading your local file system, which can have it pros and cons. So just, be warned.

There is no automatic update on this script, and that’s partly by design, and partly by choice. You do need to hit the `Reload sourcecode` button to get the latest version of your script.

Lastly, Templater does have some issues running if its user script files has errors, so then this script might suffer under those issues as well. It’s got nothing to do with this script, it’s just a dependency issue within Templater.

Example image displaying a simple javascript file:

 ![image](https://forum.obsidian.md/uploads/default/original/3X/f/b/fb58eff3c6422b0ff4acbff540dae8c18e62a54e.png)

---

_[View the full topic](https://forum.obsidian.md/t/raw-embed-of-javascript-into-a-code-block/47950)._
