@lvl99dotcom/edaha-editor 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +13 -2
- package/dist/client/assets/index-KKuFTVH2.css +1 -0
- package/dist/client/assets/index-n5aM_lw1.js +1573 -0
- package/dist/client/index.html +21 -0
- package/dist/editor/index.d.ts +69 -0
- package/dist/editor/index.js +125 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/server/app/index.d.ts +98 -0
- package/dist/server/app/index.js +389 -0
- package/dist/server/articleIndex/index.d.ts +126 -0
- package/dist/server/articleIndex/index.js +352 -0
- package/dist/server/convert/index.d.ts +38 -0
- package/dist/server/convert/index.js +180 -0
- package/dist/server/files/index.d.ts +66 -0
- package/dist/server/files/index.js +256 -0
- package/dist/server/store/index.d.ts +287 -0
- package/dist/server/store/index.js +692 -0
- package/dist/server/types/index.d.ts +249 -0
- package/dist/server/types/index.js +15 -0
- package/package.json +59 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>Article editor | edaha.md</title>
|
|
7
|
+
<script>
|
|
8
|
+
// Show the theme chosen with the theme switcher (see src/client/theme) before the page appears
|
|
9
|
+
try {
|
|
10
|
+
const theme = localStorage.getItem("edaha-editor-theme");
|
|
11
|
+
if (theme === "light" || theme === "dark")
|
|
12
|
+
document.documentElement.dataset.theme = theme;
|
|
13
|
+
} catch {}
|
|
14
|
+
</script>
|
|
15
|
+
<script type="module" crossorigin src="/_editor/assets/index-n5aM_lw1.js"></script>
|
|
16
|
+
<link rel="stylesheet" crossorigin href="/_editor/assets/index-KKuFTVH2.css">
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<edaha-editor></edaha-editor>
|
|
20
|
+
</body>
|
|
21
|
+
</html>
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { EDITOR_PORT } from "@lvl99dotcom/edaha.md/cli";
|
|
3
|
+
import type { EmbedPlugin } from "@lvl99dotcom/edaha.md/embeds";
|
|
4
|
+
import { type SiteConfigOptions } from "@lvl99dotcom/edaha.md/generator/configFile";
|
|
5
|
+
/** The editor only listens on this computer's own address, so nothing else on the network can reach it */
|
|
6
|
+
export declare const EDITOR_HOST = "127.0.0.1";
|
|
7
|
+
/** The editor's default port (the same as `edaha editor`'s) */
|
|
8
|
+
export { EDITOR_PORT };
|
|
9
|
+
/** The URL path the editor page's own files (scripts, styles) are served under, apart from the site's static files */
|
|
10
|
+
export declare const EDITOR_BASE = "/_editor/";
|
|
11
|
+
/**
|
|
12
|
+
* The editor page's built files (`dist/client`, bundled by Vite). The path is the same from `src/editor` (in the
|
|
13
|
+
* workspace) and from the published `dist/editor`, so the editor never serves the page's unbuilt source.
|
|
14
|
+
*/
|
|
15
|
+
export declare const CLIENT_DIR: string;
|
|
16
|
+
/** Options for the article editor's server */
|
|
17
|
+
export interface EditorServerOptions extends SiteConfigOptions {
|
|
18
|
+
/** The default theme folder that the site's templates and static files override. Default: THEME_DIR */
|
|
19
|
+
themeDir?: string;
|
|
20
|
+
/** The port to listen on. Default: EDITOR_PORT */
|
|
21
|
+
port?: number;
|
|
22
|
+
/** The editor page's built files. Default: CLIENT_DIR */
|
|
23
|
+
clientDir?: string;
|
|
24
|
+
/** Fetches extra data for embeds (defaults to the global fetch) */
|
|
25
|
+
fetch?: typeof fetch;
|
|
26
|
+
/** Embed plugins added after the config file's `plugins` (a key in both uses the config file's). Default: none */
|
|
27
|
+
plugins?: readonly EmbedPlugin[];
|
|
28
|
+
}
|
|
29
|
+
/** A running editor server */
|
|
30
|
+
export interface EditorServer {
|
|
31
|
+
/** The editor's address, e.g. "http://127.0.0.1:5180" */
|
|
32
|
+
url: string;
|
|
33
|
+
/** Stop the server */
|
|
34
|
+
close: () => Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The editor's address for a port.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* editorUrl(5180); // "http://127.0.0.1:5180"
|
|
41
|
+
*/
|
|
42
|
+
export declare const editorUrl: (port: number) => string;
|
|
43
|
+
/**
|
|
44
|
+
* The port an editor listens on: the given one, or EDITOR_PORT.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* editorPort({}); // 5180
|
|
48
|
+
*/
|
|
49
|
+
export declare const editorPort: (options: EditorServerOptions) => number;
|
|
50
|
+
/**
|
|
51
|
+
* Create the article editor's web app: the editor page at `/`, its files under `EDITOR_BASE`, and the editor API,
|
|
52
|
+
* previews and the site's static files (see createEditorApp) everywhere else. Throws if the site's config file can't be
|
|
53
|
+
* found or read.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const app = await createEditorServer({ siteDir: "my-site" });
|
|
57
|
+
* await app.request("/"); // the editor page
|
|
58
|
+
*/
|
|
59
|
+
export declare const createEditorServer: (options: EditorServerOptions) => Promise<Hono>;
|
|
60
|
+
/**
|
|
61
|
+
* Start the article editor on this computer only (`EDITOR_HOST`). Never deploy it: it reads and writes the site's
|
|
62
|
+
* files.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* const editor = await startEditor({ siteDir: "my-site" });
|
|
66
|
+
* console.log(`Open ${editor.url}`);
|
|
67
|
+
* await editor.close();
|
|
68
|
+
*/
|
|
69
|
+
export declare const startEditor: (options: EditorServerOptions) => Promise<EditorServer>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { serve } from "@hono/node-server";
|
|
5
|
+
import { Hono } from "hono";
|
|
6
|
+
import mime from "mime-types";
|
|
7
|
+
import { EDITOR_PORT } from "@lvl99dotcom/edaha.md/cli";
|
|
8
|
+
import { createEditorApp, previewOriginOf } from "../server/app/index.js";
|
|
9
|
+
import { ArticleStore } from "../server/store/index.js";
|
|
10
|
+
import { loadSiteConfig, } from "@lvl99dotcom/edaha.md/generator/configFile";
|
|
11
|
+
/** The editor only listens on this computer's own address, so nothing else on the network can reach it */
|
|
12
|
+
export const EDITOR_HOST = "127.0.0.1";
|
|
13
|
+
/** The editor's default port (the same as `edaha editor`'s) */
|
|
14
|
+
export { EDITOR_PORT };
|
|
15
|
+
/** The URL path the editor page's own files (scripts, styles) are served under, apart from the site's static files */
|
|
16
|
+
export const EDITOR_BASE = "/_editor/";
|
|
17
|
+
/**
|
|
18
|
+
* The editor page's built files (`dist/client`, bundled by Vite). The path is the same from `src/editor` (in the
|
|
19
|
+
* workspace) and from the published `dist/editor`, so the editor never serves the page's unbuilt source.
|
|
20
|
+
*/
|
|
21
|
+
export const CLIENT_DIR = fileURLToPath(new URL("../../dist/client", import.meta.url));
|
|
22
|
+
/**
|
|
23
|
+
* The editor's address for a port.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* editorUrl(5180); // "http://127.0.0.1:5180"
|
|
27
|
+
*/
|
|
28
|
+
export const editorUrl = (port) => `http://${EDITOR_HOST}:${port}`;
|
|
29
|
+
/**
|
|
30
|
+
* The port an editor listens on: the given one, or EDITOR_PORT.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* editorPort({}); // 5180
|
|
34
|
+
*/
|
|
35
|
+
export const editorPort = (options) => options.port ?? EDITOR_PORT;
|
|
36
|
+
/**
|
|
37
|
+
* Read one of the editor page's built files, or undefined if there is no such file or the path leaves the folder.
|
|
38
|
+
*/
|
|
39
|
+
const clientFile = async (clientDir, path) => {
|
|
40
|
+
const parts = path.split("/");
|
|
41
|
+
if (parts.some((part) => part === "" || part.startsWith(".")))
|
|
42
|
+
return undefined;
|
|
43
|
+
try {
|
|
44
|
+
return (await readFile(join(clientDir, ...parts)));
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Create the article editor's web app: the editor page at `/`, its files under `EDITOR_BASE`, and the editor API,
|
|
52
|
+
* previews and the site's static files (see createEditorApp) everywhere else. Throws if the site's config file can't be
|
|
53
|
+
* found or read.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const app = await createEditorServer({ siteDir: "my-site" });
|
|
57
|
+
* await app.request("/"); // the editor page
|
|
58
|
+
*/
|
|
59
|
+
export const createEditorServer = async (options) => (await editorServerParts(options)).app;
|
|
60
|
+
/** The editor's web app, and the article store it uses (which watches the articles folder until closed) */
|
|
61
|
+
const editorServerParts = async (options) => {
|
|
62
|
+
const port = editorPort(options);
|
|
63
|
+
const clientDir = options.clientDir ?? CLIENT_DIR;
|
|
64
|
+
const store = new ArticleStore(await loadSiteConfig(options), options.themeDir);
|
|
65
|
+
// Every article is read and checked as the editor starts
|
|
66
|
+
await store.load();
|
|
67
|
+
const previewOrigin = previewOriginOf(editorUrl(port));
|
|
68
|
+
const editorApp = createEditorApp(store, {
|
|
69
|
+
allowedOrigins: [editorUrl(port)],
|
|
70
|
+
fetch: options.fetch,
|
|
71
|
+
plugins: options.plugins,
|
|
72
|
+
previewOrigin,
|
|
73
|
+
});
|
|
74
|
+
const app = new Hono();
|
|
75
|
+
// The previews' own origin doesn't show the editor page or its files
|
|
76
|
+
app.use("/", async (c, next) => new URL(c.req.url).origin === previewOrigin
|
|
77
|
+
? c.json({ error: "Not found" }, 404)
|
|
78
|
+
: next());
|
|
79
|
+
app.use(`${EDITOR_BASE}*`, async (c, next) => new URL(c.req.url).origin === previewOrigin
|
|
80
|
+
? c.json({ error: "Not found" }, 404)
|
|
81
|
+
: next());
|
|
82
|
+
app.get("/", async (c) => {
|
|
83
|
+
const html = await clientFile(clientDir, "index.html");
|
|
84
|
+
if (!html)
|
|
85
|
+
return c.text("The editor page is missing; build it with `pnpm build`", 500);
|
|
86
|
+
return c.body(html, 200, {
|
|
87
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
88
|
+
"Cache-Control": "no-store",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
app.get(`${EDITOR_BASE}*`, async (c) => {
|
|
92
|
+
const path = decodeURIComponent(c.req.path.slice(EDITOR_BASE.length));
|
|
93
|
+
const contents = await clientFile(clientDir, path);
|
|
94
|
+
if (!contents)
|
|
95
|
+
return c.json({ error: "File not found" }, 404);
|
|
96
|
+
return c.body(contents, 200, {
|
|
97
|
+
"Content-Type": mime.lookup(path) || "application/octet-stream",
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
app.route("/", editorApp);
|
|
101
|
+
return { app, store };
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Start the article editor on this computer only (`EDITOR_HOST`). Never deploy it: it reads and writes the site's
|
|
105
|
+
* files.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* const editor = await startEditor({ siteDir: "my-site" });
|
|
109
|
+
* console.log(`Open ${editor.url}`);
|
|
110
|
+
* await editor.close();
|
|
111
|
+
*/
|
|
112
|
+
export const startEditor = async (options) => {
|
|
113
|
+
const port = editorPort(options);
|
|
114
|
+
const { app, store } = await editorServerParts(options);
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
const server = serve({ fetch: app.fetch, hostname: EDITOR_HOST, port }, () => resolve({
|
|
117
|
+
url: editorUrl(port),
|
|
118
|
+
close: () => {
|
|
119
|
+
store.close();
|
|
120
|
+
return new Promise((done, fail) => server.close((error) => (error ? fail(error) : done())));
|
|
121
|
+
},
|
|
122
|
+
}));
|
|
123
|
+
server.once("error", reject);
|
|
124
|
+
});
|
|
125
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEditorServer, EDITOR_HOST, EDITOR_PORT, startEditor, type EditorServer, type EditorServerOptions, } from "./editor/index.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// The article editor's public API: start the local editor for an edaha.md site, or create its web app to serve it
|
|
2
|
+
// yourself. `edaha editor` (from @lvl99dotcom/edaha.md) uses startEditor.
|
|
3
|
+
export { createEditorServer, EDITOR_HOST, EDITOR_PORT, startEditor, } from "./editor/index.js";
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { type EmbedPlugin } from "@lvl99dotcom/edaha.md/embeds";
|
|
3
|
+
import { type ArticleStore } from "../store/index.ts";
|
|
4
|
+
import type { ArticleQuery, FileQuery } from "../types/index.ts";
|
|
5
|
+
/**
|
|
6
|
+
* Read a file query from a URL's search parameters, e.g. "?search=photo&kind=image&sort=size&order=desc&page=2".
|
|
7
|
+
* Unknown kinds, sorts and orders, and pages that aren't numbers, are left out.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* fileQuery(new URLSearchParams("kind=image&page=2")); // { kind: "image", page: 2 }
|
|
11
|
+
*/
|
|
12
|
+
export declare const fileQuery: (params: URLSearchParams) => FileQuery;
|
|
13
|
+
/**
|
|
14
|
+
* Read an article query from a URL's search parameters, e.g. "?search=hello&type=news&sort=title&order=desc&page=2".
|
|
15
|
+
* Unknown statuses, sorts and orders, and pages that aren't numbers, are left out.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* articleQuery(new URLSearchParams("search=hello&page=2")); // { search: "hello", page: 2 }
|
|
19
|
+
*/
|
|
20
|
+
export declare const articleQuery: (params: URLSearchParams) => ArticleQuery;
|
|
21
|
+
/** Options for the editor API */
|
|
22
|
+
export interface EditorAppOptions {
|
|
23
|
+
/** Origins allowed to change articles, e.g. "http://127.0.0.1:5180" */
|
|
24
|
+
allowedOrigins: readonly string[];
|
|
25
|
+
/** Fetches extra data for embeds (defaults to the global fetch) */
|
|
26
|
+
fetch?: typeof fetch | undefined;
|
|
27
|
+
/** Embed plugins added after the config file's `plugins` (a key in both uses the config file's). Default: none */
|
|
28
|
+
plugins?: readonly EmbedPlugin[] | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Optional; the origin preview pages and embed frames are shown from, e.g. "http://localhost:5180": an address of
|
|
31
|
+
* the same server that isn't the editor's own, so they run in an origin (and site) of their own. The API answers
|
|
32
|
+
* nothing there.
|
|
33
|
+
*/
|
|
34
|
+
previewOrigin?: string | undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The origin preview pages and embed frames are shown from, for an editor at an address: `localhost` instead of
|
|
38
|
+
* `127.0.0.1`, which is the same server but another origin and site.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* previewOriginOf("http://127.0.0.1:5180"); // "http://localhost:5180"
|
|
42
|
+
*/
|
|
43
|
+
export declare const previewOriginOf: (editorOrigin: string) => string;
|
|
44
|
+
/**
|
|
45
|
+
* The Content Security Policy of preview pages, embed frames and the files they load. They are shown from an origin
|
|
46
|
+
* of their own (`previewOrigin`), which the editor's API doesn't answer and whose changes it refuses, and are
|
|
47
|
+
* sandboxed (`sandbox`, keeping their own origin, so embedded players such as Bandcamp's keep theirs) so they can't
|
|
48
|
+
* send forms or navigate the editor page. Inside, they may run their own scripts and styles, and load from their own
|
|
49
|
+
* address and the web addresses the embed plugins list in `sources`. Only the editor's pages may show them in frames.
|
|
50
|
+
*
|
|
51
|
+
* @param self - the address they are served from, e.g. "http://localhost:5180"
|
|
52
|
+
* @param ancestors - the editor's own addresses, which may show them in frames
|
|
53
|
+
* @example
|
|
54
|
+
* previewCsp([youtube], "http://localhost:5180", ["http://127.0.0.1:5180"]); // "sandbox allow-scripts allow-same-origin ...; default-src http://localhost:5180; ...; frame-src https://www.youtube-nocookie.com; ..."
|
|
55
|
+
*/
|
|
56
|
+
export declare const previewCsp: (plugins: readonly EmbedPlugin[], self: string, ancestors?: readonly string[]) => string;
|
|
57
|
+
/**
|
|
58
|
+
* The page shown in an embed block's frame in the editor's body: the embed rendered for the editor, with the HTML it
|
|
59
|
+
* needs once, in the editor's light or dark theme.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* embedFramePage('<div class="embed">...</div>', { head: "", foot: "" }, "dark");
|
|
63
|
+
*/
|
|
64
|
+
export declare const embedFramePage: (html: string, parts: {
|
|
65
|
+
head: string;
|
|
66
|
+
foot: string;
|
|
67
|
+
}, theme: "light" | "dark" | undefined) => string;
|
|
68
|
+
/**
|
|
69
|
+
* Create the article editor's local API. Requests that change files must come from the editor's own page (checked
|
|
70
|
+
* with the Origin header), so other websites open in the same browser can't write articles.
|
|
71
|
+
*
|
|
72
|
+
* Routes:
|
|
73
|
+
* - `GET /api/articles`: list articles
|
|
74
|
+
* - `GET /api/types`: the article types from the config file, with their labels
|
|
75
|
+
* - `GET /api/templates/:type`: the template names an article of a type can use
|
|
76
|
+
* - `POST /api/articles`: create an article
|
|
77
|
+
* - `GET|PUT|DELETE /api/articles/:type/:slug`: load, save (and move) or delete an article
|
|
78
|
+
* - `POST /api/articles/:type/:slug/files`: upload a file (multipart field "file")
|
|
79
|
+
* - `GET /files/:type/:slug/:name`: an article's file, for previews in the editor
|
|
80
|
+
* - `GET /api/embeds`: the embed plugins (`EmbedInfo`), for the editor's embed dialog
|
|
81
|
+
* - `POST /api/embed-validations`: check an embed block's content (`{ key, content }`) with its plugin's `validate`,
|
|
82
|
+
* giving why it isn't valid (`EmbedValidation`)
|
|
83
|
+
* - `POST /api/embed-frames`: remember an embed block (`{ key, content }`) for its frame in the editor's body, and give
|
|
84
|
+
* the frame's URL, and why its content isn't valid (then it has no frame) or can't be shown (`EmbedFrame`); `GET /embed-frame/:id/?theme=light|dark` is the frame's page
|
|
85
|
+
* - `POST /api/convert/markdown` and `POST /api/convert/html`: convert an article body between editor HTML and Markdown
|
|
86
|
+
* (`{ html | markdown, location? }`, where `location` is the saved article's, which relative URLs are relative to)
|
|
87
|
+
* - `POST /api/preview`: remember unsaved article data (`{ data, location? }`) for a preview page, and give its URL
|
|
88
|
+
* (`{ url }`); answers 400 if it can't be rendered
|
|
89
|
+
* - `GET /preview/:id/`: render a remembered preview with its type's "single" template, and `GET /<path>`: one of the
|
|
90
|
+
* site's static files (e.g. `/site.css`) or an article's file at its path on the website (e.g.
|
|
91
|
+
* `/news/welcome/photo.png`), all sandboxed by their Content Security Policy (`previewCsp`)
|
|
92
|
+
*
|
|
93
|
+
* Embeds are rendered with the config file's `plugins`, then the given `plugins`.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const app = createEditorApp(new ArticleStore(await loadSiteConfig({ siteDir: "my-site" })), { allowedOrigins: ["http://127.0.0.1:5180"] });
|
|
97
|
+
*/
|
|
98
|
+
export declare const createEditorApp: (store: ArticleStore, { allowedOrigins, fetch: fetchFn, plugins: givenPlugins, previewOrigin, }: EditorAppOptions) => Hono;
|