@lvl99dotcom/edaha.md 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/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +9 -0
- package/dist/cli/index.d.ts +71 -0
- package/dist/cli/index.js +149 -0
- package/dist/embeds/blocks/index.d.ts +59 -0
- package/dist/embeds/blocks/index.js +103 -0
- package/dist/embeds/index.d.ts +143 -0
- package/dist/embeds/index.js +411 -0
- package/dist/embeds/types/index.d.ts +117 -0
- package/dist/embeds/types/index.js +1 -0
- package/dist/generator/article/index.d.ts +151 -0
- package/dist/generator/article/index.js +325 -0
- package/dist/generator/build/index.d.ts +109 -0
- package/dist/generator/build/index.js +363 -0
- package/dist/generator/config/index.d.ts +96 -0
- package/dist/generator/config/index.js +298 -0
- 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 +92 -0
- package/dist/generator/format/index.js +177 -0
- package/dist/generator/lists/index.d.ts +63 -0
- package/dist/generator/lists/index.js +133 -0
- package/dist/generator/markdown/index.d.ts +25 -0
- package/dist/generator/markdown/index.js +62 -0
- 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/slug/index.d.ts +26 -0
- package/dist/generator/slug/index.js +57 -0
- package/dist/generator/templates/index.d.ts +88 -0
- package/dist/generator/templates/index.js +175 -0
- package/dist/generator/types/index.d.ts +186 -0
- package/dist/generator/types/index.js +1 -0
- package/dist/generator/urls/index.d.ts +50 -0
- package/dist/generator/urls/index.js +77 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +9 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/index.js +9 -0
- package/package.json +64 -3
- package/theme/static/base.css +328 -0
- package/theme/templates/article/list.njk +69 -0
- package/theme/templates/article/single.njk +64 -0
- package/theme/templates/layouts/html.njk +72 -0
- package/theme/templates/layouts/rssfeed.njk +28 -0
- package/theme/templates/page/home.njk +16 -0
- package/theme/templates/page/single.njk +18 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/** Operators comparing with one value */
|
|
2
|
+
const OPERATORS = ["=", "!=", ">", ">=", "<", "<="];
|
|
3
|
+
/** Operators comparing with a list of values */
|
|
4
|
+
const LIST_OPERATORS = ["in", "not-in"];
|
|
5
|
+
/** Whether a value can be compared by a condition */
|
|
6
|
+
const isConditionValue = (value) => value === null || ["string", "number", "boolean"].includes(typeof value);
|
|
7
|
+
/** Whether a value is one condition (rather than a list of groups) */
|
|
8
|
+
const isCondition = (value) => Array.isArray(value) && typeof value[0] === "string";
|
|
9
|
+
/** Whether conditions are a list of conditions and groups (rather than one condition or group) */
|
|
10
|
+
const isConditionList = (value) => Array.isArray(value) && !isCondition(value);
|
|
11
|
+
/**
|
|
12
|
+
* Check a field's `showWhen` or `hideWhen` is made of conditions and `and`/`or` groups. Throws an Error naming where
|
|
13
|
+
* the problem is.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* checkConditions({ or: [["layout", "=", "slim"], ["layout", "=", "large"]] }, '"showWhen"');
|
|
17
|
+
*/
|
|
18
|
+
export const checkConditions = (value, what) => {
|
|
19
|
+
if (isCondition(value)) {
|
|
20
|
+
const [path, operator, compared] = value;
|
|
21
|
+
const valid = value.length === 3 &&
|
|
22
|
+
path !== "" &&
|
|
23
|
+
(OPERATORS.includes(operator)
|
|
24
|
+
? isConditionValue(compared)
|
|
25
|
+
: LIST_OPERATORS.includes(operator) &&
|
|
26
|
+
Array.isArray(compared) &&
|
|
27
|
+
compared.every(isConditionValue));
|
|
28
|
+
if (!valid)
|
|
29
|
+
throw new Error(`${what} must be a condition like ["layout", "=", "large"] (operators: ${[...OPERATORS, ...LIST_OPERATORS].join(" ")}; "in" and "not-in" compare with a list of values), got ${JSON.stringify(value)}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
value.forEach((item, index) => checkConditions(item, `${what}.${index}`));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const keys = value && typeof value === "object" ? Object.keys(value) : undefined;
|
|
37
|
+
if (keys?.length !== 1 || !["and", "or"].includes(keys[0]))
|
|
38
|
+
throw new Error(`${what} must be a condition, or a group with one "and" or "or" list of conditions, got ${JSON.stringify(value)}`);
|
|
39
|
+
checkConditions(value[keys[0]], `${what}.${keys[0]}`);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Check an `$edahaField`'s `defaultValue`, `showWhen` and `hideWhen`. Throws an Error naming where the problem is.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* checkFieldExtensions({ select: true, defaultValue: "large" }, '"metadata.layout.$edahaField"');
|
|
46
|
+
*/
|
|
47
|
+
export const checkFieldExtensions = (field, what) => {
|
|
48
|
+
if (field.defaultValue !== undefined && !isConditionValue(field.defaultValue))
|
|
49
|
+
throw new Error(`${what}.defaultValue must be text, a number, true, false or null`);
|
|
50
|
+
for (const name of ["showWhen", "hideWhen"])
|
|
51
|
+
if (field[name] !== undefined)
|
|
52
|
+
checkConditions(field[name], `${what}.${name}`);
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The value at a dot path inside values, e.g. "credit.name" or "images.0".
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* valueAt({ credit: { name: "Ann" } }, "credit.name"); // "Ann"
|
|
59
|
+
*/
|
|
60
|
+
export const valueAt = (values, path) => path
|
|
61
|
+
.split(".")
|
|
62
|
+
.reduce((value, key) => value && typeof value === "object"
|
|
63
|
+
? value[key]
|
|
64
|
+
: undefined, values);
|
|
65
|
+
/** Compare a value with one value: equal (a missing value equals null), or in order (numbers or text only) */
|
|
66
|
+
const compare = (value, operator, compared) => {
|
|
67
|
+
const equal = value === compared || (value ?? null) === compared;
|
|
68
|
+
if (operator === "=")
|
|
69
|
+
return equal;
|
|
70
|
+
if (operator === "!=")
|
|
71
|
+
return !equal;
|
|
72
|
+
if (typeof value !== typeof compared ||
|
|
73
|
+
!["number", "string"].includes(typeof value))
|
|
74
|
+
return false;
|
|
75
|
+
const [a, b] = [value, compared];
|
|
76
|
+
return operator === ">"
|
|
77
|
+
? a > b
|
|
78
|
+
: operator === ">="
|
|
79
|
+
? a >= b
|
|
80
|
+
: operator === "<"
|
|
81
|
+
? a < b
|
|
82
|
+
: a <= b;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Whether values match conditions: one condition, `and` (all match) or `or` (any match) groups, or a list (all match).
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* matchesConditions({ layout: "slim" }, { or: [["layout", "=", "slim"], ["layout", "=", "large"]] }); // true
|
|
89
|
+
* matchesConditions({ layout: "slim" }, ["layout", "in", ["large"]]); // false
|
|
90
|
+
*/
|
|
91
|
+
export const matchesConditions = (values, conditions) => {
|
|
92
|
+
if (isConditionList(conditions))
|
|
93
|
+
return conditions.every((item) => matchesConditions(values, item));
|
|
94
|
+
if (isCondition(conditions)) {
|
|
95
|
+
const [path, operator, compared] = conditions;
|
|
96
|
+
const value = valueAt(values, path);
|
|
97
|
+
if (operator !== "in" && operator !== "not-in")
|
|
98
|
+
return compare(value, operator, compared);
|
|
99
|
+
const included = compared.some((item) => compare(value, "=", item));
|
|
100
|
+
return operator === "in" ? included : !included;
|
|
101
|
+
}
|
|
102
|
+
if ("and" in conditions)
|
|
103
|
+
return matchesConditions(values, conditions.and);
|
|
104
|
+
const any = conditions.or;
|
|
105
|
+
return isConditionList(any)
|
|
106
|
+
? any.some((item) => matchesConditions(values, item))
|
|
107
|
+
: matchesConditions(values, any);
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Whether a field shows: its `showWhen` matches (or it has none), and its `hideWhen` doesn't (or it has none).
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* fieldShown({ checkbox: true, showWhen: ["layout", "=", "large"] }, { layout: "slim" }); // false
|
|
114
|
+
*/
|
|
115
|
+
export const fieldShown = (field, values) => (!field?.showWhen || matchesConditions(values, field.showWhen)) &&
|
|
116
|
+
!(field?.hideWhen && matchesConditions(values, field.hideWhen));
|
|
117
|
+
/** Every field in a map of fields, a group (map) before the fields in it; lists' items aren't included */
|
|
118
|
+
const fieldsIn = (schemas, parent = []) => Object.entries(schemas).flatMap(([name, schema]) => {
|
|
119
|
+
const field = { path: [...parent, name], schema };
|
|
120
|
+
return schema.type === "object" && schema.properties
|
|
121
|
+
? [field, ...fieldsIn(schema.properties, field.path)]
|
|
122
|
+
: [field];
|
|
123
|
+
});
|
|
124
|
+
/** Set (or, for undefined, leave out) a value in a map at a path of keys, without changing the original */
|
|
125
|
+
const withValueAt = (values, [key, ...rest], value) => {
|
|
126
|
+
const current = values[key];
|
|
127
|
+
const next = rest.length
|
|
128
|
+
? withValueAt(current && typeof current === "object"
|
|
129
|
+
? current
|
|
130
|
+
: {}, rest, value)
|
|
131
|
+
: value;
|
|
132
|
+
return next === undefined
|
|
133
|
+
? Object.fromEntries(Object.entries(values).filter(([name]) => name !== key))
|
|
134
|
+
: { ...values, [key]: next };
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Apply fields' `defaultValue`, `showWhen` and `hideWhen` to values: shown fields without a value get their default,
|
|
138
|
+
* and hidden fields' values (and those of fields in a hidden group) are left out. Conditions are checked again until
|
|
139
|
+
* nothing changes, as hiding or filling in one value can show or hide another. The values are in the fields' order,
|
|
140
|
+
* then any values without a field.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* resolveFields(
|
|
144
|
+
* { layout: { type: "string", $edahaField: { select: true, defaultValue: "large" } },
|
|
145
|
+
* tracklist: { type: "boolean", $edahaField: { checkbox: true, hideWhen: ["layout", "=", "slim"] } } },
|
|
146
|
+
* { tracklist: true },
|
|
147
|
+
* ); // { values: { layout: "large", tracklist: true }, hidden: Set {} }
|
|
148
|
+
*/
|
|
149
|
+
export const resolveFields = (schemas, values) => {
|
|
150
|
+
const fields = fieldsIn(schemas);
|
|
151
|
+
/** The values with some fields hidden, and the others' defaults filled in */
|
|
152
|
+
const apply = (hidden) => fields.reduce((result, { path, schema }) => {
|
|
153
|
+
const key = path.join(".");
|
|
154
|
+
const { defaultValue } = (schema.$edahaField ??
|
|
155
|
+
{});
|
|
156
|
+
if (hidden.has(key))
|
|
157
|
+
return valueAt(result, key) === undefined
|
|
158
|
+
? result
|
|
159
|
+
: withValueAt(result, path, undefined);
|
|
160
|
+
return defaultValue !== undefined && valueAt(result, key) === undefined
|
|
161
|
+
? withValueAt(result, path, defaultValue)
|
|
162
|
+
: result;
|
|
163
|
+
}, values);
|
|
164
|
+
/** The fields hidden by the conditions, checked on the values (with a hidden group's fields hidden too) */
|
|
165
|
+
const hiddenBy = (current) => fields.reduce((hidden, { path, schema }) => {
|
|
166
|
+
if (hidden.has(path.slice(0, -1).join(".")) ||
|
|
167
|
+
!fieldShown(schema.$edahaField, current))
|
|
168
|
+
hidden.add(path.join("."));
|
|
169
|
+
return hidden;
|
|
170
|
+
}, new Set());
|
|
171
|
+
// Each round starts again from the given values, so a value hidden in one round can show in the next. It stops when
|
|
172
|
+
// the same fields are hidden twice; the limit stops conditions that contradict each other going on forever.
|
|
173
|
+
let hidden = new Set();
|
|
174
|
+
let current = apply(hidden);
|
|
175
|
+
for (let round = 0; round <= fields.length; round++) {
|
|
176
|
+
const next = hiddenBy(current);
|
|
177
|
+
if ([...next].join("\0") === [...hidden].join("\0"))
|
|
178
|
+
break;
|
|
179
|
+
hidden = next;
|
|
180
|
+
current = apply(hidden);
|
|
181
|
+
}
|
|
182
|
+
// In the fields' order, then any other values
|
|
183
|
+
const order = Object.keys(schemas);
|
|
184
|
+
const ordered = Object.fromEntries(Object.entries(current).sort(([a], [b]) => (order.indexOf(a) + 1 || Infinity) - (order.indexOf(b) + 1 || Infinity)));
|
|
185
|
+
return { values: ordered, hidden };
|
|
186
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format a number of bytes as a human-readable size (1 KB = 1024 bytes).
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* formatBytes(204800); // "200 KB"
|
|
6
|
+
* formatBytes(2621440); // "2.5 MB"
|
|
7
|
+
*/
|
|
8
|
+
export declare const formatBytes: (bytes: number) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Format an ISO 8601 date as a human-readable date (UTC).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* formatDate("2026-10-19T09:54:00.000Z"); // "19 October 2026"
|
|
14
|
+
*/
|
|
15
|
+
export declare const formatDate: (iso: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Format an ISO 8601 date for RSS feeds (RFC 822).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* formatRfc822("2026-10-19T09:54:00.000Z"); // "Mon, 19 Oct 2026 09:54:00 GMT"
|
|
21
|
+
*/
|
|
22
|
+
export declare const formatRfc822: (iso: string) => string;
|
|
23
|
+
/**
|
|
24
|
+
* Escape text for use in HTML content or attribute values.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* escapeHtml('<a href="x">'); // "<a href="x">"
|
|
28
|
+
*/
|
|
29
|
+
export declare const escapeHtml: (text: string) => string;
|
|
30
|
+
/**
|
|
31
|
+
* Convert HTML to plain text: remove tags, decode common entities and collapse whitespace.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* stripHtml("<p>Fish & <em>chips</em></p>"); // "Fish & chips"
|
|
35
|
+
*/
|
|
36
|
+
export declare const stripHtml: (html: string) => string;
|
|
37
|
+
/**
|
|
38
|
+
* Crop text to a maximum length, ending at a word boundary where possible, and add an ellipsis if it was cropped.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* cropText("Hello wonderful world", 15); // "Hello wonderful…"
|
|
42
|
+
*/
|
|
43
|
+
export declare const cropText: (text: string, maxLength: number) => string;
|
|
44
|
+
/**
|
|
45
|
+
* Whether a URL is relative to the current folder (not absolute, root-relative or a fragment).
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* isRelativeUrl("thumb.jpg"); // true
|
|
49
|
+
* isRelativeUrl("https://example.com/a.jpg"); // false
|
|
50
|
+
* isRelativeUrl("/news/"); // false
|
|
51
|
+
*/
|
|
52
|
+
export declare const isRelativeUrl: (url: string) => boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Blank out Markdown's code blocks and inline code with spaces, keeping every other character where it was, so
|
|
55
|
+
* markers like `<!-- readmore -->` can be found outside code only.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* maskCode("Intro `<!-- readmore -->` rest"); // "Intro rest"
|
|
59
|
+
*/
|
|
60
|
+
export declare const maskCode: (markdown: string) => string;
|
|
61
|
+
/**
|
|
62
|
+
* Replace a pattern's matches in Markdown, except in code blocks and inline code. The replacer gets each match as
|
|
63
|
+
* `exec` gives it (from the original text, groups included).
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* replaceOutsideCode("a <!-- x --> `<!-- x -->`", /<!-- x -->/g, () => "X"); // "a X `<!-- x -->`"
|
|
67
|
+
*/
|
|
68
|
+
export declare const replaceOutsideCode: (markdown: string, pattern: RegExp, replacer: (match: RegExpExecArray) => string) => string;
|
|
69
|
+
/** A `<!-- raw -->...<!-- /raw -->` block, whose content is shown exactly as it is written */
|
|
70
|
+
export declare const RAW_BLOCK: RegExp;
|
|
71
|
+
/** A raw block's content, and whether it is a block (on its own lines) or inline (within a line) */
|
|
72
|
+
export interface RawContent {
|
|
73
|
+
content: string;
|
|
74
|
+
block: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Replace every raw block outside code in Markdown. A raw block on its own lines is a block, without the line breaks
|
|
78
|
+
* after its opening and before its closing marker; one within a line is inline.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* replaceRawBlocks("<!-- raw -->\n*a*\n<!-- /raw -->", ({ content }) => content); // "*a*"
|
|
82
|
+
*/
|
|
83
|
+
export declare const replaceRawBlocks: (markdown: string, replacer: (raw: RawContent) => string) => string;
|
|
84
|
+
/**
|
|
85
|
+
* The HTML that shows a raw block's content exactly as it is written: in `<pre class="raw">` for a block (which keeps
|
|
86
|
+
* its spaces and line breaks whatever the stylesheet), or `<span class="raw">` inline, on one line. Line breaks and punctuation are written as character references, so
|
|
87
|
+
* Markdown, shortcodes and typography never act on it.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* rawHtml({ content: "*a*", block: false }); // '<span class="raw">*a*</span>'
|
|
91
|
+
*/
|
|
92
|
+
export declare const rawHtml: ({ content, block }: RawContent) => string;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/** Units for human-readable file sizes */
|
|
2
|
+
const SIZE_UNITS = ["bytes", "KB", "MB", "GB"];
|
|
3
|
+
/**
|
|
4
|
+
* Format a number of bytes as a human-readable size (1 KB = 1024 bytes).
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* formatBytes(204800); // "200 KB"
|
|
8
|
+
* formatBytes(2621440); // "2.5 MB"
|
|
9
|
+
*/
|
|
10
|
+
export const formatBytes = (bytes) => {
|
|
11
|
+
let size = bytes;
|
|
12
|
+
let unit = 0;
|
|
13
|
+
while (size >= 1024 && unit < SIZE_UNITS.length - 1) {
|
|
14
|
+
size /= 1024;
|
|
15
|
+
unit++;
|
|
16
|
+
}
|
|
17
|
+
const rounded = unit === 0 ? size : Math.round(size * 10) / 10;
|
|
18
|
+
return `${rounded} ${SIZE_UNITS[unit]}`;
|
|
19
|
+
};
|
|
20
|
+
const DATE_FORMAT = new Intl.DateTimeFormat("en-GB", {
|
|
21
|
+
day: "numeric",
|
|
22
|
+
month: "long",
|
|
23
|
+
year: "numeric",
|
|
24
|
+
timeZone: "UTC",
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Format an ISO 8601 date as a human-readable date (UTC).
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* formatDate("2026-10-19T09:54:00.000Z"); // "19 October 2026"
|
|
31
|
+
*/
|
|
32
|
+
export const formatDate = (iso) => DATE_FORMAT.format(new Date(iso));
|
|
33
|
+
/**
|
|
34
|
+
* Format an ISO 8601 date for RSS feeds (RFC 822).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* formatRfc822("2026-10-19T09:54:00.000Z"); // "Mon, 19 Oct 2026 09:54:00 GMT"
|
|
38
|
+
*/
|
|
39
|
+
export const formatRfc822 = (iso) => new Date(iso).toUTCString();
|
|
40
|
+
/**
|
|
41
|
+
* Escape text for use in HTML content or attribute values.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* escapeHtml('<a href="x">'); // "<a href="x">"
|
|
45
|
+
*/
|
|
46
|
+
export const escapeHtml = (text) => text
|
|
47
|
+
.replace(/&/g, "&")
|
|
48
|
+
.replace(/</g, "<")
|
|
49
|
+
.replace(/>/g, ">")
|
|
50
|
+
.replace(/"/g, """)
|
|
51
|
+
.replace(/'/g, "'");
|
|
52
|
+
/** Named HTML entities decoded by `stripHtml` */
|
|
53
|
+
const ENTITIES = {
|
|
54
|
+
amp: "&",
|
|
55
|
+
lt: "<",
|
|
56
|
+
gt: ">",
|
|
57
|
+
quot: '"',
|
|
58
|
+
apos: "'",
|
|
59
|
+
nbsp: " ",
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Convert HTML to plain text: remove tags, decode common entities and collapse whitespace.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* stripHtml("<p>Fish & <em>chips</em></p>"); // "Fish & chips"
|
|
66
|
+
*/
|
|
67
|
+
export const stripHtml = (html) => html
|
|
68
|
+
.replace(/<[^>]*>/g, " ")
|
|
69
|
+
.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (entity, code) => {
|
|
70
|
+
if (code[0] === "#") {
|
|
71
|
+
const point = code[1] === "x" || code[1] === "X"
|
|
72
|
+
? parseInt(code.slice(2), 16)
|
|
73
|
+
: parseInt(code.slice(1), 10);
|
|
74
|
+
return String.fromCodePoint(point);
|
|
75
|
+
}
|
|
76
|
+
return ENTITIES[code.toLowerCase()] ?? entity;
|
|
77
|
+
})
|
|
78
|
+
.replace(/\s+/g, " ")
|
|
79
|
+
.trim();
|
|
80
|
+
/**
|
|
81
|
+
* Crop text to a maximum length, ending at a word boundary where possible, and add an ellipsis if it was cropped.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* cropText("Hello wonderful world", 15); // "Hello wonderful…"
|
|
85
|
+
*/
|
|
86
|
+
export const cropText = (text, maxLength) => {
|
|
87
|
+
if (text.length <= maxLength)
|
|
88
|
+
return text;
|
|
89
|
+
// Include the next character, so a word ending exactly at the limit is kept whole
|
|
90
|
+
const lastSpace = text.slice(0, maxLength + 1).lastIndexOf(" ");
|
|
91
|
+
const cropped = lastSpace > 0 ? text.slice(0, lastSpace) : text.slice(0, maxLength);
|
|
92
|
+
return `${cropped.trimEnd()}…`;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Whether a URL is relative to the current folder (not absolute, root-relative or a fragment).
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* isRelativeUrl("thumb.jpg"); // true
|
|
99
|
+
* isRelativeUrl("https://example.com/a.jpg"); // false
|
|
100
|
+
* isRelativeUrl("/news/"); // false
|
|
101
|
+
*/
|
|
102
|
+
export const isRelativeUrl = (url) => url !== "" && !/^([a-z][a-z0-9+.-]*:|\/|#|\?)/i.test(url);
|
|
103
|
+
/**
|
|
104
|
+
* Fenced code blocks (``` or ~~~, to their closing fence or the end of the text) and inline code spans in Markdown
|
|
105
|
+
*/
|
|
106
|
+
const MARKDOWN_CODE = /^ {0,3}(?<fence>`{3,}|~{3,})[^\n]*\n[\s\S]*?(?:^ {0,3}\k<fence>[`~]*[ \t]*$|(?![\s\S]))|(?<ticks>`+)(?:(?!\k<ticks>)[\s\S])+?\k<ticks>(?!`)/gm;
|
|
107
|
+
/**
|
|
108
|
+
* Blank out Markdown's code blocks and inline code with spaces, keeping every other character where it was, so
|
|
109
|
+
* markers like `<!-- readmore -->` can be found outside code only.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* maskCode("Intro `<!-- readmore -->` rest"); // "Intro rest"
|
|
113
|
+
*/
|
|
114
|
+
export const maskCode = (markdown) => markdown.replace(MARKDOWN_CODE, (code) => " ".repeat(code.length));
|
|
115
|
+
/**
|
|
116
|
+
* Replace a pattern's matches in Markdown, except in code blocks and inline code. The replacer gets each match as
|
|
117
|
+
* `exec` gives it (from the original text, groups included).
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* replaceOutsideCode("a <!-- x --> `<!-- x -->`", /<!-- x -->/g, () => "X"); // "a X `<!-- x -->`"
|
|
121
|
+
*/
|
|
122
|
+
export const replaceOutsideCode = (markdown, pattern, replacer) => {
|
|
123
|
+
const single = new RegExp(pattern.source, pattern.flags.replace("g", ""));
|
|
124
|
+
let result = "";
|
|
125
|
+
let last = 0;
|
|
126
|
+
for (const { index, 0: text } of maskCode(markdown).matchAll(new RegExp(single.source, `${single.flags}g`))) {
|
|
127
|
+
const match = single.exec(markdown.slice(index, index + text.length));
|
|
128
|
+
result += markdown.slice(last, index) + replacer(match);
|
|
129
|
+
last = index + text.length;
|
|
130
|
+
}
|
|
131
|
+
return result + markdown.slice(last);
|
|
132
|
+
};
|
|
133
|
+
/** A `<!-- raw -->...<!-- /raw -->` block, whose content is shown exactly as it is written */
|
|
134
|
+
export const RAW_BLOCK = /<!--\s*raw\s*-->(?<rawContent>[\s\S]*?)<!--\s*\/raw\s*-->/;
|
|
135
|
+
/**
|
|
136
|
+
* Raw blocks and code, whichever starts first: code can show raw markers as they are, and raw blocks can hold code
|
|
137
|
+
*/
|
|
138
|
+
const RAW_OR_CODE = new RegExp(`(?<raw>${RAW_BLOCK.source})|${MARKDOWN_CODE.source}`, "gm");
|
|
139
|
+
/**
|
|
140
|
+
* Replace every raw block outside code in Markdown. A raw block on its own lines is a block, without the line breaks
|
|
141
|
+
* after its opening and before its closing marker; one within a line is inline.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* replaceRawBlocks("<!-- raw -->\n*a*\n<!-- /raw -->", ({ content }) => content); // "*a*"
|
|
145
|
+
*/
|
|
146
|
+
export const replaceRawBlocks = (markdown, replacer) => markdown.replace(RAW_OR_CODE, (...args) => {
|
|
147
|
+
const match = args[0];
|
|
148
|
+
const groups = args.at(-1);
|
|
149
|
+
if (groups.raw === undefined)
|
|
150
|
+
return match;
|
|
151
|
+
const offset = args.at(-3);
|
|
152
|
+
const block = /(?:^|\n)[ \t]*$/.test(markdown.slice(0, offset)) &&
|
|
153
|
+
/^[ \t]*(?:\r?\n|$)/.test(markdown.slice(offset + match.length));
|
|
154
|
+
const content = groups.rawContent;
|
|
155
|
+
return replacer({
|
|
156
|
+
block,
|
|
157
|
+
content: block
|
|
158
|
+
? content.replace(/^[ \t]*\r?\n/, "").replace(/\r?\n[ \t]*$/, "")
|
|
159
|
+
: content,
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
/**
|
|
163
|
+
* The HTML that shows a raw block's content exactly as it is written: in `<pre class="raw">` for a block (which keeps
|
|
164
|
+
* its spaces and line breaks whatever the stylesheet), or `<span class="raw">` inline, on one line. Line breaks and punctuation are written as character references, so
|
|
165
|
+
* Markdown, shortcodes and typography never act on it.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* rawHtml({ content: "*a*", block: false }); // '<span class="raw">*a*</span>'
|
|
169
|
+
*/
|
|
170
|
+
export const rawHtml = ({ content, block }) => {
|
|
171
|
+
const tag = block ? "pre" : "span";
|
|
172
|
+
const text = content
|
|
173
|
+
.replace(/\r\n?/g, "\n")
|
|
174
|
+
.replace(/[\n!-/:-@[-`{-~]/g, (char) => `&#${char.charCodeAt(0)};`);
|
|
175
|
+
const html = `<${tag} class="raw">${text}</${tag}>`;
|
|
176
|
+
return block ? `\n\n${html}\n\n` : html;
|
|
177
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ListKind, TemplateDataArticle, TemplateDataSite, TemplateDataList } from "../types/index.ts";
|
|
2
|
+
/** Articles per list page */
|
|
3
|
+
export declare const PAGE_SIZE = 20;
|
|
4
|
+
/** A list of articles before pagination */
|
|
5
|
+
export interface ListDefinition {
|
|
6
|
+
kind: ListKind;
|
|
7
|
+
/** The list's name */
|
|
8
|
+
name: string;
|
|
9
|
+
/** The article type/tag/author slug */
|
|
10
|
+
slug: string;
|
|
11
|
+
/** The article type the list belongs to (its list template renders it) */
|
|
12
|
+
type: string;
|
|
13
|
+
/** The list's folder relative to the website's root, e.g. "news/" or "news/tags/art/" */
|
|
14
|
+
dir: string;
|
|
15
|
+
/** The URL of the list's first page */
|
|
16
|
+
url: string;
|
|
17
|
+
/** Whether list pages are built (otherwise only the feed, or nothing) */
|
|
18
|
+
pages: boolean;
|
|
19
|
+
/** The articles on the list's pages, newest first */
|
|
20
|
+
articles: TemplateDataArticle[];
|
|
21
|
+
/** Whether a feed is built */
|
|
22
|
+
feed: boolean;
|
|
23
|
+
/** The articles in the list's feed, newest first */
|
|
24
|
+
feedArticles: TemplateDataArticle[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Sort articles newest first (by type and slug when dates are equal, so builds are stable).
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* sortArticles(articles)[0]; // the newest article
|
|
31
|
+
*/
|
|
32
|
+
export declare const sortArticles: (articles: readonly TemplateDataArticle[]) => TemplateDataArticle[];
|
|
33
|
+
/**
|
|
34
|
+
* Group published articles into lists, following each article type's `generate` settings (see config.ts):
|
|
35
|
+
* - one per type (its pages unless the type has an index article, and its feed)
|
|
36
|
+
* - one per tag and per author within a type, with articles of types with `tag` or `author` (and in their feeds,
|
|
37
|
+
* also `feed`)
|
|
38
|
+
*
|
|
39
|
+
* Every type in `withIndex` gets a type list (even without articles or list settings), so its index article can
|
|
40
|
+
* show it; build only the lists with `pages` or `feed`.
|
|
41
|
+
*
|
|
42
|
+
* @param articles - published articles, without index articles
|
|
43
|
+
* @param withIndex - article types that have an index article
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* collectLists(published, site, new Set()).map((list) => list.url); // ["/news/", "/news/tags/art/", ...]
|
|
47
|
+
*/
|
|
48
|
+
export declare const collectLists: (articles: readonly TemplateDataArticle[], site: TemplateDataSite, withIndex: ReadonlySet<string>) => ListDefinition[];
|
|
49
|
+
/**
|
|
50
|
+
* The output file name of a list page.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* pageFile(1); // "index.html"
|
|
54
|
+
* pageFile(2); // "2.html"
|
|
55
|
+
*/
|
|
56
|
+
export declare const pageFile: (index: number) => string;
|
|
57
|
+
/**
|
|
58
|
+
* Split a list into pages of template data (at least one page, even when empty).
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* paginate(list).map((page) => page.pagination.page.url); // ["/news/", "/news/2"]
|
|
62
|
+
*/
|
|
63
|
+
export declare const paginate: (list: ListDefinition, pageSize?: number) => TemplateDataList[];
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/** Articles per list page */
|
|
2
|
+
export const PAGE_SIZE = 20;
|
|
3
|
+
/**
|
|
4
|
+
* Sort articles newest first (by type and slug when dates are equal, so builds are stable).
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* sortArticles(articles)[0]; // the newest article
|
|
8
|
+
*/
|
|
9
|
+
export const sortArticles = (articles) => [...articles].sort((a, b) => b.date.localeCompare(a.date) ||
|
|
10
|
+
a.type.localeCompare(b.type) ||
|
|
11
|
+
a.slug.localeCompare(b.slug));
|
|
12
|
+
/**
|
|
13
|
+
* Group published articles into lists, following each article type's `generate` settings (see config.ts):
|
|
14
|
+
* - one per type (its pages unless the type has an index article, and its feed)
|
|
15
|
+
* - one per tag and per author within a type, with articles of types with `tag` or `author` (and in their feeds,
|
|
16
|
+
* also `feed`)
|
|
17
|
+
*
|
|
18
|
+
* Every type in `withIndex` gets a type list (even without articles or list settings), so its index article can
|
|
19
|
+
* show it; build only the lists with `pages` or `feed`.
|
|
20
|
+
*
|
|
21
|
+
* @param articles - published articles, without index articles
|
|
22
|
+
* @param withIndex - article types that have an index article
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* collectLists(published, site, new Set()).map((list) => list.url); // ["/news/", "/news/tags/art/", ...]
|
|
26
|
+
*/
|
|
27
|
+
export const collectLists = (articles, site, withIndex) => {
|
|
28
|
+
const lists = new Map();
|
|
29
|
+
/** Get or create a list */
|
|
30
|
+
const list = (definition) => {
|
|
31
|
+
const existing = lists.get(definition.dir);
|
|
32
|
+
if (existing)
|
|
33
|
+
return existing;
|
|
34
|
+
const created = {
|
|
35
|
+
...definition,
|
|
36
|
+
url: `${site.baseUrl}/${definition.dir}`,
|
|
37
|
+
articles: [],
|
|
38
|
+
feedArticles: [],
|
|
39
|
+
};
|
|
40
|
+
lists.set(definition.dir, created);
|
|
41
|
+
return created;
|
|
42
|
+
};
|
|
43
|
+
/** A type's own list */
|
|
44
|
+
const typeList = (type) => {
|
|
45
|
+
const config = site.types[type];
|
|
46
|
+
return list({
|
|
47
|
+
kind: "type",
|
|
48
|
+
name: config.labelPlural,
|
|
49
|
+
slug: type,
|
|
50
|
+
type,
|
|
51
|
+
dir: config.url.slice(site.baseUrl.length + 1),
|
|
52
|
+
// An index article takes the place of the list's pages
|
|
53
|
+
pages: config.generate.list && !withIndex.has(type),
|
|
54
|
+
feed: false,
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
for (const type of withIndex)
|
|
58
|
+
typeList(type);
|
|
59
|
+
for (const article of sortArticles(articles)) {
|
|
60
|
+
const config = site.types[article.type];
|
|
61
|
+
const settings = config.generate;
|
|
62
|
+
/** Add the article to a list, and to its feed if the type allows feeds */
|
|
63
|
+
const add = (entry) => {
|
|
64
|
+
entry.articles.push(article);
|
|
65
|
+
if (settings.feed)
|
|
66
|
+
entry.feedArticles.push(article);
|
|
67
|
+
entry.feed ||= settings.feed;
|
|
68
|
+
};
|
|
69
|
+
if (settings.list || settings.feed || withIndex.has(article.type))
|
|
70
|
+
add(typeList(article.type));
|
|
71
|
+
const typeDir = config.url.slice(site.baseUrl.length + 1);
|
|
72
|
+
if (settings.tag)
|
|
73
|
+
for (const tag of article.tags)
|
|
74
|
+
add(list({
|
|
75
|
+
kind: "tag",
|
|
76
|
+
name: tag.name,
|
|
77
|
+
slug: tag.slug,
|
|
78
|
+
type: article.type,
|
|
79
|
+
dir: `${typeDir}tags/${tag.slug}/`,
|
|
80
|
+
pages: true,
|
|
81
|
+
feed: false,
|
|
82
|
+
}));
|
|
83
|
+
// Articles without an author aren't in any author's list
|
|
84
|
+
if (settings.author && article.author)
|
|
85
|
+
add(list({
|
|
86
|
+
kind: "author",
|
|
87
|
+
name: article.author.name,
|
|
88
|
+
slug: article.author.slug,
|
|
89
|
+
type: article.type,
|
|
90
|
+
dir: `${typeDir}authors/${article.author.slug}/`,
|
|
91
|
+
pages: true,
|
|
92
|
+
feed: false,
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
return [...lists.values()];
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* The output file name of a list page.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* pageFile(1); // "index.html"
|
|
102
|
+
* pageFile(2); // "2.html"
|
|
103
|
+
*/
|
|
104
|
+
export const pageFile = (index) => index === 1 ? "index.html" : `${index}.html`;
|
|
105
|
+
/**
|
|
106
|
+
* Split a list into pages of template data (at least one page, even when empty).
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* paginate(list).map((page) => page.pagination.page.url); // ["/news/", "/news/2"]
|
|
110
|
+
*/
|
|
111
|
+
export const paginate = (list, pageSize = PAGE_SIZE) => {
|
|
112
|
+
const count = Math.max(1, Math.ceil(list.articles.length / pageSize));
|
|
113
|
+
const pageUrl = (index) => index === 1 ? list.url : `${list.url}${index}`;
|
|
114
|
+
return Array.from({ length: count }, (_, i) => {
|
|
115
|
+
const pages = Array.from({ length: count }, (_, p) => ({ index: p + 1, url: pageUrl(p + 1), isCurrent: p === i }));
|
|
116
|
+
return {
|
|
117
|
+
name: list.name,
|
|
118
|
+
slug: list.slug,
|
|
119
|
+
type: list.type,
|
|
120
|
+
kind: list.kind,
|
|
121
|
+
url: list.url,
|
|
122
|
+
feedUrl: `${list.url}feed.xml`,
|
|
123
|
+
hasFeed: list.feed,
|
|
124
|
+
articles: list.articles.slice(i * pageSize, (i + 1) * pageSize),
|
|
125
|
+
pagination: {
|
|
126
|
+
pages,
|
|
127
|
+
page: pages[i],
|
|
128
|
+
prevPage: pages[i - 1],
|
|
129
|
+
nextPage: pages[i + 1],
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import MarkdownIt from "markdown-it";
|
|
2
|
+
/** A configured markdown renderer */
|
|
3
|
+
export type Markdown = InstanceType<typeof MarkdownIt>;
|
|
4
|
+
/**
|
|
5
|
+
* Create the markdown renderer. Raw HTML is allowed, as articles are written by trusted authors and embeds are HTML.
|
|
6
|
+
* Relative link and image URLs are resolved against the article's URL, so they work on any page (e.g. lists), and
|
|
7
|
+
* root-relative ones (e.g. `/docs/`) are put under the website's base URL.
|
|
8
|
+
* Headings get IDs from their text (see `headingIds`), so they can be linked to, e.g. `#this-is-an-example`.
|
|
9
|
+
* Typographer (curly quotes, dashes) is on for the website and off for the editor, so text round-trips unchanged.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const markdown = createMarkdown();
|
|
13
|
+
* renderMarkdown(markdown, "", "/news/article/hello/"); // '<p><img src="/news/article/hello/a.png" alt="A"></p>\n'
|
|
14
|
+
*/
|
|
15
|
+
export declare const createMarkdown: ({ typographer }?: {
|
|
16
|
+
typographer?: boolean | undefined;
|
|
17
|
+
}) => Markdown;
|
|
18
|
+
/**
|
|
19
|
+
* Render markdown to HTML, resolving relative URLs against a folder URL and putting root-relative ones under the
|
|
20
|
+
* website's base URL.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* renderMarkdown(markdown, "[Docs](/docs/)", "/edaha.md/news/hello/", "/edaha.md"); // '<p><a href="/edaha.md/docs/">Docs</a></p>\n'
|
|
24
|
+
*/
|
|
25
|
+
export declare const renderMarkdown: (markdown: Markdown, source: string, baseUrl: string, siteBaseUrl?: string) => string;
|