domma-cms 1.67.2 → 1.68.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/CHANGELOG.md +19 -0
- package/CLAUDE.md +2 -0
- package/admin/js/lib/layouts-arrange.js +1 -1
- package/admin/js/lib/rich-field.js +9 -2
- package/admin/js/lib/seo-defaults.js +1 -1
- package/admin/js/templates/docs/api-content.html +3 -2
- package/admin/js/templates/docs/api-layouts.html +1 -1
- package/admin/js/templates/docs/usage-navigation.html +2 -1
- package/admin/js/templates/docs/usage-pages.html +3 -1
- package/admin/js/templates/docs/usage-search-engines.html +3 -0
- package/admin/js/templates/page-editor.html +3 -3
- package/admin/js/views/form-editor.js +5 -5
- package/admin/js/views/layouts.js +7 -7
- package/admin/js/views/menu-editor.js +20 -20
- package/admin/js/views/page-editor.js +9 -9
- package/admin/js/views/seo.js +31 -30
- package/docs/DommaFeatures.md +6 -2
- package/docs/api-reference.md +7 -3
- package/docs/guides/multi-page-forms.md +1 -1
- package/docs/plugin-development.md +20 -0
- package/package.json +1 -1
- package/plugins/blog/CHANGELOG.md +3 -0
- package/plugins/blog/CLAUDE.md +15 -0
- package/plugins/blog/admin/views/categories.js +5 -2
- package/plugins/blog/admin/views/kit.js +19 -0
- package/plugins/blog/docs/guide.md +2 -0
- package/plugins/blog/plugin.js +18 -2
- package/plugins/blog/plugin.json +6 -6
- package/plugins/blog/plugin.public.js +16 -2
- package/plugins/blog/public/blog.css +3 -0
- package/plugins/blog/tests/public.test.js +29 -2
- package/plugins/feedback/CLAUDE.md +22 -0
- package/plugins/feedback/admin/css/index.css +2 -0
- package/plugins/feedback/admin/views/inbox.js +5 -2
- package/plugins/feedback/admin/views/send.js +12 -5
- package/plugins/feedback/docs/guide.md +3 -2
- package/plugins/feedback/lib/sender.js +1 -1
- package/plugins/feedback/lib/shape.js +72 -11
- package/plugins/feedback/plugin.js +3 -3
- package/plugins/feedback/plugin.json +3 -2
- package/plugins/feedback/tests/api.test.js +47 -1
- package/plugins/feedback/tests/shape.test.js +87 -1
- package/plugins/free-tier.lock.json +43 -42
- package/plugins/shopping-cart/CHANGELOG.md +20 -0
- package/plugins/shopping-cart/CLAUDE.md +38 -1
- package/plugins/shopping-cart/admin/css/index.css +4 -0
- package/plugins/shopping-cart/admin/lib/kit.js +1 -0
- package/plugins/shopping-cart/admin/views/products.js +95 -3
- package/plugins/shopping-cart/admin/views/shop.js +2 -1
- package/plugins/shopping-cart/docs/guide.md +6 -1
- package/plugins/shopping-cart/inject/body.html +2 -2
- package/plugins/shopping-cart/lib/description.js +173 -0
- package/plugins/shopping-cart/lib/render.js +3 -2
- package/plugins/shopping-cart/lib/shop.js +4 -1
- package/plugins/shopping-cart/plugin.js +17 -2
- package/plugins/shopping-cart/plugin.json +3 -2
- package/plugins/shopping-cart/plugin.public.js +6 -9
- package/plugins/shopping-cart/public/lib/products.js +40 -3
- package/plugins/shopping-cart/public/shop.css +1 -0
- package/plugins/shopping-cart/tests/pure.test.js +26 -1
- package/plugins/shopping-cart/tests/seo.test.js +174 -0
- package/public/js/menu-cards.mjs +4 -4
- package/public/js/menu-look.mjs +1 -1
- package/public/js/rich-text.mjs +4 -4
- package/public/js/site.js +1 -1
- package/server/routes/api/forms.js +5 -3
- package/server/routes/api/layouts.js +8 -4
- package/server/routes/api/seo.js +10 -4
- package/server/services/content.js +23 -0
- package/server/services/contentApi.js +27 -4
- package/server/services/forms.js +58 -2
- package/server/services/health.js +3 -1
- package/server/services/markdown.js +2 -2
- package/server/services/menus.js +56 -6
- package/server/services/renderer.js +28 -4
- package/server/services/richText.js +56 -3
- package/server/services/search.js +5 -2
- package/server/services/sitemap.js +5 -3
- package/server/templates/page.html +1 -1
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A product's description - one stored string, read two ways.
|
|
3
|
+
*
|
|
4
|
+
* A description used to be Markdown, with shortcodes, rendered by the CMS's
|
|
5
|
+
* page pipeline. The product editor now has a rich text editor, and what it
|
|
6
|
+
* saves is rich text. Nothing stored is rewritten: a description that is not
|
|
7
|
+
* rich text is still Markdown and still goes through `parseMarkdown`,
|
|
8
|
+
* shortcodes and all, exactly as before. Rich text is cleaned to a few
|
|
9
|
+
* formatted paragraphs - shortcodes do NOT run in it.
|
|
10
|
+
*
|
|
11
|
+
* `isRichDescription` is the ONE place that tells the two apart, and it is
|
|
12
|
+
* strict on purpose: Markdown may begin with raw HTML (`<p class="lead">`),
|
|
13
|
+
* and reading that as rich text would cost it its classes, pictures and
|
|
14
|
+
* shortcodes. Every reader in the Tool asks it; nothing else decides.
|
|
15
|
+
*
|
|
16
|
+
* @module shopping-cart/lib/description
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {inertShortcodes, parseMarkdown} from '../../../server/services/markdown.js';
|
|
20
|
+
import {cleanRichText, isRichText, plainToRichHtml, renderRichText, richTextToPlain} from '../../../server/services/richText.js';
|
|
21
|
+
import {DESCRIPTION_MAX} from '../public/lib/products.js';
|
|
22
|
+
|
|
23
|
+
const esc = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
24
|
+
.replace(/"/g, '"').replace(/'/g, ''');
|
|
25
|
+
|
|
26
|
+
/** The blocks rich text is made of; everything in a rich description is inside one. */
|
|
27
|
+
const RICH_BLOCKS = new Set(['p', 'ul', 'ol', 'h2', 'h3', 'h4', 'blockquote']);
|
|
28
|
+
|
|
29
|
+
/** Is there nothing but white space outside the top-level blocks? (On cleaned, well-formed rich text.) */
|
|
30
|
+
function blocksOnly(html) {
|
|
31
|
+
const tag = /<(\/?)([a-z][a-z0-9]*)\b[^<>]*>/gi;
|
|
32
|
+
let depth = 0;
|
|
33
|
+
let from = 0;
|
|
34
|
+
for (let m = tag.exec(html); m; m = tag.exec(html)) {
|
|
35
|
+
if (depth === 0 && html.slice(from, m.index).trim()) return false;
|
|
36
|
+
const block = RICH_BLOCKS.has(m[2].toLowerCase());
|
|
37
|
+
if (depth === 0 && (!block || m[1])) return false; // an inline element, or a stray closing tag, outside a block
|
|
38
|
+
if (block) depth += m[1] ? -1 : 1;
|
|
39
|
+
from = tag.lastIndex;
|
|
40
|
+
}
|
|
41
|
+
return depth === 0 && !html.slice(from).trim();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Is this stored description rich text - exactly what the editor and the
|
|
46
|
+
* server's cleaner store? It starts with a block (`isRichText`), cleaning it
|
|
47
|
+
* changes nothing, and all of it is inside blocks. Anything else is Markdown:
|
|
48
|
+
* one that opens with `<p class="lead">`, has an <img>, or follows a `<p>`
|
|
49
|
+
* with a line of its own (a shortcode, say) is not what the editor saves.
|
|
50
|
+
*
|
|
51
|
+
* @param {*} value - as stored
|
|
52
|
+
* @returns {boolean}
|
|
53
|
+
*/
|
|
54
|
+
export function isRichDescription(value) {
|
|
55
|
+
return typeof value === 'string' && isRichText(value)
|
|
56
|
+
&& cleanRichText(value, {max: DESCRIPTION_MAX}) === value && blocksOnly(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Text as it was always stored: control characters out, trimmed, cut to length. */
|
|
60
|
+
// eslint-disable-next-line no-control-regex
|
|
61
|
+
const asTyped = (value) => String(value ?? '').replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, '').trim().slice(0, DESCRIPTION_MAX);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A description as it is to be stored.
|
|
65
|
+
*
|
|
66
|
+
* One that arrives as the product already has it (`stored` - the form sends an
|
|
67
|
+
* untouched description back, a quick edit and a copy pass it on) is kept as
|
|
68
|
+
* it is, whatever it is. Otherwise a value that starts with a block is the
|
|
69
|
+
* editor's and is cleaned into rich text; anything else is Markdown, as typed.
|
|
70
|
+
*
|
|
71
|
+
* @param {*} value
|
|
72
|
+
* @param {{stored?: string}} [opts] - the description this product (or the one being copied) has now
|
|
73
|
+
* @returns {string}
|
|
74
|
+
*/
|
|
75
|
+
export function cleanDescription(value, {stored} = {}) {
|
|
76
|
+
const text = asTyped(value);
|
|
77
|
+
if (typeof stored === 'string' && text === stored) return text;
|
|
78
|
+
if (!isRichText(text)) return text;
|
|
79
|
+
const out = cleanRichText(text, {max: DESCRIPTION_MAX});
|
|
80
|
+
if (!out || isRichDescription(out)) return out;
|
|
81
|
+
// Something outside a block survived the cleaner: keep the words, as paragraphs, so it is rich text for certain.
|
|
82
|
+
return cleanRichText(plainToRichHtml(richTextToPlain(out)), {max: DESCRIPTION_MAX});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The description on the product's page.
|
|
87
|
+
*
|
|
88
|
+
* @param {*} value - as stored
|
|
89
|
+
* @param {{urlPath?: string}} [opts]
|
|
90
|
+
* @returns {Promise<string>} HTML that is safe to put in the page; '' for nothing
|
|
91
|
+
*/
|
|
92
|
+
export async function descriptionHtml(value, {urlPath} = {}) {
|
|
93
|
+
const text = typeof value === 'string' ? value : '';
|
|
94
|
+
if (!text.trim()) return '';
|
|
95
|
+
// Rich text is words and formatting: a "[shortcode]" typed in it is shown, never run.
|
|
96
|
+
if (isRichDescription(text)) return inertShortcodes(renderRichText(text));
|
|
97
|
+
try { return (await parseMarkdown(text, {urlPath})).html; }
|
|
98
|
+
catch { return `<p>${esc(text)}</p>`; }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The description as plain words, for a search engine's summary or anything
|
|
103
|
+
* else that is not a page.
|
|
104
|
+
*
|
|
105
|
+
* @param {*} value - as stored
|
|
106
|
+
* @returns {string}
|
|
107
|
+
*/
|
|
108
|
+
export function descriptionText(value) {
|
|
109
|
+
const text = typeof value === 'string' ? value : '';
|
|
110
|
+
if (isRichDescription(text)) return richTextToPlain(text);
|
|
111
|
+
return text.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/^\s*>+/gm, '').replace(/[#*_`]/g, '');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The blocks rich text has; a line break before or after one is only how the HTML was laid out. */
|
|
115
|
+
const BLOCK = 'p|ul|ol|li|h[2-4]|blockquote';
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Page HTML cut down to rich text: headings the editor has no level for become
|
|
119
|
+
* ones it has, and everything else it cannot hold (pictures, tables, embeds)
|
|
120
|
+
* is left out, keeping its words.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} html
|
|
123
|
+
* @returns {string}
|
|
124
|
+
*/
|
|
125
|
+
function toRichText(html) {
|
|
126
|
+
let out = String(html ?? '')
|
|
127
|
+
.replace(/<(\/?)h1(\s[^<>]*)?>/gi, '<$1h2>')
|
|
128
|
+
.replace(/<(\/?)h[56](\s[^<>]*)?>/gi, '<$1h4>')
|
|
129
|
+
.replace(/<(\/?)(pre|tr|figcaption|dt|dd)(\s[^<>]*)?>/gi, '<$1p>')
|
|
130
|
+
.replace(/<hr(\s[^<>]*)?\/?>/gi, '')
|
|
131
|
+
.trim();
|
|
132
|
+
if (!isRichText(out)) out = `<p>${out}</p>`;
|
|
133
|
+
return cleanRichText(out, {max: DESCRIPTION_MAX})
|
|
134
|
+
.replace(new RegExp(`\\s*\\n\\s*(?=</?(${BLOCK})\\b)`, 'gi'), '')
|
|
135
|
+
.replace(new RegExp(`(</?(${BLOCK})(\\s[^<>]*)?>)\\s*\\n\\s*`, 'gi'), '$1')
|
|
136
|
+
.replace(/<p>\s*<\/p>/g, '');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A Markdown description as the rich text editor can show it: rendered, with
|
|
141
|
+
* its shortcodes left as the words in the brackets (that is what saving it
|
|
142
|
+
* from the editor will keep), and cleaned to rich text.
|
|
143
|
+
*
|
|
144
|
+
* Also says what an edit would lose, so the editor can warn before the first
|
|
145
|
+
* one: `shortcodes` when the page pipeline does something with the text that
|
|
146
|
+
* rich text cannot (it renders differently with its brackets made inert), and
|
|
147
|
+
* `pictures` when the rendered description has any.
|
|
148
|
+
*
|
|
149
|
+
* @param {*} value - a description as stored; one that is rich text already comes back as it is, `rich: true`
|
|
150
|
+
* @param {{urlPath?: string}} [opts]
|
|
151
|
+
* @returns {Promise<{html: string, rich: boolean, shortcodes: boolean, pictures: boolean}>}
|
|
152
|
+
*/
|
|
153
|
+
export async function descriptionForEditor(value, {urlPath} = {}) {
|
|
154
|
+
const text = typeof value === 'string' ? value.slice(0, DESCRIPTION_MAX) : '';
|
|
155
|
+
if (!text.trim()) return {html: '', rich: false, shortcodes: false, pictures: false};
|
|
156
|
+
if (isRichDescription(text)) return {html: text, rich: true, shortcodes: false, pictures: false};
|
|
157
|
+
let live;
|
|
158
|
+
let inert;
|
|
159
|
+
try {
|
|
160
|
+
live = (await parseMarkdown(text, {urlPath})).html;
|
|
161
|
+
inert = (await parseMarkdown(inertShortcodes(text), {urlPath})).html;
|
|
162
|
+
} catch {
|
|
163
|
+
// It does not render as a page either: the editor shows it as the text it is.
|
|
164
|
+
return {html: '', rich: false, shortcodes: false, pictures: false};
|
|
165
|
+
}
|
|
166
|
+
const flat = (html) => String(html).replace(/\s+/g, ' ').trim();
|
|
167
|
+
return {
|
|
168
|
+
html: toRichText(inert),
|
|
169
|
+
rich: false,
|
|
170
|
+
shortcodes: flat(live) !== flat(inert),
|
|
171
|
+
pictures: /<(img|picture|video|iframe)\b/i.test(live)
|
|
172
|
+
};
|
|
173
|
+
}
|
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* the live parts - the basket, filtering, quantities, checkout.
|
|
5
5
|
*
|
|
6
6
|
* Every value that came from a person is escaped here. Product descriptions
|
|
7
|
-
* are the exception: an admin wrote them, and they
|
|
8
|
-
*
|
|
7
|
+
* are the exception: an admin wrote them, and they arrive already rendered and
|
|
8
|
+
* safe - rich text cleaned by the CMS, or Markdown through its page pipeline
|
|
9
|
+
* (sanitised) like any page. See lib/description.js.
|
|
9
10
|
*
|
|
10
11
|
* @module shopping-cart/lib/render
|
|
11
12
|
*/
|
|
@@ -15,6 +15,7 @@ import {randomBytes} from 'crypto';
|
|
|
15
15
|
import {createCollection, createEntry, deleteEntry, getCollection, getEntry, listEntries, updateEntry} from '../../../server/services/collections.js';
|
|
16
16
|
import {getPluginSettings, getPluginStates, savePluginState} from '../../../server/services/plugins.js';
|
|
17
17
|
import {cleanProduct} from '../public/lib/products.js';
|
|
18
|
+
import {cleanDescription} from './description.js';
|
|
18
19
|
import {NEXT, SOLD, canMove} from '../public/lib/orders.js';
|
|
19
20
|
import {currencyOf} from '../public/lib/money.js';
|
|
20
21
|
import {cartRegistry} from './extensions.js';
|
|
@@ -197,7 +198,9 @@ export function saveProduct(input, {id = null, currency = 'GBP', by = null, copy
|
|
|
197
198
|
return withLock(async () => {
|
|
198
199
|
const had = id ? await getProduct(id) : null;
|
|
199
200
|
if (id && !had) return {notFound: true};
|
|
200
|
-
|
|
201
|
+
// The description this product has now (a copy: the one it is a copy of) is kept as it is when it comes back unchanged.
|
|
202
|
+
const stored = copy ? input?.description : had?.data?.description;
|
|
203
|
+
const {data, errors} = cleanProduct(input, {currency, minor: true, cleanDescription: (v) => cleanDescription(v, {stored})});
|
|
201
204
|
// What extensions keep on a product (Shopping Cart Pro's downloads), each checked by its own extension.
|
|
202
205
|
const x = await cartRegistry().cleanProductExt(input?.ext, copy ? input?.ext : had?.data?.ext, {copy, product: data, variantMap});
|
|
203
206
|
Object.assign(errors, x.errors);
|
|
@@ -29,7 +29,9 @@ import {getConfig} from '../../server/config.js';
|
|
|
29
29
|
import {originOf, trustedOrigin} from '../../server/services/passwordReset.js';
|
|
30
30
|
import {cleanCheckout, orderLabel} from './public/lib/orders.js';
|
|
31
31
|
import {formatMoney} from './public/lib/money.js';
|
|
32
|
-
import {publicProduct, byShelf} from './public/lib/products.js';
|
|
32
|
+
import {publicProduct, byShelf, DESCRIPTION_MAX} from './public/lib/products.js';
|
|
33
|
+
import {descriptionForEditor, isRichDescription} from './lib/description.js';
|
|
34
|
+
import {richTextToPlain} from '../../server/services/richText.js';
|
|
33
35
|
import {cartRegistry} from './lib/extensions.js';
|
|
34
36
|
import {SAMPLES} from './lib/samples.js';
|
|
35
37
|
import * as accounts from './lib/accounts.js';
|
|
@@ -341,6 +343,17 @@ export default async function shoppingCart(fastify, options) {
|
|
|
341
343
|
return r.entry;
|
|
342
344
|
});
|
|
343
345
|
|
|
346
|
+
/**
|
|
347
|
+
* A Markdown description as the product editor's rich text editor shows it, and what editing it there
|
|
348
|
+
* would lose (shortcodes, pictures). Nothing is saved: the description stays Markdown until it is edited.
|
|
349
|
+
*/
|
|
350
|
+
fastify.post('/products/description', can('manage'), async (request, reply) => {
|
|
351
|
+
const text = request.body?.description;
|
|
352
|
+
if (typeof text !== 'string' || text.length > DESCRIPTION_MAX) return fail(reply, 400, 'Send the description as text.');
|
|
353
|
+
const s = await liveSettings();
|
|
354
|
+
return descriptionForEditor(text, {urlPath: s.shopPath});
|
|
355
|
+
});
|
|
356
|
+
|
|
344
357
|
/** Quick edits from the list: stock, status, featured - merged over the product. */
|
|
345
358
|
fastify.patch('/products/:id', can('manage'), async (request, reply) => {
|
|
346
359
|
const had = await getProduct(request.params.id);
|
|
@@ -621,7 +634,9 @@ export default async function shoppingCart(fastify, options) {
|
|
|
621
634
|
|
|
622
635
|
fastify.get('/catalogue', async (request) => {
|
|
623
636
|
const s = await liveSettings();
|
|
624
|
-
|
|
637
|
+
// A description is text here, as it always was: Markdown as typed, rich text as the words it says.
|
|
638
|
+
const products = (await allProducts()).filter(e => e.data?.status === 'active' && !e.data?.unlisted).sort(byShelf).map(publicProduct)
|
|
639
|
+
.map(p => (isRichDescription(p.description) ? {...p, description: richTextToPlain(p.description)} : p));
|
|
625
640
|
return {currency: s.currency, shopPath: s.shopPath, products};
|
|
626
641
|
});
|
|
627
642
|
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shopping-cart",
|
|
3
3
|
"displayName": "Shopping Cart",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"tier": "free",
|
|
6
6
|
"description": "Sell from your site: products with options, stock and pictures, a storefront and a cart that follows shoppers from page to page, and checkout with Stripe, PayPal or payment offline. Orders to fulfil in the sidebar, refunds from the order.",
|
|
7
7
|
"author": "Domma CMS",
|
|
8
8
|
"date": "2026-11-08",
|
|
9
|
+
"minCmsVersion": "1.67.2",
|
|
9
10
|
"icon": "shopping-bag",
|
|
10
11
|
"permissions": [
|
|
11
12
|
{
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
],
|
|
46
47
|
"views": {
|
|
47
48
|
"plugin-shopping-cart": {
|
|
48
|
-
"entry": "shopping-cart/admin/views/shop.js?v=
|
|
49
|
+
"entry": "shopping-cart/admin/views/shop.js?v=2026-11-15",
|
|
49
50
|
"exportName": "shopView"
|
|
50
51
|
}
|
|
51
52
|
}
|
|
@@ -16,15 +16,15 @@
|
|
|
16
16
|
import path from 'path';
|
|
17
17
|
import {fileURLToPath} from 'url';
|
|
18
18
|
import {renderBlogPage} from '../../server/services/renderer.js';
|
|
19
|
-
import {parseMarkdown} from '../../server/services/markdown.js';
|
|
20
19
|
import {getConfig} from '../../server/config.js';
|
|
21
20
|
import {byShelf, safeImage} from './public/lib/products.js';
|
|
22
21
|
import {accountHtml, cartHtml, esc, productHtml, receiptHtml, scriptJson, storefrontHtml} from './lib/render.js';
|
|
23
22
|
import {allProducts, getOrder, liveSettings, publicOrder, tokenOk} from './lib/shop.js';
|
|
24
23
|
import {cartRegistry} from './lib/extensions.js';
|
|
24
|
+
import {descriptionHtml, descriptionText} from './lib/description.js';
|
|
25
25
|
|
|
26
26
|
const TEMPLATE = path.join(fileURLToPath(new URL('.', import.meta.url)), 'templates', 'shop.html');
|
|
27
|
-
export const ASSET_V = '1.
|
|
27
|
+
export const ASSET_V = '1.8.0';
|
|
28
28
|
|
|
29
29
|
/** Which page a path is, under the shop's base. */
|
|
30
30
|
export function matchShopPath(pathname, base) {
|
|
@@ -121,19 +121,16 @@ export default async function shopPublic(fastify) {
|
|
|
121
121
|
const all = onShelf(await allProducts());
|
|
122
122
|
const entry = all.find(e => e.data.slug === slug);
|
|
123
123
|
if (!entry) return null; // fall through to the CMS's own 404 (or a page at that address)
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
try { descriptionHtml = (await parseMarkdown(entry.data.description, {urlPath: `${s.shopPath}/${slug}`})).html; }
|
|
127
|
-
catch { descriptionHtml = `<p>${esc(entry.data.description)}</p>`; }
|
|
128
|
-
}
|
|
124
|
+
// Rich text from the editor, or Markdown (with shortcodes) as it always was - lib/description.js.
|
|
125
|
+
const description = await descriptionHtml(entry.data.description, {urlPath: `${s.shopPath}/${slug}`});
|
|
129
126
|
const related = all.filter(e => e.id !== entry.id)
|
|
130
127
|
.sort((a, b) => (Number(b.data.category === entry.data.category) - Number(a.data.category === entry.data.category)) || byShelf(a, b))
|
|
131
128
|
.slice(0, 4);
|
|
132
129
|
return send(reply, request, s, {
|
|
133
130
|
page: 'product',
|
|
134
|
-
body: productHtml({entry, settings: s, base: s.shopPath, descriptionHtml, related}),
|
|
131
|
+
body: productHtml({entry, settings: s, base: s.shopPath, descriptionHtml: description, related}),
|
|
135
132
|
title: entry.data.name,
|
|
136
|
-
description: metaDescription(entry.data.summary ||
|
|
133
|
+
description: metaDescription(entry.data.summary || descriptionText(entry.data.description), `Buy ${entry.data.name}${siteName() ? ` from ${siteName()}` : ''} online.`),
|
|
137
134
|
image: safeImage(entry.data.images?.[0]),
|
|
138
135
|
urlPath: `${s.shopPath}/${slug}`,
|
|
139
136
|
extra: {product: {id: entry.id, variants: (entry.data.variants || []).length}}
|
|
@@ -24,6 +24,8 @@ export const STATUS_LABEL = {active: 'On sale', draft: 'Draft', archived: 'Archi
|
|
|
24
24
|
export const TAX_CLASSES = ['standard', 'reduced', 'zero'];
|
|
25
25
|
export const MAX_IMAGES = 12;
|
|
26
26
|
export const MAX_VARIANTS = 60;
|
|
27
|
+
/** The longest a description may be. Rich text counts its markup, so this is half as much again as the 20,000 it was. */
|
|
28
|
+
export const DESCRIPTION_MAX = 30000;
|
|
27
29
|
|
|
28
30
|
/** "Blue Mug (Large)!" -> "blue-mug-large". */
|
|
29
31
|
export function slugify(text) {
|
|
@@ -68,11 +70,16 @@ const newId = () => Math.random().toString(36).slice(2, 10);
|
|
|
68
70
|
* Clean a product as sent by the admin. Amounts arrive in MINOR units
|
|
69
71
|
* (`minor: true`, the API's contract) or as typed text (the editor's check).
|
|
70
72
|
*
|
|
73
|
+
* The description is rich text (from the editor) or Markdown (typed before the
|
|
74
|
+
* editor, or sent by an API client). Cleaning rich text needs the server's
|
|
75
|
+
* sanitiser, so the server passes `cleanDescription`; without it (the editor's
|
|
76
|
+
* as-you-type check, which only reads the errors) the text is kept as typed.
|
|
77
|
+
*
|
|
71
78
|
* @param {object} input
|
|
72
|
-
* @param {{currency?: string, minor?: boolean}} [opts]
|
|
79
|
+
* @param {{currency?: string, minor?: boolean, cleanDescription?: (value: *) => string}} [opts]
|
|
73
80
|
* @returns {{data: object, errors: Object<string, string>}}
|
|
74
81
|
*/
|
|
75
|
-
export function cleanProduct(input = {}, {currency = 'GBP', minor = true} = {}) {
|
|
82
|
+
export function cleanProduct(input = {}, {currency = 'GBP', minor = true, cleanDescription = null} = {}) {
|
|
76
83
|
const errors = {};
|
|
77
84
|
const name = text(input.name, 140);
|
|
78
85
|
if (!name) errors.name = 'Give the product a name.';
|
|
@@ -126,7 +133,7 @@ export function cleanProduct(input = {}, {currency = 'GBP', minor = true} = {})
|
|
|
126
133
|
name,
|
|
127
134
|
slug,
|
|
128
135
|
summary: text(input.summary, 300),
|
|
129
|
-
description: text(input.description,
|
|
136
|
+
description: typeof cleanDescription === 'function' ? String(cleanDescription(input.description) ?? '') : text(input.description, DESCRIPTION_MAX),
|
|
130
137
|
price: Number.isInteger(price) ? price : 0,
|
|
131
138
|
compareAt: Number.isInteger(compareAt) ? compareAt : null,
|
|
132
139
|
sku: text(input.sku, 60),
|
|
@@ -252,3 +259,33 @@ export function byShelf(a, b) {
|
|
|
252
259
|
|| ((pa.position || 0) - (pb.position || 0))
|
|
253
260
|
|| String(pa.name).localeCompare(String(pb.name));
|
|
254
261
|
}
|
|
262
|
+
|
|
263
|
+
/** A value that starts with one of the rich text blocks - the same test as the CMS's `isRichText`. */
|
|
264
|
+
const RICH_START = /^\s*<(p|ul|ol|h[2-4]|blockquote)(\s[^<>]*)?>/i;
|
|
265
|
+
const BLOCK_START = /<(p|ul|ol|h[2-4]|blockquote)\b/i;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* What a rich text editor holds, as rich text.
|
|
269
|
+
*
|
|
270
|
+
* A browser leaves the first line typed into an EMPTY editor as bare text and
|
|
271
|
+
* makes each later line a <div>, so the editor's HTML does not start with a
|
|
272
|
+
* block - and a description is only read as rich text when it does. Left like
|
|
273
|
+
* that it would be stored as plain text and its tags shown as typed. So: a
|
|
274
|
+
* <div> becomes a paragraph (and a pasted heading of a level rich text has
|
|
275
|
+
* not, one it has), and whatever comes before the first block is put in a
|
|
276
|
+
* paragraph.
|
|
277
|
+
*
|
|
278
|
+
* @param {*} html - the editor's value
|
|
279
|
+
* @returns {string} '' for nothing; otherwise HTML that starts with a block
|
|
280
|
+
*/
|
|
281
|
+
export function editorHtml(html) {
|
|
282
|
+
const value = String(html ?? '');
|
|
283
|
+
if (!value.trim() || RICH_START.test(value)) return value;
|
|
284
|
+
// Emptied again, the editor is left holding a lone <br>: that is nothing.
|
|
285
|
+
if (!value.replace(/<[^<>]*>/g, '').replace(/ /gi, ' ').trim()) return '';
|
|
286
|
+
const blocks = value.replace(/<(\/?)div\b[^<>]*>/gi, '<$1p>')
|
|
287
|
+
.replace(/<(\/?)h1\b[^<>]*>/gi, '<$1h2>').replace(/<(\/?)h[56]\b[^<>]*>/gi, '<$1h4>');
|
|
288
|
+
if (RICH_START.test(blocks)) return blocks;
|
|
289
|
+
const at = blocks.search(BLOCK_START);
|
|
290
|
+
return at < 0 ? `<p>${blocks}</p>` : `<p>${blocks.slice(0, at)}</p>${blocks.slice(at)}`;
|
|
291
|
+
}
|
|
@@ -191,6 +191,7 @@ body {
|
|
|
191
191
|
.shop-assure li { display: flex; gap: .55rem; align-items: center; }
|
|
192
192
|
.shop-description { margin-top: 1rem; padding-top: 1.25rem; border-top: 1px solid var(--shop-line); line-height: 1.7; }
|
|
193
193
|
.shop-description img { max-width: 100%; border-radius: var(--shop-radius-sm); }
|
|
194
|
+
.shop-description p:empty { display: none; }
|
|
194
195
|
.shop-related { margin-top: 4.5rem; }
|
|
195
196
|
.shop-related h2 { font-size: 1.4rem; margin-bottom: 1.25rem; }
|
|
196
197
|
|
|
@@ -2,7 +2,7 @@ import {test} from 'node:test';
|
|
|
2
2
|
import assert from 'node:assert/strict';
|
|
3
3
|
import {createHmac} from 'node:crypto';
|
|
4
4
|
import {formatMoney, fromMinor, includedTax, percentOf, toMinor} from '../public/lib/money.js';
|
|
5
|
-
import {available, cleanProduct, optionsText, priceRange, publicProduct, slugify, unitPrice} from '../public/lib/products.js';
|
|
5
|
+
import {DESCRIPTION_MAX, available, cleanProduct, editorHtml, optionsText, priceRange, publicProduct, slugify, unitPrice} from '../public/lib/products.js';
|
|
6
6
|
import {canMove, cleanCheckout} from '../public/lib/orders.js';
|
|
7
7
|
import {cleanCharges, cleanCustomLines, cleanLines, priceBasket} from '../lib/pricing.js';
|
|
8
8
|
import {paypalPaid, stripeCreateSession, stripeForm, stripeLineItems, stripeRefund, stripeSessionPaid, stripeVerify} from '../lib/payments.js';
|
|
@@ -356,3 +356,28 @@ test('Needs you says a sold-out option is sold out, not ": 0"', async () => {
|
|
|
356
356
|
const s = summarise([], products, {lowStockAt: 3});
|
|
357
357
|
assert.deepEqual(s.lowStock[0].variants, ['Imperial sold out', 'Metric: 2 left']);
|
|
358
358
|
});
|
|
359
|
+
|
|
360
|
+
test('products: the description is cleaned by the server\'s cleaner when given one, kept as typed otherwise', () => {
|
|
361
|
+
const md = ' Hand **thrown**\n\n[buy-button slug="x" /] ';
|
|
362
|
+
assert.equal(cleanProduct({name: 'A', price: 1, description: md}).data.description, md.trim(), 'the editor\'s as-you-type check has no sanitiser');
|
|
363
|
+
assert.equal(cleanProduct({name: 'A', price: 1, description: 'x'.repeat(DESCRIPTION_MAX + 10)}).data.description.length, DESCRIPTION_MAX);
|
|
364
|
+
const seen = [];
|
|
365
|
+
const {data} = cleanProduct({name: 'A', price: 1, description: '<p>Hi</p>'}, {cleanDescription: (v) => { seen.push(v); return '<p>cleaned</p>'; }});
|
|
366
|
+
assert.deepEqual(seen, ['<p>Hi</p>']);
|
|
367
|
+
assert.equal(data.description, '<p>cleaned</p>');
|
|
368
|
+
assert.equal(cleanProduct({name: 'A', price: 1}, {cleanDescription: () => undefined}).data.description, '');
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
test('editorHtml: what the editor holds always starts with a block, so it is read as rich text', () => {
|
|
372
|
+
assert.equal(editorHtml(''), '');
|
|
373
|
+
assert.equal(editorHtml(null), '');
|
|
374
|
+
assert.equal(editorHtml('<p>As it is</p><ul><li>a</li></ul>'), '<p>As it is</p><ul><li>a</li></ul>');
|
|
375
|
+
assert.equal(editorHtml('Brass lamp'), '<p>Brass lamp</p>', 'the first line typed into an empty editor is bare text');
|
|
376
|
+
assert.equal(editorHtml('<b>Brass</b> lamp<div>Second line</div>'), '<p><b>Brass</b> lamp</p><p>Second line</p>');
|
|
377
|
+
assert.equal(editorHtml('<div>a</div><div>b</div>'), '<p>a</p><p>b</p>');
|
|
378
|
+
assert.equal(editorHtml('Sizes<ul><li>S</li></ul>'), '<p>Sizes</p><ul><li>S</li></ul>');
|
|
379
|
+
assert.equal(editorHtml('<h1>Title</h1><p>x</p>'), '<h2>Title</h2><p>x</p>');
|
|
380
|
+
assert.equal(editorHtml('[shop-products /]'), '<p>[shop-products /]</p>', 'so a shortcode typed there is never read as Markdown');
|
|
381
|
+
assert.equal(editorHtml('<br>'), '', 'an emptied editor');
|
|
382
|
+
assert.equal(editorHtml('<div><br></div>'), '');
|
|
383
|
+
});
|
|
@@ -12,6 +12,7 @@ import path from 'node:path';
|
|
|
12
12
|
import Fastify from 'fastify';
|
|
13
13
|
import shoppingCart from '../plugin.js';
|
|
14
14
|
import shopPublic, {metaDescription, shopSitemapEntries} from '../plugin.public.js';
|
|
15
|
+
import {cleanDescription, isRichDescription} from '../lib/description.js';
|
|
15
16
|
|
|
16
17
|
const PLUGINS = path.resolve('config/plugins.json');
|
|
17
18
|
const DIRS = ['shop-products', 'shop-orders'].map(c => path.resolve('content/collections', c));
|
|
@@ -93,3 +94,176 @@ test('shopSitemapEntries is pure: the shop\'s own path, products with a usable s
|
|
|
93
94
|
assert.deepEqual(shopSitemapEntries([e({name: 'A', slug: 'a'}), e({name: 'B', slug: '../b'})], {shopPath: '/store', shopTitle: 'Store'}),
|
|
94
95
|
[{urlPath: '/store', title: 'Store', changefreq: 'daily'}, {urlPath: '/store/a', title: 'A', lastmod: '2031-01-01T00:00:00.000Z'}]);
|
|
95
96
|
});
|
|
97
|
+
|
|
98
|
+
// ---- Descriptions: rich text from the editor, Markdown as it always was -----------------
|
|
99
|
+
|
|
100
|
+
const descOf = (html) => { const i = html.indexOf('<div class="shop-description">'); return i < 0 ? '' : html.slice(i, html.indexOf('</div>\n </div>', i)); };
|
|
101
|
+
const MARKDOWN = 'Hand **thrown**.\n\n[badge variant="success"]New[/badge]\n\n\n\n- 350 ml\n- Made in [Devon](https://example.test/devon)';
|
|
102
|
+
|
|
103
|
+
test('a Markdown description is stored as typed and rendered as before, shortcodes and all', async () => {
|
|
104
|
+
let res = await app.inject({method: 'POST', url: '/products', payload: {name: 'Old mug', price: 900, status: 'active', description: ` ${MARKDOWN}\n`}});
|
|
105
|
+
assert.equal(res.statusCode, 201, res.body);
|
|
106
|
+
assert.equal(res.json().data.description, MARKDOWN, 'trimmed, otherwise untouched: never turned into rich text');
|
|
107
|
+
|
|
108
|
+
const page = (await pub.inject({method: 'GET', url: '/shop/old-mug'})).body;
|
|
109
|
+
const desc = descOf(page);
|
|
110
|
+
assert.match(desc, /<strong>thrown<\/strong>/);
|
|
111
|
+
assert.match(desc, /<span class="badge badge-success">New<\/span>/, 'the shortcode ran');
|
|
112
|
+
assert.match(desc, /<img src="\/media\/mug\.jpg" alt="A mug"/);
|
|
113
|
+
assert.match(desc, /<li>350 ml<\/li>/);
|
|
114
|
+
assert.equal(metaOf(page, 'description'), 'Hand thrown. [badge variant="success"]New[/badge] A mug - 350 ml - Made in Devon', 'Markdown stripped as it was');
|
|
115
|
+
|
|
116
|
+
// What the editor is given for it, and what an edit would lose.
|
|
117
|
+
res = await app.inject({method: 'POST', url: '/products/description', payload: {description: MARKDOWN}});
|
|
118
|
+
assert.equal(res.statusCode, 200, res.body);
|
|
119
|
+
assert.deepEqual(res.json(), {
|
|
120
|
+
html: '<p>Hand <strong>thrown</strong>.</p><p>[badge variant="success"]New[/badge]</p><ul><li>350 ml</li>'
|
|
121
|
+
+ '<li>Made in <a href="https://example.test/devon" rel="noopener noreferrer">Devon</a></li></ul>',
|
|
122
|
+
rich: false, shortcodes: true, pictures: true
|
|
123
|
+
});
|
|
124
|
+
res = await app.inject({method: 'POST', url: '/products/description', payload: {description: 'Just words.\n\nAnd *more*.'}});
|
|
125
|
+
assert.deepEqual(res.json(), {html: '<p>Just words.</p><p>And <em>more</em>.</p>', rich: false, shortcodes: false, pictures: false});
|
|
126
|
+
assert.equal((await app.inject({method: 'POST', url: '/products/description', payload: {description: 7}})).statusCode, 400);
|
|
127
|
+
assert.equal((await app.inject({method: 'POST', url: '/products/description', payload: {description: 'x'.repeat(30001)}})).statusCode, 400);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('a description saved from the editor is rich text: cleaned, shown with its formatting, shortcodes never run', async () => {
|
|
131
|
+
const rich = '<p>Hand <strong>thrown</strong>.</p><p>[badge variant="success"]New[/badge]</p><ul><li>350 ml</li></ul>';
|
|
132
|
+
let res = await app.inject({method: 'POST', url: '/products', payload: {name: 'New mug', price: 900, status: 'active', description: rich}});
|
|
133
|
+
assert.equal(res.statusCode, 201, res.body);
|
|
134
|
+
const id = res.json().id;
|
|
135
|
+
assert.equal(res.json().data.description, rich);
|
|
136
|
+
|
|
137
|
+
let page = (await pub.inject({method: 'GET', url: '/shop/new-mug'})).body;
|
|
138
|
+
let desc = descOf(page);
|
|
139
|
+
assert.match(desc, /<p>Hand <strong>thrown<\/strong>\.<\/p>/);
|
|
140
|
+
assert.match(desc, /<ul><li>350 ml<\/li><\/ul>/);
|
|
141
|
+
assert.doesNotMatch(desc, /class="badge/, 'no shortcode runs in rich text');
|
|
142
|
+
assert.doesNotMatch(desc, /\[badge/, 'and the bracket is written so that no later stage can read it as one');
|
|
143
|
+
assert.match(desc.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(n)), /<p>\[badge variant="success"\]New\[\/badge\]<\/p>/, 'a shopper reads the words');
|
|
144
|
+
assert.equal(metaOf(page, 'description'), 'Hand thrown. [badge variant="success"]New[/badge] - 350 ml', 'the words, without the tags');
|
|
145
|
+
|
|
146
|
+
// Hostile input is cut down to the few elements rich text has - on the way in...
|
|
147
|
+
res = await app.inject({method: 'PUT', url: `/products/${id}`, payload: {...res.json().data,
|
|
148
|
+
description: '<p onclick="x()">Safe <script>alert(1)</script><img src=x onerror=alert(2)><a href="javascript:alert(3)">link</a>'
|
|
149
|
+
+ '<iframe src="https://evil.example"></iframe></p><style>p{display:none}</style>'}});
|
|
150
|
+
assert.equal(res.statusCode, 200, res.body);
|
|
151
|
+
assert.match(res.json().data.description, /^<p>Safe /);
|
|
152
|
+
assert.doesNotMatch(res.json().data.description, /onclick|<script|<img|javascript:|<iframe|<style|alert\(1\)/);
|
|
153
|
+
// One written around the API that the cleaner would change is not rich text (isRichDescription), so it takes
|
|
154
|
+
// the Markdown path - whose own sanitiser still takes the script and the handler out.
|
|
155
|
+
const {updateEntry} = await import('../../../server/services/collections.js');
|
|
156
|
+
const around = '<p>Out <img src=x onerror=alert(2)><script>alert(1)</script></p>';
|
|
157
|
+
await updateEntry('shop-products', id, {...res.json().data, description: around});
|
|
158
|
+
await app.inject({method: 'PATCH', url: `/products/${id}`, payload: {featured: true}}); // refreshes the shelf
|
|
159
|
+
page = (await pub.inject({method: 'GET', url: '/shop/new-mug'})).body;
|
|
160
|
+
desc = descOf(page);
|
|
161
|
+
assert.match(desc, /<p>Out /);
|
|
162
|
+
assert.doesNotMatch(desc, /onerror|<script|alert\(/);
|
|
163
|
+
|
|
164
|
+
// The catalogue's description is text, as it always was.
|
|
165
|
+
const cat = (await app.inject({method: 'GET', url: '/catalogue'})).json().products;
|
|
166
|
+
assert.equal(cat.find(p => p.slug === 'old-mug').description, MARKDOWN);
|
|
167
|
+
assert.equal(cat.find(p => p.slug === 'new-mug').description, around, 'not rich text, so as stored');
|
|
168
|
+
const tidy = await app.inject({method: 'PUT', url: `/products/${id}`, payload: {...res.json().data, description: '<p>Tidy <strong>mug</strong></p><ul><li>350 ml</li></ul>'}});
|
|
169
|
+
assert.equal(tidy.statusCode, 200, tidy.body);
|
|
170
|
+
const cat2 = (await app.inject({method: 'GET', url: '/catalogue'})).json().products;
|
|
171
|
+
assert.equal(cat2.find(p => p.slug === 'new-mug').description, 'Tidy mug\n\n- 350 ml', 'rich text as the words it says');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('a quick edit from the list leaves a description exactly as it is stored', async () => {
|
|
175
|
+
const all = (await app.inject({method: 'GET', url: '/products'})).json();
|
|
176
|
+
const old = all.find(p => p.data.slug === 'old-mug');
|
|
177
|
+
const res = await app.inject({method: 'PATCH', url: `/products/${old.id}`, payload: {featured: true}});
|
|
178
|
+
assert.equal(res.statusCode, 200, res.body);
|
|
179
|
+
assert.equal(res.json().data.description, MARKDOWN);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ---- Which stored descriptions are rich text: only what the editor and the cleaner store ---------
|
|
183
|
+
|
|
184
|
+
test('isRichDescription: Markdown that merely begins with HTML is not rich text', () => {
|
|
185
|
+
const saved = cleanDescription('<p>Hand <b>thrown</b> & glazed, "by hand". </p><div>Second</div><ul><li>350 ml</li></ul>');
|
|
186
|
+
assert.match(saved, /^<p>Hand <b>thrown<\/b>/);
|
|
187
|
+
assert.equal(isRichDescription(saved), true, 'what the editor and the server store');
|
|
188
|
+
assert.equal(cleanDescription(saved), saved, 'and cleaning it again changes nothing');
|
|
189
|
+
assert.equal(isRichDescription('<p>One</p><p>Two <a href="https://example.test/" rel="noopener noreferrer">link</a></p>'), true);
|
|
190
|
+
assert.equal(isRichDescription('<blockquote><p>Quoted</p></blockquote>\n<ol><li>a</li></ol>'), true);
|
|
191
|
+
|
|
192
|
+
assert.equal(isRichDescription('<p class="lead">Hello</p>\n\n**bold**'), false, 'a class the cleaner would take off');
|
|
193
|
+
assert.equal(isRichDescription('<p>Intro</p>\n\n[badge variant="success"]New[/badge]'), false, 'a line of its own after the paragraph');
|
|
194
|
+
assert.equal(isRichDescription('<p>Look <img src="/media/mug.jpg" alt="A mug"></p>'), false, 'a picture');
|
|
195
|
+
assert.equal(isRichDescription('<p>Intro</p> and **more**'), false);
|
|
196
|
+
assert.equal(isRichDescription('<p>Intro</p><strong>loose</strong>'), false);
|
|
197
|
+
assert.equal(isRichDescription('<h2>Features</h2>\n\n- one\n- two'), false);
|
|
198
|
+
assert.equal(isRichDescription('Plain words'), false);
|
|
199
|
+
assert.equal(isRichDescription(''), false);
|
|
200
|
+
assert.equal(isRichDescription(null), false);
|
|
201
|
+
|
|
202
|
+
// A new value from the editor always ends up rich text, even with something loose outside a block.
|
|
203
|
+
const loose = cleanDescription('<p>Intro</p>[badge]New[/badge]');
|
|
204
|
+
assert.equal(isRichDescription(loose), true, loose);
|
|
205
|
+
assert.match(loose, /Intro/);
|
|
206
|
+
assert.match(loose, /\[badge\]New\[\/badge\]/);
|
|
207
|
+
// As the product already has it: kept, whatever it is.
|
|
208
|
+
const old = '<p class="lead">Hello</p>\n\n**bold**';
|
|
209
|
+
assert.equal(cleanDescription(old, {stored: old}), old);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test('old Markdown that begins with HTML keeps its page, meta description, catalogue text and editor warning', async () => {
|
|
213
|
+
const {updateEntry} = await import('../../../server/services/collections.js');
|
|
214
|
+
const OLD = {
|
|
215
|
+
'lead-mug': '<p class="lead">Hand thrown.</p>\n\n**Glazed** by hand.',
|
|
216
|
+
'badge-mug': '<p>Hand thrown.</p>\n\n[badge variant="success"]New[/badge]',
|
|
217
|
+
'picture-mug': '<p>Hand thrown. <img src="/media/mug.jpg" alt="A mug"></p>'
|
|
218
|
+
};
|
|
219
|
+
const ids = {};
|
|
220
|
+
for (const [slug, description] of Object.entries(OLD)) {
|
|
221
|
+
const made = await app.inject({method: 'POST', url: '/products', payload: {name: slug.replace('-', ' '), price: 900, status: 'active'}});
|
|
222
|
+
assert.equal(made.statusCode, 201, made.body);
|
|
223
|
+
ids[slug] = made.json().id;
|
|
224
|
+
// As a shop already has it on disk: written before the editor existed.
|
|
225
|
+
await updateEntry('shop-products', ids[slug], {...made.json().data, description});
|
|
226
|
+
// A quick edit from the list (which also refreshes the shelf) leaves it byte for byte...
|
|
227
|
+
const patched = await app.inject({method: 'PATCH', url: `/products/${ids[slug]}`, payload: {featured: true}});
|
|
228
|
+
assert.equal(patched.json().data.description, description, slug);
|
|
229
|
+
// ...and so does the product form saving it back untouched.
|
|
230
|
+
const put = await app.inject({method: 'PUT', url: `/products/${ids[slug]}`, payload: {...patched.json().data, summary: ''}});
|
|
231
|
+
assert.equal(put.statusCode, 200, put.body);
|
|
232
|
+
assert.equal(put.json().data.description, description, slug);
|
|
233
|
+
}
|
|
234
|
+
const page = async (slug) => (await pub.inject({method: 'GET', url: `/shop/${slug}`})).body;
|
|
235
|
+
|
|
236
|
+
let body = await page('lead-mug');
|
|
237
|
+
assert.match(descOf(body), /<p class="lead">Hand thrown\.<\/p>/, 'the class is still there');
|
|
238
|
+
assert.match(descOf(body), /<strong>Glazed<\/strong>/, 'and the Markdown after it still renders');
|
|
239
|
+
assert.equal(metaOf(body, 'description'), '<p class="lead">Hand thrown.</p> Glazed by hand.', 'the Markdown strip, as before');
|
|
240
|
+
|
|
241
|
+
body = await page('badge-mug');
|
|
242
|
+
assert.match(descOf(body), /<span class="badge badge-success">New<\/span>/, 'the shortcode still runs');
|
|
243
|
+
|
|
244
|
+
body = await page('picture-mug');
|
|
245
|
+
assert.match(descOf(body), /<img src="\/media\/mug\.jpg" alt="A mug"/, 'the picture is still there');
|
|
246
|
+
|
|
247
|
+
const cat = (await app.inject({method: 'GET', url: '/catalogue'})).json().products;
|
|
248
|
+
for (const [slug, description] of Object.entries(OLD)) assert.equal(cat.find(p => p.slug === slug).description, description, slug);
|
|
249
|
+
|
|
250
|
+
// The editor is told each is Markdown, shown it as formatting, and warned about what an edit would lose.
|
|
251
|
+
const describe = async (description) => (await app.inject({method: 'POST', url: '/products/description', payload: {description}})).json();
|
|
252
|
+
assert.deepEqual(await describe(OLD['lead-mug']), {html: '<p>Hand thrown.</p><p><strong>Glazed</strong> by hand.</p>', rich: false, shortcodes: false, pictures: false});
|
|
253
|
+
assert.deepEqual(await describe(OLD['badge-mug']), {html: '<p>Hand thrown.</p><p>[badge variant="success"]New[/badge]</p>', rich: false, shortcodes: true, pictures: false});
|
|
254
|
+
assert.deepEqual(await describe(OLD['picture-mug']), {html: '<p>Hand thrown. </p>', rich: false, shortcodes: false, pictures: true});
|
|
255
|
+
const genuine = '<p>Hand <strong>thrown</strong>.</p><ul><li>350 ml</li></ul>';
|
|
256
|
+
assert.deepEqual(await describe(genuine), {html: genuine, rich: true, shortcodes: false, pictures: false});
|
|
257
|
+
|
|
258
|
+
// A copy carries the description over as it is.
|
|
259
|
+
const copy = await app.inject({method: 'POST', url: `/products/${ids['badge-mug']}/duplicate`});
|
|
260
|
+
assert.equal(copy.statusCode, 201, copy.body);
|
|
261
|
+
assert.equal(copy.json().data.description, OLD['badge-mug']);
|
|
262
|
+
|
|
263
|
+
// Edited in the editor (a different value arrives): now it is rich text, and the shortcode is words.
|
|
264
|
+
const edited = await app.inject({method: 'PUT', url: `/products/${ids['badge-mug']}`, payload: {...copy.json().data, name: 'badge mug', slug: 'badge-mug', status: 'active',
|
|
265
|
+
description: '<p>Hand thrown, twice.</p><p>[badge variant="success"]New[/badge]</p>'}});
|
|
266
|
+
assert.equal(edited.statusCode, 200, edited.body);
|
|
267
|
+
assert.equal(isRichDescription(edited.json().data.description), true);
|
|
268
|
+
assert.doesNotMatch(descOf(await page('badge-mug')), /class="badge/);
|
|
269
|
+
});
|