@lvl99dotcom/edaha.md 0.1.0 → 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 +183 -183
- package/README.md +7 -252
- package/dist/cli/index.d.ts +36 -6
- package/dist/cli/index.js +48 -9
- package/dist/embeds/blocks/index.d.ts +59 -0
- package/dist/embeds/blocks/index.js +103 -0
- package/dist/embeds/index.d.ts +122 -19
- package/dist/embeds/index.js +381 -41
- package/dist/embeds/types/index.d.ts +111 -8
- package/dist/generator/article/index.d.ts +52 -12
- package/dist/generator/article/index.js +94 -26
- package/dist/generator/build/index.d.ts +55 -23
- package/dist/generator/build/index.js +151 -81
- package/dist/generator/config/index.d.ts +45 -8
- package/dist/generator/config/index.js +152 -30
- package/dist/generator/configFile/index.d.ts +58 -0
- package/dist/generator/configFile/index.js +122 -0
- package/dist/generator/fields/index.d.ts +93 -0
- package/dist/generator/fields/index.js +186 -0
- package/dist/generator/format/index.d.ts +40 -0
- package/dist/generator/format/index.js +75 -0
- package/dist/generator/lists/index.d.ts +3 -3
- package/dist/generator/lists/index.js +9 -8
- package/dist/generator/markdown/index.d.ts +8 -3
- package/dist/generator/markdown/index.js +11 -4
- package/dist/generator/metadata/index.d.ts +150 -0
- package/dist/generator/metadata/index.js +361 -0
- package/dist/generator/metadataGroups/index.d.ts +45 -0
- package/dist/generator/metadataGroups/index.js +169 -0
- package/dist/generator/templates/index.d.ts +35 -3
- package/dist/generator/templates/index.js +72 -6
- package/dist/generator/types/index.d.ts +43 -18
- package/dist/generator/urls/index.d.ts +18 -8
- package/dist/generator/urls/index.js +22 -12
- package/dist/index.d.ts +5 -7
- package/dist/index.js +6 -7
- package/package.json +18 -47
- package/theme/static/{site.css → base.css} +132 -24
- package/theme/templates/article/list.njk +6 -6
- package/theme/templates/article/single.njk +13 -7
- package/theme/templates/layouts/html.njk +54 -32
- package/theme/templates/layouts/rssfeed.njk +7 -7
- package/theme/templates/page/home.njk +2 -7
- package/theme/templates/page/single.njk +2 -2
- package/dist/client/assets/index-BbKEbPS8.css +0 -1
- package/dist/client/assets/index-DtG7vi1d.js +0 -559
- package/dist/client/index.html +0 -13
- package/dist/editor/index.d.ts +0 -62
- package/dist/editor/index.js +0 -102
- package/dist/embeds/bandcamp/index.d.ts +0 -9
- package/dist/embeds/bandcamp/index.js +0 -28
- package/dist/embeds/youtube/index.d.ts +0 -17
- package/dist/embeds/youtube/index.js +0 -48
- package/dist/server/app/index.d.ts +0 -39
- package/dist/server/app/index.js +0 -138
- package/dist/server/convert/index.d.ts +0 -25
- package/dist/server/convert/index.js +0 -125
- package/dist/server/store/index.d.ts +0 -148
- package/dist/server/store/index.js +0 -411
- package/dist/server/types/index.d.ts +0 -76
- package/dist/server/types/index.js +0 -1
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import type { EmbedResolver } from "../../embeds/index.ts";
|
|
2
|
-
import type { ArticleSummary, ArticleTypeLabels, EditorArticle, EditorArticleData, UploadResult } from "../types/index.ts";
|
|
3
|
-
/** An editor request that can't be done, with the HTTP status to answer with */
|
|
4
|
-
export declare class EditorError extends Error {
|
|
5
|
-
readonly status: number;
|
|
6
|
-
constructor(status: number, message: string);
|
|
7
|
-
}
|
|
8
|
-
/** How many previews the editor remembers (the newest) */
|
|
9
|
-
export declare const MAX_PREVIEWS = 20;
|
|
10
|
-
/**
|
|
11
|
-
* Check an article type and slug are valid folder names. Throws a 400 EditorError if not.
|
|
12
|
-
*/
|
|
13
|
-
export declare const checkLocation: (type: string, slug: string) => void;
|
|
14
|
-
/**
|
|
15
|
-
* The URL relative URLs in an article's body point at in the editor: its saved folder's file URL, or "" for an article
|
|
16
|
-
* that isn't saved yet (and has no files).
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* bodyBaseUrl({ type: "news", slug: "hello" }); // "/files/news/hello/"
|
|
20
|
-
* bodyBaseUrl(undefined); // ""
|
|
21
|
-
*/
|
|
22
|
-
export declare const bodyBaseUrl: (location: {
|
|
23
|
-
type: string;
|
|
24
|
-
slug: string;
|
|
25
|
-
} | undefined) => string;
|
|
26
|
-
/**
|
|
27
|
-
* Make an uploaded file name safe: a slug of its base name plus its lowercase extension.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* safeFileName("My Photo (1).JPG"); // "my-photo-1.jpg"
|
|
31
|
-
*/
|
|
32
|
-
export declare const safeFileName: (name: string) => string;
|
|
33
|
-
/**
|
|
34
|
-
* Write an article's markdown source: the YAML config header, the title as an H1 and the body.
|
|
35
|
-
* The type (always the article's folder) and defaults (empty lists) are left out to keep the header short.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* serializeArticle({ type: "article", slug: "hi", status: "published", date, title: "Hi", tags: [], attachments: [] }, "Body");
|
|
39
|
-
*/
|
|
40
|
-
export declare const serializeArticle: (data: EditorArticleData, body: string) => string;
|
|
41
|
-
/**
|
|
42
|
-
* Reads and writes articles in the site's `articles/` folder for the editor, and renders previews with the site's
|
|
43
|
-
* templates over the default theme's.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* const store = new ArticleStore("my-site");
|
|
47
|
-
* await store.list();
|
|
48
|
-
*/
|
|
49
|
-
export declare class ArticleStore {
|
|
50
|
-
private readonly articlesDir;
|
|
51
|
-
/** Previews' unsaved data by ID, the newest last (see `rememberPreview`) */
|
|
52
|
-
private readonly previews;
|
|
53
|
-
private readonly siteDir;
|
|
54
|
-
private readonly themeDir;
|
|
55
|
-
/**
|
|
56
|
-
* @param siteDir - the site folder (holds config.yml and articles/, and optionally templates/ and static/)
|
|
57
|
-
* @param themeDir - the default theme folder that the site's templates and static files override
|
|
58
|
-
*/
|
|
59
|
-
constructor(siteDir: string, themeDir?: string);
|
|
60
|
-
/** The site's templates over the theme's */
|
|
61
|
-
private loadTemplates;
|
|
62
|
-
/** Read config.yml */
|
|
63
|
-
private config;
|
|
64
|
-
/**
|
|
65
|
-
* The template names an article of a type can use (see Templates.names).
|
|
66
|
-
*/
|
|
67
|
-
templates(type: string): string[];
|
|
68
|
-
/**
|
|
69
|
-
* The article types from config.yml, with their labels.
|
|
70
|
-
*/
|
|
71
|
-
types(): Promise<Record<string, ArticleTypeLabels>>;
|
|
72
|
-
/** An article's folder */
|
|
73
|
-
private dir;
|
|
74
|
-
/** Read an article's source, or throw a 404 EditorError */
|
|
75
|
-
private source;
|
|
76
|
-
/**
|
|
77
|
-
* List every article, including drafts, newest first. Articles that can't be read are listed with their error.
|
|
78
|
-
*/
|
|
79
|
-
list(): Promise<ArticleSummary[]>;
|
|
80
|
-
/**
|
|
81
|
-
* Load an article for editing. Throws a 404 EditorError if it doesn't exist, or 422 if it can't be read.
|
|
82
|
-
*/
|
|
83
|
-
read(type: string, slug: string): Promise<EditorArticle>;
|
|
84
|
-
/**
|
|
85
|
-
* Save an article. When its type or slug changed, its folder (with all its files) moves to the new location.
|
|
86
|
-
* Pass no `from` for a new article. Returns where the article now is.
|
|
87
|
-
*/
|
|
88
|
-
save(data: EditorArticleData, from?: {
|
|
89
|
-
type: string;
|
|
90
|
-
slug: string;
|
|
91
|
-
}): Promise<{
|
|
92
|
-
type: string;
|
|
93
|
-
slug: string;
|
|
94
|
-
}>;
|
|
95
|
-
/**
|
|
96
|
-
* Render an article from unsaved editor data as the website would, with its type's "single" template, for the
|
|
97
|
-
* editor's preview. Drafts are shown as if published. Its files come from its saved folder (`location`), if it has one,
|
|
98
|
-
* and are shown from the editor's file URLs. Throws a 400 EditorError if it can't be rendered (e.g. a file isn't in
|
|
99
|
-
* the article's folder).
|
|
100
|
-
*
|
|
101
|
-
* @param embeds - renders embed blocks
|
|
102
|
-
*/
|
|
103
|
-
preview(data: EditorArticleData, location: {
|
|
104
|
-
type: string;
|
|
105
|
-
slug: string;
|
|
106
|
-
} | undefined, embeds: EmbedResolver): Promise<string>;
|
|
107
|
-
/**
|
|
108
|
-
* Delete an article and all its files.
|
|
109
|
-
*/
|
|
110
|
-
remove(type: string, slug: string): Promise<void>;
|
|
111
|
-
/**
|
|
112
|
-
* Save an uploaded file into an article's folder (replacing a file with the same name).
|
|
113
|
-
*/
|
|
114
|
-
upload(type: string, slug: string, fileName: string, contents: Uint8Array): Promise<UploadResult>;
|
|
115
|
-
/**
|
|
116
|
-
* Remember an article's unsaved data for its preview page, under a new ID. Only the newest `MAX_PREVIEWS` are kept,
|
|
117
|
-
* in memory: they're gone when the editor stops. Returns the ID.
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* const id = store.rememberPreview(data, { type: "news", slug: "hello" }); // the editor shows it at /preview/<id>/
|
|
121
|
-
*/
|
|
122
|
-
rememberPreview(data: EditorArticleData, location: {
|
|
123
|
-
type: string;
|
|
124
|
-
slug: string;
|
|
125
|
-
} | undefined): string;
|
|
126
|
-
/**
|
|
127
|
-
* Render a remembered preview as the website would (see `preview`), so changes to templates show when it reloads.
|
|
128
|
-
* Throws a 404 EditorError if there is no preview with that ID (e.g. after the editor restarted).
|
|
129
|
-
*
|
|
130
|
-
* @param embeds - renders embed blocks
|
|
131
|
-
*/
|
|
132
|
-
renderPreview(id: string, embeds: EmbedResolver): Promise<string>;
|
|
133
|
-
/**
|
|
134
|
-
* Read one of the static files published at the website's root (the site's `static/`, then the theme's), e.g.
|
|
135
|
-
* `site.css`, for previews. Throws a 404 EditorError if there is no such file, or the path leaves the folder or
|
|
136
|
-
* names a hidden file.
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* await store.staticFile("fonts/my-font.woff2");
|
|
140
|
-
*/
|
|
141
|
-
staticFile(path: string): Promise<Uint8Array>;
|
|
142
|
-
/** The static folders (stylesheet, fonts) published at the website's root: the site's, then the theme's */
|
|
143
|
-
get staticDirs(): string[];
|
|
144
|
-
/**
|
|
145
|
-
* Read a file from an article's folder. Throws a 404 EditorError if there is no such file.
|
|
146
|
-
*/
|
|
147
|
-
file(type: string, slug: string, name: string): Promise<Uint8Array>;
|
|
148
|
-
}
|
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdir, readdir, readFile, rename, rm, stat, writeFile, } from "node:fs/promises";
|
|
3
|
-
import { basename, extname, join } from "node:path";
|
|
4
|
-
import { stringify } from "yaml";
|
|
5
|
-
import { loadArticle, parseArticleConfig, splitArticle, splitContent, } from "../../generator/article/index.js";
|
|
6
|
-
import { blogData, DEFAULT_SITE_URL } from "../../generator/build/index.js";
|
|
7
|
-
import { defaultTypeConfig, parseConfig, RESERVED_TYPES, } from "../../generator/config/index.js";
|
|
8
|
-
import { createMarkdown, renderMarkdown, } from "../../generator/markdown/index.js";
|
|
9
|
-
import { Templates } from "../../generator/templates/index.js";
|
|
10
|
-
import { THEME_DIR } from "../../theme/index.js";
|
|
11
|
-
import { slugProblem } from "../../generator/urls/index.js";
|
|
12
|
-
import { isSlug, slugify } from "../../generator/slug/index.js";
|
|
13
|
-
import { editorHtmlToMarkdown, filesUrl, markdownToEditorHtml, } from "../convert/index.js";
|
|
14
|
-
/** An editor request that can't be done, with the HTTP status to answer with */
|
|
15
|
-
export class EditorError extends Error {
|
|
16
|
-
status;
|
|
17
|
-
constructor(status, message) {
|
|
18
|
-
super(message);
|
|
19
|
-
this.status = status;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/** List the names of sub-folders in a folder (none if it doesn't exist) */
|
|
23
|
-
const subfolders = async (dir) => {
|
|
24
|
-
try {
|
|
25
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
26
|
-
return entries
|
|
27
|
-
.filter((e) => e.isDirectory())
|
|
28
|
-
.map((e) => e.name)
|
|
29
|
-
.sort();
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
return [];
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
/** How many previews the editor remembers (the newest) */
|
|
36
|
-
export const MAX_PREVIEWS = 20;
|
|
37
|
-
/** Whether a path exists */
|
|
38
|
-
const exists = (path) => stat(path).then(() => true, () => false);
|
|
39
|
-
/**
|
|
40
|
-
* Check an article type and slug are valid folder names. Throws a 400 EditorError if not.
|
|
41
|
-
*/
|
|
42
|
-
export const checkLocation = (type, slug) => {
|
|
43
|
-
if (!isSlug(type) || RESERVED_TYPES.includes(type))
|
|
44
|
-
throw new EditorError(400, `Type "${type}" must be lowercase kebab-case and not ${RESERVED_TYPES.join(" or ")}`);
|
|
45
|
-
if (!isSlug(slug))
|
|
46
|
-
throw new EditorError(400, `Slug "${slug}" must be lowercase kebab-case`);
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* The URL relative URLs in an article's body point at in the editor: its saved folder's file URL, or "" for an article
|
|
50
|
-
* that isn't saved yet (and has no files).
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* bodyBaseUrl({ type: "news", slug: "hello" }); // "/files/news/hello/"
|
|
54
|
-
* bodyBaseUrl(undefined); // ""
|
|
55
|
-
*/
|
|
56
|
-
export const bodyBaseUrl = (location) => {
|
|
57
|
-
if (!location)
|
|
58
|
-
return "";
|
|
59
|
-
checkLocation(location.type, location.slug);
|
|
60
|
-
return filesUrl(location.type, location.slug);
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* Make an uploaded file name safe: a slug of its base name plus its lowercase extension.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* safeFileName("My Photo (1).JPG"); // "my-photo-1.jpg"
|
|
67
|
-
*/
|
|
68
|
-
export const safeFileName = (name) => {
|
|
69
|
-
const extension = extname(name)
|
|
70
|
-
.toLowerCase()
|
|
71
|
-
.replace(/[^.a-z0-9]/g, "");
|
|
72
|
-
const base = slugify(name.slice(0, name.length - extname(name).length));
|
|
73
|
-
if (!base)
|
|
74
|
-
throw new EditorError(400, `"${name}" is not a usable file name`);
|
|
75
|
-
return base + extension;
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* Write an article's markdown source: the YAML config header, the title as an H1 and the body.
|
|
79
|
-
* The type (always the article's folder) and defaults (empty lists) are left out to keep the header short.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* serializeArticle({ type: "article", slug: "hi", status: "published", date, title: "Hi", tags: [], attachments: [] }, "Body");
|
|
83
|
-
*/
|
|
84
|
-
export const serializeArticle = (data, body) => {
|
|
85
|
-
const author = data.author && (data.author.email || data.author.url)
|
|
86
|
-
? Object.fromEntries(Object.entries(data.author).filter(([, value]) => value))
|
|
87
|
-
: data.author?.name;
|
|
88
|
-
const config = Object.fromEntries(Object.entries({
|
|
89
|
-
status: data.status,
|
|
90
|
-
date: data.date,
|
|
91
|
-
author,
|
|
92
|
-
template: data.template,
|
|
93
|
-
excerpt: data.excerpt,
|
|
94
|
-
thumbnail: data.thumbnail,
|
|
95
|
-
attachments: data.attachments.length
|
|
96
|
-
? data.attachments.map((attachment) => Object.fromEntries(Object.entries(attachment).filter(([, value]) => value)))
|
|
97
|
-
: undefined,
|
|
98
|
-
tags: data.tags.length ? data.tags : undefined,
|
|
99
|
-
}).filter(([, value]) => value !== undefined && value !== ""));
|
|
100
|
-
return `<!--yaml\n${stringify(config)}-->\n\n# ${data.title.trim()}\n\n${body}\n`;
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Reads and writes articles in the site's `articles/` folder for the editor, and renders previews with the site's
|
|
104
|
-
* templates over the default theme's.
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
|
-
* const store = new ArticleStore("my-site");
|
|
108
|
-
* await store.list();
|
|
109
|
-
*/
|
|
110
|
-
export class ArticleStore {
|
|
111
|
-
articlesDir;
|
|
112
|
-
/** Previews' unsaved data by ID, the newest last (see `rememberPreview`) */
|
|
113
|
-
previews = new Map();
|
|
114
|
-
siteDir;
|
|
115
|
-
themeDir;
|
|
116
|
-
/**
|
|
117
|
-
* @param siteDir - the site folder (holds config.yml and articles/, and optionally templates/ and static/)
|
|
118
|
-
* @param themeDir - the default theme folder that the site's templates and static files override
|
|
119
|
-
*/
|
|
120
|
-
constructor(siteDir, themeDir = THEME_DIR) {
|
|
121
|
-
this.siteDir = siteDir;
|
|
122
|
-
this.themeDir = themeDir;
|
|
123
|
-
this.articlesDir = join(siteDir, "articles");
|
|
124
|
-
}
|
|
125
|
-
/** The site's templates over the theme's */
|
|
126
|
-
loadTemplates() {
|
|
127
|
-
return new Templates([join(this.siteDir, "templates"), join(this.themeDir, "templates")], console);
|
|
128
|
-
}
|
|
129
|
-
/** Read config.yml */
|
|
130
|
-
async config() {
|
|
131
|
-
return parseConfig(await readFile(join(this.siteDir, "config.yml"), "utf8"));
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* The template names an article of a type can use (see Templates.names).
|
|
135
|
-
*/
|
|
136
|
-
templates(type) {
|
|
137
|
-
return this.loadTemplates().names(type);
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* The article types from config.yml, with their labels.
|
|
141
|
-
*/
|
|
142
|
-
async types() {
|
|
143
|
-
const config = await this.config();
|
|
144
|
-
return Object.fromEntries(Object.entries(config.types).map(([type, { labelSingle, labelPlural }]) => [
|
|
145
|
-
type,
|
|
146
|
-
{ labelSingle, labelPlural },
|
|
147
|
-
]));
|
|
148
|
-
}
|
|
149
|
-
/** An article's folder */
|
|
150
|
-
dir(type, slug) {
|
|
151
|
-
checkLocation(type, slug);
|
|
152
|
-
return join(this.articlesDir, type, slug);
|
|
153
|
-
}
|
|
154
|
-
/** Read an article's source, or throw a 404 EditorError */
|
|
155
|
-
async source(type, slug) {
|
|
156
|
-
try {
|
|
157
|
-
return await readFile(join(this.dir(type, slug), "article.md"), "utf8");
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
if (error instanceof EditorError)
|
|
161
|
-
throw error;
|
|
162
|
-
throw new EditorError(404, `Article ${type}/${slug} not found`);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* List every article, including drafts, newest first. Articles that can't be read are listed with their error.
|
|
167
|
-
*/
|
|
168
|
-
async list() {
|
|
169
|
-
const summaries = [];
|
|
170
|
-
for (const type of await subfolders(this.articlesDir)) {
|
|
171
|
-
for (const slug of await subfolders(join(this.articlesDir, type))) {
|
|
172
|
-
let source;
|
|
173
|
-
try {
|
|
174
|
-
source = await this.source(type, slug);
|
|
175
|
-
}
|
|
176
|
-
catch {
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
try {
|
|
180
|
-
const { header, content } = splitArticle(source);
|
|
181
|
-
const config = parseArticleConfig(header, type, `${type}/${slug}`);
|
|
182
|
-
summaries.push({
|
|
183
|
-
type,
|
|
184
|
-
slug,
|
|
185
|
-
title: config.title ?? splitContent(content).heading ?? slug,
|
|
186
|
-
status: config.status,
|
|
187
|
-
date: config.date,
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
catch (error) {
|
|
191
|
-
summaries.push({
|
|
192
|
-
type,
|
|
193
|
-
slug,
|
|
194
|
-
title: slug,
|
|
195
|
-
status: "draft",
|
|
196
|
-
date: "",
|
|
197
|
-
error: error.message,
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return summaries.sort((a, b) => b.date.localeCompare(a.date) || a.slug.localeCompare(b.slug));
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Load an article for editing. Throws a 404 EditorError if it doesn't exist, or 422 if it can't be read.
|
|
206
|
-
*/
|
|
207
|
-
async read(type, slug) {
|
|
208
|
-
const source = await this.source(type, slug);
|
|
209
|
-
const { header, content } = splitArticle(source);
|
|
210
|
-
let config;
|
|
211
|
-
try {
|
|
212
|
-
config = parseArticleConfig(header, type, `${type}/${slug}`);
|
|
213
|
-
}
|
|
214
|
-
catch (error) {
|
|
215
|
-
throw new EditorError(422, error.message);
|
|
216
|
-
}
|
|
217
|
-
const parts = splitContent(content);
|
|
218
|
-
// The title is edited separately, so a leading H1 stays out of the body; the readmore marker stays in
|
|
219
|
-
const body = content.replace(/^\s*#[ \t]+.+(?:\r?\n|$)/, "").trim();
|
|
220
|
-
const files = (await readdir(this.dir(type, slug)))
|
|
221
|
-
.filter((name) => name !== "article.md")
|
|
222
|
-
.sort();
|
|
223
|
-
return {
|
|
224
|
-
type,
|
|
225
|
-
slug,
|
|
226
|
-
status: config.status,
|
|
227
|
-
date: config.date,
|
|
228
|
-
title: config.title ?? parts.heading ?? "",
|
|
229
|
-
template: config.template,
|
|
230
|
-
author: config.author,
|
|
231
|
-
excerpt: config.excerpt,
|
|
232
|
-
thumbnail: config.thumbnail,
|
|
233
|
-
tags: config.tags,
|
|
234
|
-
attachments: config.attachments,
|
|
235
|
-
bodyHtml: markdownToEditorHtml(body, filesUrl(type, slug)),
|
|
236
|
-
files,
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Save an article. When its type or slug changed, its folder (with all its files) moves to the new location.
|
|
241
|
-
* Pass no `from` for a new article. Returns where the article now is.
|
|
242
|
-
*/
|
|
243
|
-
async save(data, from) {
|
|
244
|
-
const dir = this.dir(data.type, data.slug);
|
|
245
|
-
// The slug must not clash with other URLs
|
|
246
|
-
const config = await this.config();
|
|
247
|
-
const problem = slugProblem(data.slug, data.type, config.path, {
|
|
248
|
-
[data.type]: defaultTypeConfig(data.type),
|
|
249
|
-
...config.types,
|
|
250
|
-
}, config.reservedPaths);
|
|
251
|
-
if (problem)
|
|
252
|
-
throw new EditorError(400, `The slug can't be used: ${problem}`);
|
|
253
|
-
if (!data.title.trim())
|
|
254
|
-
throw new EditorError(400, "The article needs a title");
|
|
255
|
-
const moving = !from || from.type !== data.type || from.slug !== data.slug;
|
|
256
|
-
if (moving && (await exists(dir)))
|
|
257
|
-
throw new EditorError(409, `Article ${data.type}/${data.slug} already exists`);
|
|
258
|
-
const fromDir = from ? this.dir(from.type, from.slug) : undefined;
|
|
259
|
-
if (fromDir && !(await exists(join(fromDir, "article.md"))))
|
|
260
|
-
throw new EditorError(404, `Article ${from?.type}/${from?.slug} not found`);
|
|
261
|
-
// Files referenced from the body keep working after a move, as they are saved relative to the folder
|
|
262
|
-
const body = editorHtmlToMarkdown(data.bodyHtml, filesUrl(from?.type ?? data.type, from?.slug ?? data.slug));
|
|
263
|
-
const source = serializeArticle(data, body);
|
|
264
|
-
try {
|
|
265
|
-
parseArticleConfig(splitArticle(source).header, data.type, `${data.type}/${data.slug}`);
|
|
266
|
-
}
|
|
267
|
-
catch (error) {
|
|
268
|
-
throw new EditorError(400, error.message);
|
|
269
|
-
}
|
|
270
|
-
if (fromDir && moving) {
|
|
271
|
-
await mkdir(join(this.articlesDir, data.type), { recursive: true });
|
|
272
|
-
await rename(fromDir, dir);
|
|
273
|
-
}
|
|
274
|
-
else
|
|
275
|
-
await mkdir(dir, { recursive: true });
|
|
276
|
-
await writeFile(join(dir, "article.md"), source);
|
|
277
|
-
return { type: data.type, slug: data.slug };
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Render an article from unsaved editor data as the website would, with its type's "single" template, for the
|
|
281
|
-
* editor's preview. Drafts are shown as if published. Its files come from its saved folder (`location`), if it has one,
|
|
282
|
-
* and are shown from the editor's file URLs. Throws a 400 EditorError if it can't be rendered (e.g. a file isn't in
|
|
283
|
-
* the article's folder).
|
|
284
|
-
*
|
|
285
|
-
* @param embeds - renders embed blocks
|
|
286
|
-
*/
|
|
287
|
-
async preview(data, location, embeds) {
|
|
288
|
-
if (!isSlug(data.type))
|
|
289
|
-
throw new EditorError(400, `Type "${data.type}" must be lowercase kebab-case`);
|
|
290
|
-
const baseUrl = bodyBaseUrl(location);
|
|
291
|
-
const config = await this.config();
|
|
292
|
-
const blog = blogData(config, {
|
|
293
|
-
[data.type]: defaultTypeConfig(data.type),
|
|
294
|
-
...config.types,
|
|
295
|
-
});
|
|
296
|
-
const source = serializeArticle({ ...data, status: "published", title: data.title.trim() || "Untitled" }, editorHtmlToMarkdown(data.bodyHtml, baseUrl));
|
|
297
|
-
const name = `${data.type}/${data.slug || "new"}`;
|
|
298
|
-
const markdown = createMarkdown();
|
|
299
|
-
try {
|
|
300
|
-
// Published, so always rendered
|
|
301
|
-
const article = (await loadArticle(source, data.type, {
|
|
302
|
-
blog,
|
|
303
|
-
// An unsaved article has no folder, so any file it names isn't found
|
|
304
|
-
dir: location
|
|
305
|
-
? this.dir(location.type, location.slug)
|
|
306
|
-
: join(this.articlesDir, data.type, ".unsaved"),
|
|
307
|
-
slug: data.slug,
|
|
308
|
-
url: baseUrl,
|
|
309
|
-
typeUrl: blog.types[data.type].url,
|
|
310
|
-
source: name,
|
|
311
|
-
replaceEmbeds: (md) => embeds.replaceBlocks(md, name),
|
|
312
|
-
renderMarkdown: (md, base) => renderMarkdown(markdown, md, base),
|
|
313
|
-
}));
|
|
314
|
-
const templates = this.loadTemplates();
|
|
315
|
-
return templates.render(templates.resolve(data.type, "single"), {
|
|
316
|
-
site: { url: DEFAULT_SITE_URL },
|
|
317
|
-
blog,
|
|
318
|
-
article,
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
catch (error) {
|
|
322
|
-
throw new EditorError(400, error.message);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Delete an article and all its files.
|
|
327
|
-
*/
|
|
328
|
-
async remove(type, slug) {
|
|
329
|
-
await this.source(type, slug);
|
|
330
|
-
await rm(this.dir(type, slug), { recursive: true });
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* Save an uploaded file into an article's folder (replacing a file with the same name).
|
|
334
|
-
*/
|
|
335
|
-
async upload(type, slug, fileName, contents) {
|
|
336
|
-
await this.source(type, slug);
|
|
337
|
-
const name = safeFileName(fileName);
|
|
338
|
-
await writeFile(join(this.dir(type, slug), name), contents);
|
|
339
|
-
return { name, url: filesUrl(type, slug) + name };
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* Remember an article's unsaved data for its preview page, under a new ID. Only the newest `MAX_PREVIEWS` are kept,
|
|
343
|
-
* in memory: they're gone when the editor stops. Returns the ID.
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* const id = store.rememberPreview(data, { type: "news", slug: "hello" }); // the editor shows it at /preview/<id>/
|
|
347
|
-
*/
|
|
348
|
-
rememberPreview(data, location) {
|
|
349
|
-
const id = randomUUID();
|
|
350
|
-
this.previews.set(id, { data, location });
|
|
351
|
-
for (const old of [...this.previews.keys()].slice(0, -MAX_PREVIEWS))
|
|
352
|
-
this.previews.delete(old);
|
|
353
|
-
return id;
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Render a remembered preview as the website would (see `preview`), so changes to templates show when it reloads.
|
|
357
|
-
* Throws a 404 EditorError if there is no preview with that ID (e.g. after the editor restarted).
|
|
358
|
-
*
|
|
359
|
-
* @param embeds - renders embed blocks
|
|
360
|
-
*/
|
|
361
|
-
async renderPreview(id, embeds) {
|
|
362
|
-
const preview = this.previews.get(id);
|
|
363
|
-
if (!preview)
|
|
364
|
-
throw new EditorError(404, "Preview not found");
|
|
365
|
-
return this.preview(preview.data, preview.location, embeds);
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* Read one of the static files published at the website's root (the site's `static/`, then the theme's), e.g.
|
|
369
|
-
* `site.css`, for previews. Throws a 404 EditorError if there is no such file, or the path leaves the folder or
|
|
370
|
-
* names a hidden file.
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
* await store.staticFile("fonts/my-font.woff2");
|
|
374
|
-
*/
|
|
375
|
-
async staticFile(path) {
|
|
376
|
-
const parts = path.split("/");
|
|
377
|
-
if (parts.some((part) => part === "" || part.startsWith(".")))
|
|
378
|
-
throw new EditorError(404, "File not found");
|
|
379
|
-
for (const dir of this.staticDirs) {
|
|
380
|
-
try {
|
|
381
|
-
return await readFile(join(dir, ...parts));
|
|
382
|
-
}
|
|
383
|
-
catch {
|
|
384
|
-
// Not in this folder
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
throw new EditorError(404, "File not found");
|
|
388
|
-
}
|
|
389
|
-
/** The static folders (stylesheet, fonts) published at the website's root: the site's, then the theme's */
|
|
390
|
-
get staticDirs() {
|
|
391
|
-
return [join(this.siteDir, "static"), join(this.themeDir, "static")];
|
|
392
|
-
}
|
|
393
|
-
/**
|
|
394
|
-
* Read a file from an article's folder. Throws a 404 EditorError if there is no such file.
|
|
395
|
-
*/
|
|
396
|
-
async file(type, slug, name) {
|
|
397
|
-
// Only plain file names in the article's folder (no sub-folders, parent folders or hidden files)
|
|
398
|
-
if (name === "article.md" ||
|
|
399
|
-
name !== basename(name) ||
|
|
400
|
-
name.startsWith("."))
|
|
401
|
-
throw new EditorError(404, "File not found");
|
|
402
|
-
try {
|
|
403
|
-
return await readFile(join(this.dir(type, slug), name));
|
|
404
|
-
}
|
|
405
|
-
catch (error) {
|
|
406
|
-
if (error instanceof EditorError)
|
|
407
|
-
throw error;
|
|
408
|
-
throw new EditorError(404, "File not found");
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import type { ArticleStatus } from "../../generator/types/index.ts";
|
|
2
|
-
export type { ArticleStatus } from "../../generator/types/index.ts";
|
|
3
|
-
/** Where an article is saved */
|
|
4
|
-
export interface ArticleLocation {
|
|
5
|
-
type: string;
|
|
6
|
-
slug: string;
|
|
7
|
-
}
|
|
8
|
-
/** An article in the editor's article list */
|
|
9
|
-
export interface ArticleSummary {
|
|
10
|
-
type: string;
|
|
11
|
-
slug: string;
|
|
12
|
-
/** The article title, or its folder name if it can't be read */
|
|
13
|
-
title: string;
|
|
14
|
-
status: ArticleStatus;
|
|
15
|
-
/** ISO 8601 date (empty if it can't be read) */
|
|
16
|
-
date: string;
|
|
17
|
-
/** Why the article can't be read, if it can't */
|
|
18
|
-
error?: string | undefined;
|
|
19
|
-
}
|
|
20
|
-
/** An author as edited */
|
|
21
|
-
export interface EditorAuthor {
|
|
22
|
-
name: string;
|
|
23
|
-
email?: string | undefined;
|
|
24
|
-
url?: string | undefined;
|
|
25
|
-
}
|
|
26
|
-
/** An attachment as edited (URLs relative to the article folder, or absolute) */
|
|
27
|
-
export interface EditorAttachment {
|
|
28
|
-
url: string;
|
|
29
|
-
thumbnail?: string | undefined;
|
|
30
|
-
mimeType?: string | undefined;
|
|
31
|
-
name?: string | undefined;
|
|
32
|
-
}
|
|
33
|
-
/** An article's editable data, as saved by the editor */
|
|
34
|
-
export interface EditorArticleData {
|
|
35
|
-
type: string;
|
|
36
|
-
slug: string;
|
|
37
|
-
status: ArticleStatus;
|
|
38
|
-
/** ISO 8601 date */
|
|
39
|
-
date: string;
|
|
40
|
-
title: string;
|
|
41
|
-
/** The template to render the article with (undefined for the default, "single") */
|
|
42
|
-
template?: string | undefined;
|
|
43
|
-
author?: EditorAuthor | undefined;
|
|
44
|
-
excerpt?: string | undefined;
|
|
45
|
-
thumbnail?: string | undefined;
|
|
46
|
-
tags: string[];
|
|
47
|
-
attachments: EditorAttachment[];
|
|
48
|
-
/** The article body as editor HTML (readmore as `<hr class="readmore">`, embeds as `<div class="embed">URL</div>`) */
|
|
49
|
-
bodyHtml: string;
|
|
50
|
-
}
|
|
51
|
-
/** An article as loaded into the editor */
|
|
52
|
-
export interface EditorArticle extends EditorArticleData {
|
|
53
|
-
/** Files in the article's folder (except article.md) */
|
|
54
|
-
files: string[];
|
|
55
|
-
}
|
|
56
|
-
/** The result of uploading a file into an article's folder */
|
|
57
|
-
export interface UploadResult {
|
|
58
|
-
/** The saved file name (relative to the article folder) */
|
|
59
|
-
name: string;
|
|
60
|
-
/** Where the editor can show the file */
|
|
61
|
-
url: string;
|
|
62
|
-
}
|
|
63
|
-
/** A rendered embed for the editor's preview */
|
|
64
|
-
export interface EmbedPreview {
|
|
65
|
-
/** The embed HTML as the website shows it (or a plain link if it can't be embedded) */
|
|
66
|
-
html: string;
|
|
67
|
-
/** Why the URL couldn't be embedded, if it couldn't */
|
|
68
|
-
warnings: string[];
|
|
69
|
-
}
|
|
70
|
-
/** An article type's labels from config.yml */
|
|
71
|
-
export interface ArticleTypeLabels {
|
|
72
|
-
/** e.g. "Article" */
|
|
73
|
-
labelSingle: string;
|
|
74
|
-
/** e.g. "Articles" */
|
|
75
|
-
labelPlural: string;
|
|
76
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|