Everything the edaha command does, you can do from JavaScript or TypeScript: build a site when and how you like, read its config file, write embed plugins, and run the article editor.
The packages are ES modules with TypeScript types, and need Node.js 22 or later:
| Package | What it exports |
|---|---|
@lvl99dotcom/edaha.md |
buildSite, loadSiteConfig and the other config helpers, the embed API, runCli |
@lvl99dotcom/edaha-editor |
startEditor and createEditorServer |
@lvl99dotcom/edaha-plugin-… |
One embed plugin each, as their default export |
Build
buildSite builds a site, as edaha build does:
How a build works
A build reads the config file and the site’s articles, turns each article’s Markdown into HTML, and renders it with the site’s templates into the output folder:
loadSiteConfigfinds and reads the config file, checks its settings and loads its embed plugins. The site folder is the config file’s folder.- Every folder in the articles folder is an article type, with its settings from the config file (or the defaults, with a warning).
- Each article’s
article.mdis read: its header settings are checked, its embed blocks are set aside for the pages that show them, and its Markdown becomes HTML. Drafts are checked but not built. - Published articles are collected into lists: one for each type, and one for each tag and author within a type, as the types’
generatesettings say. - Each article page, list page and RSS feed is rendered with its template, then the embed blocks it shows are rendered by their plugins (sharing each embed’s state on the page) and the HTML its embeds need is added. Pages are written to
outputDirwith the articles’ files and the static files. IfpublishDiris set, everything is then copied there.
Templates are found in the site’s templates folder first, then in the default theme. A type’s own template (e.g. templates/news/single.njk) comes before the article ones, and an article can name another with its template setting. Every page template extends a layout:
A site’s template can build on the theme’s version of itself with {% extends "@theme/article/single.njk" %}. See Templating for the template data and filters.
buildSite options
| Option | Type | Description | Default |
|---|---|---|---|
siteDir |
String | The folder to look for a config file in, before the nearest package.json’s folder. |
The current folder |
configFile |
String | The config file to use instead of looking for one. The site folder is its folder. | The first config file found |
siteUrl |
String | The website’s absolute URL, for feeds. Its path (e.g. /edaha.md) starts every URL in the pages. |
siteUrl in the config file, then http://localhost with a warning |
themeDir |
String | The theme’s folder (with templates/ and static/), which the site’s own templates and static files override, instead of the config file’s theme. |
The config file’s theme, then edaha.md’s default theme (THEME_DIR) |
plugins |
EmbedPlugin[] | Embed plugins added after the config file’s plugins. One with the same key as the config file’s is left out. |
None |
logger |
{ warn } |
Where warnings go, e.g. about articles with problems. | console |
fetch |
Function | Fetches data for embeds that need it, e.g. Bandcamp’s. | The global fetch |
buildSite resolves with:
| Property | Description |
|---|---|
outputDir |
The absolute output folder. |
publishDir |
The absolute folder the files were copied into, if publishDir is set. |
articles |
The built articles’ template data (published and unlisted). |
files |
Every file written, relative to the output folder. |
It rejects with an error that names the problem (and the config file or article) when the site can’t be built, e.g. a config setting is wrong, or a static file has the same path as an article’s file. Problems that don’t stop the build go to logger.warn.
Reading the config file
loadSiteConfig finds and reads a site’s config file as the build does, e.g. to read its settings in a script of your own:
It takes siteDir, configFile and logger, like buildSite. Besides the config file, its folder and its settings, it gives themeDir: the folder of the theme named in the config file’s theme, or undefined for the default theme. The config helpers it uses are exported too: findConfigFile (which file would be used), readConfigFile (its raw settings) and parseConfig (check settings and fill in defaults; give it plugin objects, as it doesn’t load packages).
Themes
@lvl99dotcom/edaha.md/theme has helpers for themes, e.g. for tools that manage them:
| Export | Description |
|---|---|
THEME_DIR |
The default theme’s folder. |
THEME_FILE |
The name of a theme’s settings file: edahatheme.yml. |
resolveTheme(theme, siteDir) |
The folder of a theme setting: a folder path (relative to siteDir) or an installed package’s name. Rejects if it isn’t found. |
isThemePath(theme) |
Whether a theme setting is a folder path (it starts with . or is absolute) rather than a package name. |
readThemeInfo(themeDir) |
A theme’s name, description, version and authors, from its package.json if it has one (then isPackage is true), else its edahatheme.yml, else just its folder’s name. Rejects, naming the file, if the file is invalid. |
parseThemeInfo(data, source) |
Checks the settings read from an edahatheme.yml. |
packageAuthor(value) |
Reads a package.json author, e.g. "Jane Doe <jane@example.com> (https://example.com)". |
Embed plugins
The embed API (EmbedPlugin, EmbedResolver, EMBED_PAGE_PARTS, embedBlock, splitEmbed, cachedFetch, isEmbedPlugin, loadPlugins and validationErrors) is exported too. EmbedResolver renders embeds page by page: replaceBlocks puts placeholders for a page’s blocks in its Markdown, and renderPage renders them in the page’s HTML, with the HTML its embeds need in place of EMBED_PAGE_PARTS (the template data for {% embedHead %} and {% embedFoot %}). The fields’ defaultValue, showWhen and hideWhen are applied by resolveFields, from @lvl99dotcom/edaha.md/generator/fields. See Embeds for how to write a plugin.
The command line
runCli runs the edaha command with its arguments, and resolves with its exit code. It takes the folder, environment and output to use, so it can run inside another tool:
Editor
startEditor, from the editor package, starts the article editor on this computer, as edaha editor does:
It only listens on 127.0.0.1: never deploy it, as it reads and writes the site’s files.
startEditor options
startEditor (and createEditorServer) take:
| Option | Type | Description | Default |
|---|---|---|---|
siteDir |
String | The folder to look for a config file in, before the nearest package.json’s folder. |
The current folder |
configFile |
String | The config file to use instead of looking for one. | The first config file found |
port |
Number | The port to listen on. | 5180 |
themeDir |
String | The theme’s folder, which the site’s own templates and static files override, instead of the config file’s theme. |
The config file’s theme, then edaha.md’s default theme |
plugins |
EmbedPlugin[] | Embed plugins added after the config file’s plugins. |
None |
hideTitle |
Boolean | Hide the “edaha.md” title, e.g. in an app that shows the editor in its own window. Home still goes back to the welcome prompt. | false |
hideThemeSwitcher |
Boolean | Hide the light/dark theme switcher, showing the device’s light or dark theme instead. | false |
fetch |
Function | Fetches data for embeds that need it. | The global fetch |
clientDir |
String | The folder with the editor page’s built files. | The editor package’s dist/client |
logger |
{ warn } |
Where the warning about a folder with several config files goes. | None |
startEditor resolves with the editor’s url, a fetch function that answers a Request without the network (see Showing the editor in your own app) and a close function, once it is listening. It rejects if the config file can’t be read, or the port is in use.
Serving it yourself
createEditorServer gives the editor as a Hono app instead, without listening, e.g. to serve it with your own server or test it:
Its API only accepts changes from the editor page’s own address (http://127.0.0.1:<port>), so serve it there.
Showing the editor in your own app
@lvl99dotcom/edaha-editor/client gives the editor’s page as a web component, <edaha-editor>, for an app’s own page (e.g. a desktop app). It’s defined when imported. It needs an editor server for the site (see startEditor), which it reaches with an EditorApi:
The stylesheet styles the editor and has edaha.md’s design tokens as CSS variables on :root (e.g. --colour-text, --colour-link, --border-radius), in light and dark, so the app’s page can use them too. Inside a shadow root, add the stylesheet to the shadow root as well, as :root rules don’t apply there (the variables still do).
EditorApi option |
Type | Description | Default |
|---|---|---|---|
baseUrl |
String | The editor server’s address, e.g. http://127.0.0.1:5180. Requests, articles’ files and images, and preview pages use it. |
"": the page’s own server |
fetch |
Function | Sends the requests, e.g. through the app’s own channel to startEditor’s fetch, so the API isn’t reached over the network. Requests other than GET need an Origin header of the editor’s address. |
The global fetch |
<edaha-editor> has:
| Attribute or property | Description |
|---|---|
api |
The EditorApi it uses. Set it before adding the editor to the page. |
hide-title |
Hides the “edaha.md” title. Home still goes back to the welcome prompt. |
hide-theme-switcher |
Hides the light/dark theme switcher, showing the device’s (or the app’s) light or dark theme. |
dirty |
Whether the open article has unsaved changes. |
leaveForm() |
Asks whether to save unsaved changes first: resolves with whether the editor can be closed (or another site shown). |
Preview pages and embed frames are still shown from the editor server, at its previews’ own address (http://localhost:<port>), so the app’s page must allow loading frames and images from it.