This plugin allows to embed code files from Obsidian vault or remote file (eg., GitHub). It works better with live preview feature of Obsidian.
The plugin include multiple language by default (c,cpp,java,python,go,ruby,javascript,js,typescript,ts,shell,sh,bash). You can include any needed language to the comma separated list.
First you need to activate the plugin from Community Plugins. Then you can embed the code as follows:
```embed-<some-language>
PATH: "vault://<some-path-to-code-file>" or "http[s]://<some-path-to-remote-file>" or "<relative-path>"
LINES: "<some-line-number>,<other-number>,...,<some-range>"
TITLE: "<some-title>"
```Examples:
```embed-cpp
PATH: "vault://Code/main.cpp"
LINES: "2,9,30-40,100-122,150"
TITLE: "Some title"
``````embed-cpp
PATH: "main.cpp"
LINES: "30-40"
TITLE: "Some title"
```Or with parent directory navigation:
```embed-python
PATH: "../utils/helper.py"
LINES: "10-20"
TITLE: "Helper Functions"
``````embed-cpp
PATH: "https://raw.githubusercontent.com/almariah/embed-code-file/main/main.ts"
LINES: "30-40"
TITLE: "Some title"
```where PATH, LINES and TITLE properties are set as YAML key-value pairs:
-
The
PATHcan be:- Relative path: A path relative to the current note (e.g.,
"main.cpp","./src/file.js","../utils/helper.py") - Absolute vault path: Use
vault://prefix for absolute paths from vault root (e.g.,"vault://Code/main.cpp") - Remote URL: Use
http://orhttps://for remote files (e.g., from GitHub - make sure to usehttps://raw.githubusercontent.com/...)
- Relative path: A path relative to the current note (e.g.,
-
The
LINESwill include only the specified lines of the code file. Every set of included lines either range or explicit line will append dots (...) to included line in a newline. If you want to get rid of dots, minimize the number of sets by using one range as much as you can. -
If
TITLEis not set, then the title of the code block will bePATHvalue.
You can use also TITLE with normal code block (without embed-), but make sure that the title value is set with double quotes:
```cpp TITLE: "Some title"
// some code
...
```Using live preview feature will enhance the embedding experience.



