Modules allows you to load JavaScript and TypeScript files in your vault and npm packages, with major improvements over existing solutions. It can compliment or replace any other plugins using JavaScript like Dataview dv.view, CustomJS, Templater user scripts, etc.
To get you intrigued, here is an example that produces ---------Hello, world!----------:
module.js.md:
- Some optional *Markdown* here describing the following code block: A `hello world` function.
~~~JavaScript
// Enable "Enable external links" in plugin settings first.
const { pad } = await require.import("https://esm.sh/lodash-es");
export function helloWorld(dv) {
const text = pad("Hello, world!", 32, "-");
dv.paragraph(text);
}
~~~
The major improvements over existing solutions are:
No arbitrary export restrictions like exporting only one class or function per file.
Load scripts from the Internet. You can load a npm package using await require.import("https://esm.sh/(package name here)") or other CDNs. Most packages should work.
Load TypeScript.
Load Markdown files with JavaScript or TypeScript code blocks like in the example. Content outside code blocks are ignored.
Supports ./relative/paths.js, [whatever](Markdown%20links.js), and [[wikilinks.js]], which works exactly the same way Obsidian resolves them.
I installed dataview to try your plugin. I did 2 files as you told. but I get the error
Evaluation Error: Error: ./module.js.md
at D (plugin:modules:30154:2473)
at Function.import (plugin:modules:30154:4796)
at async eval (eval at (plugin:dataview), :1:77)
at async DataviewJSRenderer.render (plugin:dataview:18436:13)
what I did wrong
ok the created must be named as module.js I called it module.js.md…
then I get a new error
Evaluation Error: Error: https://esm.sh/lodash-es
at D (plugin:modules:30154:2473)
at Function.import (plugin:modules:30154:4796)
at async blob:app://obsidian.md/c92af95e-47eb-4f25-a4e7-7d01d107b6ca:4:17
It looks like import ... from "https://example.com" and await import("https://example.com") can handle external links (if CORS allows). Obviously they cannot handle Obsidian files though.
3b251be: Add cwd detection for dv.view in Dataview.
ef032bf: Add cwd detection for Templater user scripts.
fdbee2f: Add cwd detection for canvases.
2fd4dc4: Change the type and meaning of null and undefined for working directory. This allows overriding the working directory with no working directory in CommonOptions.
5fbad90: Always change the cwd context in preview and editor.
Patch Changes
92b9215: Fix context detection for Templater templates.
004ed2e: Fix weird behaviors if the vault has more than 1000 files. Fixes GH#4.