domma-cms 1.60.0 → 1.61.1

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/CLAUDE.md +1 -1
  3. package/admin/index.html +7 -1
  4. package/admin/js/app.js +2 -2
  5. package/admin/js/lib/ai-assist.js +1 -1
  6. package/admin/js/templates/ai-assist-builders.html +29 -0
  7. package/admin/js/templates/docs/usage-chat.html +5 -2
  8. package/admin/js/views/ai-assist-builders.js +1 -0
  9. package/admin/js/views/ai-assist-settings.js +1 -1
  10. package/admin/js/views/index.js +1 -1
  11. package/admin/js/views/login.js +1 -1
  12. package/docs/DommaFeatures.md +4 -2
  13. package/docs/cache.md +24 -0
  14. package/docs/configuration.md +1 -1
  15. package/docs/plugin-development.md +26 -0
  16. package/package.json +1 -1
  17. package/public/js/effects.js +1 -1
  18. package/public/js/site.js +1 -1
  19. package/server/config.js +37 -0
  20. package/server/routes/api/admin-assistant.js +11 -3
  21. package/server/routes/api/auth.js +4 -1
  22. package/server/routes/api/chats.js +3 -1
  23. package/server/routes/api/components.js +2 -1
  24. package/server/routes/api/content-api.js +1 -1
  25. package/server/routes/api/content-public.js +2 -2
  26. package/server/routes/api/content-writes.js +1 -1
  27. package/server/routes/api/effects.js +4 -9
  28. package/server/routes/api/layouts.js +1 -1
  29. package/server/routes/api/pages.js +6 -5
  30. package/server/routes/api/search.js +3 -3
  31. package/server/routes/api/sidebar-counts.js +2 -0
  32. package/server/routes/api/theme.js +4 -7
  33. package/server/routes/api/views.js +2 -1
  34. package/server/routes/public.js +4 -4
  35. package/server/server.js +14 -7
  36. package/server/services/aiAssist.js +67 -0
  37. package/server/services/cache/adminChange.js +28 -0
  38. package/server/services/chat/ai.js +1 -1
  39. package/server/services/content.js +73 -10
  40. package/server/services/effectsSettings.js +25 -0
  41. package/server/services/health.js +2 -2
  42. package/server/services/hooks.js +1 -1
  43. package/server/services/htmlResponse.js +33 -4
  44. package/server/services/listTags.js +1 -1
  45. package/server/services/markdown.js +56 -22
  46. package/server/services/pluginGuides.js +2 -2
  47. package/server/services/plugins.js +34 -3
  48. package/server/services/projects.js +39 -4
  49. package/server/services/publicAssets.js +488 -0
  50. package/server/services/renderer.js +10 -8
  51. package/server/services/seo/index.js +3 -3
  52. package/server/services/sitemap.js +1 -1
  53. package/server/services/themeSettings.js +11 -0
  54. package/server/services/versions.js +2 -2
  55. package/server/services/wpImport/index.js +2 -2
  56. package/server/templates/page.html +3 -2
@@ -2071,6 +2071,32 @@ export function unindentShortTags(markdown) {
2071
2071
  return lines.map((l, i) => (layout[i].cut ? l.slice(layout[i].cut) : l)).join('\n');
2072
2072
  }
2073
2073
 
2074
+ const SCRUB_PLACEHOLDER = /\x02SC(\d+):(\d+)\x03/g;
2075
+ const SHIELD_PLACEHOLDER = /DMSCT(\d+)x(\d+)x/g;
2076
+
2077
+ /**
2078
+ * The two patterns a shortcode name is matched with - self-closing and wrapping -
2079
+ * built once per name. `bounded`: the name must end there ([map] is not [map-guess]).
2080
+ */
2081
+ const _shortcodePatterns = new Map();
2082
+ function shortcodePatterns(name, bounded) {
2083
+ const key = `${bounded ? 'b' : 'u'}:${name}`;
2084
+ let kept = _shortcodePatterns.get(key);
2085
+ if (!kept) {
2086
+ const end = bounded ? '(?=[\\s/\\]])' : '';
2087
+ kept = {
2088
+ selfClosing: new RegExp(`\\[${name}${end}([^\\]]*)\\s*\\/\\]`, 'gi'),
2089
+ wrapping: new RegExp(`\\[${name}${end}([^\\]]*)\\]([\\s\\S]*?)\\[\\/${name}\\]`, 'gi'),
2090
+ // What must be in the text (lower-cased) for either to match at all.
2091
+ needle: `[${String(name).toLowerCase()}`,
2092
+ // A name with pattern characters in it cannot be looked for as plain text.
2093
+ plain: /^[\w-]+$/.test(String(name))
2094
+ };
2095
+ _shortcodePatterns.set(key, kept);
2096
+ }
2097
+ return kept;
2098
+ }
2099
+
2074
2100
  export function scrubCodeRegions(markdown, preserveIndented = false) {
2075
2101
  const callId = ++scrubCallId;
2076
2102
  const store = [];
@@ -2095,7 +2121,12 @@ export function scrubCodeRegions(markdown, preserveIndented = false) {
2095
2121
  // A stored region can hold an earlier placeholder of this same call - an indented
2096
2122
  // line (preserveIndented) holding an inline <code> - so what comes back is
2097
2123
  // restored too. Restoring it once left "SC1:0" on the page.
2098
- const restore = (s) => s.replace(new RegExp(`\\x02SC${callId}:(\\d+)\\x03`, 'g'), (_, i) => restore(store[parseInt(i, 10)]));
2124
+ // One shared pattern, the call checked in the callback: a pattern built per call
2125
+ // (its id in the source) is compiled afresh every time and never optimised, and
2126
+ // this runs many times for every page drawn. Another call's placeholders are left alone.
2127
+ const restore = (s) => (store.length
2128
+ ? s.replace(SCRUB_PLACEHOLDER, (m, id, i) => (Number(id) === callId ? restore(store[Number(i)]) : m))
2129
+ : s);
2099
2130
  return {scrubbed, restore};
2100
2131
  }
2101
2132
 
@@ -2119,7 +2150,7 @@ function markedBody(md) {
2119
2150
  const {scrubbed, restore} = scrubCodeRegions(text);
2120
2151
  const shielded = restore(scrubbed.replace(ATTR_TAG, (tag) => { held.push(tag); return `DMSCT${id}x${held.length - 1}x`; }));
2121
2152
  if (!held.length) return marked.parse(text);
2122
- return marked.parse(shielded).replace(new RegExp(`DMSCT${id}x(\\d+)x`, 'g'), (_, i) => held[Number(i)]);
2153
+ return marked.parse(shielded).replace(SHIELD_PLACEHOLDER, (m, call, i) => (Number(call) === id ? held[Number(i)] : m));
2123
2154
  }
2124
2155
 
2125
2156
  /**
@@ -2158,20 +2189,19 @@ async function processPluginShortcodes(markdown, user = null) {
2158
2189
  return res + input.slice(lastEnd);
2159
2190
  }
2160
2191
 
2192
+ // Most pages use none of most plugins' shortcodes: a plain-text look for "[name"
2193
+ // first, so a page pays two pattern scans only for the names it holds.
2194
+ let lower = result.toLowerCase();
2161
2195
  for (const {name, handler} of processors) {
2196
+ // The name must end there: [map] is not [map-guess].
2197
+ const patterns = shortcodePatterns(name, true);
2198
+ if (patterns.plain && !lower.includes(patterns.needle)) continue;
2199
+ const before = result;
2162
2200
  // Self-closing: [name attrs /]
2163
- result = await replaceAsync(
2164
- result,
2165
- // The name must end there: [map] is not [map-guess].
2166
- new RegExp(`\\[${name}(?=[\\s/\\]])([^\\]]*)\\s*\\/\\]`, 'gi'),
2167
- (_, attrStr) => handler(attrStr, null, context)
2168
- );
2201
+ result = await replaceAsync(result, patterns.selfClosing, (_, attrStr) => handler(attrStr, null, context));
2169
2202
  // Wrapping: [name attrs]...[/name]
2170
- result = await replaceAsync(
2171
- result,
2172
- new RegExp(`\\[${name}(?=[\\s/\\]])([^\\]]*)\\]([\\s\\S]*?)\\[\\/${name}\\]`, 'gi'),
2173
- (_, attrStr, body) => handler(attrStr, body, context)
2174
- );
2203
+ result = await replaceAsync(result, patterns.wrapping, (_, attrStr, body) => handler(attrStr, body, context));
2204
+ if (result !== before) lower = result.toLowerCase();
2175
2205
  }
2176
2206
  return restore(result);
2177
2207
  }
@@ -2189,15 +2219,15 @@ function processEffectsBlocks(markdown) {
2189
2219
  const {scrubbed, restore} = scrubCodeRegions(markdown);
2190
2220
  let result = scrubbed;
2191
2221
 
2222
+ // As with plugin shortcodes: look for "[name" as plain text before scanning with the patterns.
2223
+ let lower = result.toLowerCase();
2192
2224
  function apply(name, handler) {
2193
- result = result.replace(
2194
- new RegExp(`\\[${name}([^\\]]*)\\s*\\/\\]`, 'gi'),
2195
- (_, attrStr) => handler(attrStr, null)
2196
- );
2197
- result = result.replace(
2198
- new RegExp(`\\[${name}([^\\]]*)\\]([\\s\\S]*?)\\[\\/${name}\\]`, 'gi'),
2199
- (_, attrStr, body) => handler(attrStr, body)
2200
- );
2225
+ const patterns = shortcodePatterns(name, false);
2226
+ if (patterns.plain && !lower.includes(patterns.needle)) return;
2227
+ const before = result;
2228
+ result = result.replace(patterns.selfClosing, (_, attrStr) => handler(attrStr, null));
2229
+ result = result.replace(patterns.wrapping, (_, attrStr, body) => handler(attrStr, body));
2230
+ if (result !== before) lower = result.toLowerCase();
2201
2231
  }
2202
2232
 
2203
2233
  apply('counter', (attrStr) => {
@@ -2230,6 +2260,7 @@ function processEffectsBlocks(markdown) {
2230
2260
  // A single inline character also accepts the concise, unpaired form.
2231
2261
  result = result.replace(/\[celebrate([^\]]*)\]/gi, (match, attrs) =>
2232
2262
  parseShortcodeAttrs(attrs).character ? renderCelebration(attrs) : match);
2263
+ lower = result.toLowerCase();
2233
2264
 
2234
2265
  // Named celebration shortcodes - sugar for [celebrate theme="..."] with override support
2235
2266
  ['christmas', 'halloween', 'st-patricks', 'valentines', 'guy-fawkes',
@@ -5406,7 +5437,10 @@ export function sanitizePageHtml(html) {
5406
5437
  */
5407
5438
  export async function parseMarkdown(raw, opts = {}) {
5408
5439
  opts = {...opts, [SLOT_BUDGET]:opts[SLOT_BUDGET] || {remaining:500}};
5409
- const {data, content: written} = matter(raw);
5440
+ // Options passed on purpose: without them gray-matter keeps every string it is
5441
+ // given for the life of the process - each editor preview, each version of each
5442
+ // page - and hands every caller the same `data` object.
5443
+ const {data, content: written} = matter(raw, {});
5410
5444
  const content = unindentShortTags(written);
5411
5445
 
5412
5446
  // Pipeline (earlier stages are already HTML when later ones run; keep in step
@@ -78,7 +78,7 @@ export function titleFromSlug(slug) {
78
78
  */
79
79
  export function pageMeta(slug, source) {
80
80
  let data = {};
81
- try { data = matter(source).data || {}; } catch { data = {}; }
81
+ try { data = matter(source, {}).data || {}; } catch { data = {}; }
82
82
  const title = typeof data.title === 'string' && data.title.trim() ? data.title.trim() : titleFromSlug(slug);
83
83
  const order = Number.isFinite(Number(data.order)) && data.order !== null && data.order !== ''
84
84
  ? Number(data.order)
@@ -123,7 +123,7 @@ const SANITIZE = {
123
123
  */
124
124
  export function renderGuide(source) {
125
125
  let body = source;
126
- try { body = matter(source).content; } catch { /* no frontmatter */ }
126
+ try { body = matter(source, {}).content; } catch { /* no frontmatter */ }
127
127
  return sanitizeHtml(marked.parse(body, {gfm: true, async: false}), SANITIZE);
128
128
  }
129
129
 
@@ -43,7 +43,7 @@ import {disableInstantCalls, enableInstantCalls, grantInstantCallsOnce} from './
43
43
  import {claimHosts} from './hostClaims.js';
44
44
  import {pluginSafeFetch} from './outbound.js';
45
45
  import {disableDashboardPro, enableDashboardPro, registerWidget, unregisterWidgetsFrom} from './widgets.js';
46
- import {disableAiAssist, enableAiAssist} from './aiAssist.js';
46
+ import {disableAiAssist, enableAiAssist, registerAiAssistBuilder, unregisterAiAssistBuilder} from './aiAssist.js';
47
47
  import {createCollection, getCollection, updateCollection} from './collections.js';
48
48
  import * as defaultRolesService from './roles.js';
49
49
  import {getAdminScope, normaliseAdminScope} from './roles.js';
@@ -52,7 +52,8 @@ import {getEffectiveRoles} from './userRoles.js';
52
52
  import {isDcmStaff} from './staffAccess.js';
53
53
  import * as defaultUsersService from './users.js';
54
54
  import {registerLoginCheck, registerPasswordCheck} from './authChecks.js';
55
- import {invalidateTags as cacheInvalidateTags} from './cache/index.js';
55
+ import * as cache from './cache/index.js';
56
+ const cacheInvalidateTags = cache.invalidateTags;
56
57
  import {CORE_TOOLS, getToolStates, toolOpenAt, unmetRequirements} from './tools.js';
57
58
  import {adminPortalFor} from './adminPortal.js';
58
59
 
@@ -624,6 +625,13 @@ export async function registerPlugins(fastify) {
624
625
  }
625
626
  }
626
627
 
628
+ // AI Assist's Builders tab (services/aiAssist.js): this plugin's AI chats, linked
629
+ // from there. Only its own screen and API are accepted.
630
+ if (manifest.admin?.aiAssist) {
631
+ const problem = registerAiAssistBuilder(manifest.name, manifest.admin.aiAssist);
632
+ if (problem) fastify.log.warn(`[plugins] admin.aiAssist for "${manifest.name}" skipped: ${problem}`);
633
+ }
634
+
627
635
  // Dashboard widgets the manifest offers (services/widgets.js) - drawn by the admin
628
636
  // from their `kind` and `dataUrl`, so a Tool needs no code to have one.
629
637
  for (const def of manifest.admin?.widgets || []) {
@@ -707,6 +715,28 @@ export async function getPluginSettings(name) {
707
715
  * @returns {Promise<{ head: string, bodyEnd: string }>}
708
716
  */
709
717
  export async function getInjectionSnippets() {
718
+ if (!cache.isEnabled()) return readInjectionSnippets();
719
+ const now = Date.now();
720
+ if (!_injectionMemo || now - _injectionMemo.at >= INJECTION_MEMO_TTL_MS) {
721
+ const promise = readInjectionSnippets();
722
+ _injectionMemo = {at: now, promise};
723
+ promise.catch(() => { if (_injectionMemo?.promise === promise) _injectionMemo = null; });
724
+ }
725
+ return {...await _injectionMemo.promise};
726
+ }
727
+
728
+ // Every page drawn asks for these, and the answer meant reading every plugin's
729
+ // manifest and snippet files again - a few hundred file operations a page on a
730
+ // site with dozens of plugins. Kept while the response cache is on, dropped
731
+ // with it (switching a plugin on or off is an admin change, which clears the
732
+ // cache), and for no more than a few seconds, so files changed by hand show
733
+ // without a restart.
734
+ const INJECTION_MEMO_TTL_MS = 10_000;
735
+ /** @type {{at: number, promise: Promise<{head: string, headLate: string, bodyEnd: string}>}|null} */
736
+ let _injectionMemo = null;
737
+ cache.onInvalidate(() => { _injectionMemo = null; });
738
+
739
+ async function readInjectionSnippets() {
710
740
  const manifests = await discoverPlugins();
711
741
  const states = getPluginStates();
712
742
 
@@ -842,7 +872,7 @@ export async function setupPlugin(pluginName, services) {
842
872
  for (const filePath of mdFiles) {
843
873
  try {
844
874
  const raw = await fs.readFile(filePath, 'utf8');
845
- const parsed = matter(raw);
875
+ const parsed = matter(raw, {});
846
876
  const rel = path.relative(pagesDir, filePath);
847
877
  let urlPath = '/' + rel.replace(/\\/g, '/').replace(/\.md$/, '').replace(/\/index$/, '');
848
878
  if (urlPath !== '/' && urlPath.endsWith('/')) urlPath = urlPath.slice(0, -1);
@@ -1172,6 +1202,7 @@ export async function runLifecycleHook(name, hook, fastify) {
1172
1202
  disableInstantCalls(name);
1173
1203
  disableDashboardPro(name);
1174
1204
  disableAiAssist(name);
1205
+ unregisterAiAssistBuilder(name);
1175
1206
  unregisterWidgetsFrom(name);
1176
1207
  unregisterContactAccess(name);
1177
1208
  unregisterDirectMessenger(name);
@@ -21,6 +21,7 @@
21
21
 
22
22
  import {createEntry, deleteEntry, listEntries, updateEntry} from './collections.js';
23
23
  import {getRoleLevel} from './roles.js';
24
+ import * as cache from './cache/index.js';
24
25
 
25
26
  /** Slug of the preset collection that stores project records. */
26
27
  export const PROJECTS_COLLECTION_SLUG = 'projects';
@@ -77,6 +78,29 @@ export function validateSlug(slug) {
77
78
  * @throws {Error} If the projects collection schema is missing.
78
79
  */
79
80
  export async function listProjects() {
81
+ if (!cache.isEnabled()) return readProjects();
82
+ const now = Date.now();
83
+ if (!_projectsMemo || now - _projectsMemo.at >= PROJECTS_MEMO_TTL_MS) {
84
+ const promise = readProjects();
85
+ _projectsMemo = {at: now, promise};
86
+ promise.catch(() => { if (_projectsMemo?.promise === promise) _projectsMemo = null; });
87
+ }
88
+ return structuredClone(await _projectsMemo.promise);
89
+ }
90
+
91
+ // Every public page request asks which project the page is in and whether that
92
+ // project is switched on, and each answer used to read the whole projects
93
+ // collection. The list is kept while the response cache is on and dropped
94
+ // whenever ANYTHING leaves that cache - an entry write invalidates its
95
+ // collection's tag - so switching a project off still takes effect at once.
96
+ // The short TTL bounds a change the hooks never hear about (a database edited
97
+ // from outside). Cloned out: a caller that edits a row edits its own copy.
98
+ const PROJECTS_MEMO_TTL_MS = 30_000;
99
+ /** @type {{at: number, promise: Promise<Array<object>>}|null} */
100
+ let _projectsMemo = null;
101
+ cache.onInvalidate(() => { _projectsMemo = null; });
102
+
103
+ async function readProjects() {
80
104
  const {entries} = await listEntries(PROJECTS_COLLECTION_SLUG, {limit: 0});
81
105
  const projects = entries.map(e => normalise(e));
82
106
  return projects.sort((a, b) => {
@@ -95,9 +119,7 @@ export async function listProjects() {
95
119
  */
96
120
  export async function getProject(slug) {
97
121
  if (!validateSlug(slug)) return null;
98
- const {entries} = await listEntries(PROJECTS_COLLECTION_SLUG, {limit: 0});
99
- const entry = entries.find(e => e.data?.slug === slug);
100
- return entry ? normalise(entry) : null;
122
+ return (await listProjects()).find(p => p.slug === slug) || null;
101
123
  }
102
124
 
103
125
  /**
@@ -384,7 +406,20 @@ export function resolveArtefactProject(artefact) {
384
406
  */
385
407
  export async function getProjectForPage(urlPath, explicitProject) {
386
408
  if (typeof explicitProject === 'string' && explicitProject) return explicitProject;
387
- const projects = await listProjects();
409
+ return projectForPage(await listProjects(), urlPath, explicitProject);
410
+ }
411
+
412
+ /**
413
+ * getProjectForPage() over a project list the caller already holds - a page
414
+ * listing resolves hundreds of pages against one read of the projects.
415
+ *
416
+ * @param {Array<object>} projects - listProjects()
417
+ * @param {string} urlPath
418
+ * @param {string|null|undefined} explicitProject
419
+ * @returns {string|null}
420
+ */
421
+ export function projectForPage(projects, urlPath, explicitProject) {
422
+ if (typeof explicitProject === 'string' && explicitProject) return explicitProject;
388
423
  const coreFallback = projects.some(p => p.slug === CORE_PROJECT_SLUG) ? CORE_PROJECT_SLUG : null;
389
424
  if (explicitProject === null) return coreFallback;
390
425
  let best = null;