@mscode-pl/feed-lint 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +282 -2
- package/dist/cli/colors.d.ts +13 -0
- package/dist/cli/colors.js +23 -0
- package/dist/cli/config.d.ts +11 -0
- package/dist/cli/config.js +59 -0
- package/dist/cli/format.d.ts +23 -0
- package/dist/cli/format.js +129 -0
- package/dist/cli/input.d.ts +3 -0
- package/dist/cli/input.js +27 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +151 -0
- package/dist/core/fixes.d.ts +1 -0
- package/dist/core/fixes.js +41 -0
- package/dist/core/gtin.d.ts +5 -0
- package/dist/core/gtin.js +22 -0
- package/dist/core/lint.d.ts +19 -0
- package/dist/core/lint.js +46 -0
- package/dist/core/parse.d.ts +33 -0
- package/dist/core/parse.js +188 -0
- package/dist/core/report.d.ts +28 -0
- package/dist/core/report.js +29 -0
- package/dist/core/urls.d.ts +23 -0
- package/dist/core/urls.js +114 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +5 -0
- package/dist/mcp/server.d.ts +19 -0
- package/dist/mcp/server.js +118 -0
- package/dist/mcp/tools.d.ts +74 -0
- package/dist/mcp/tools.js +124 -0
- package/dist/rules/ceneo.d.ts +7 -0
- package/dist/rules/ceneo.js +222 -0
- package/dist/rules/google.d.ts +5 -0
- package/dist/rules/google.js +278 -0
- package/dist/rules/text.d.ts +5 -0
- package/dist/rules/text.js +21 -0
- package/package.json +60 -4
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { checkGtin, normalizeGtin } from '../core/gtin.js';
|
|
2
|
+
import { htmlTags, isHttpUrl, promoText } from './text.js';
|
|
3
|
+
const GUIDE = 'https://www.ceneo.pl/poradniki/Instrukcja-tworzenia-pliku-XML';
|
|
4
|
+
const ENCODINGS = new Set(['utf-8', 'utf8', 'iso-8859-2', 'windows-1250']);
|
|
5
|
+
const AVAIL = ['1', '3', '7', '14', '90', '99', '110'];
|
|
6
|
+
const DESC_TAGS = new Set(['p', 'h2', 'h3', 'br', 'ol', 'ul', 'li', 'i', 'b', 'strong', 'em']);
|
|
7
|
+
const GENERIC_CATEGORIES = new Set(['wyprzedaż', 'promocja', 'nowości', 'tymczasowa', 'księgarnia', 'zabawki', 'książki i ebooki', 'leki i suplementy', 'inne', 'pozostałe'].map((c) => c.toLowerCase()));
|
|
8
|
+
const ELEMENTS = ['cat', 'name', 'imgs', 'desc', 'attrs'];
|
|
9
|
+
const OFFER_ATTRS = ['id', 'url', 'price', 'avail'];
|
|
10
|
+
const LIMITS = { id: 100, url: 1000, cat: 255, name: 150, desc: 100_000, fileVersion: 255 };
|
|
11
|
+
export const CENEO_RULES = {};
|
|
12
|
+
function rule(id, level, title) {
|
|
13
|
+
CENEO_RULES[`ceneo/${id}`] = { level, title, url: GUIDE };
|
|
14
|
+
return `ceneo/${id}`;
|
|
15
|
+
}
|
|
16
|
+
const kebab = (s) => s.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
17
|
+
for (const a of OFFER_ATTRS)
|
|
18
|
+
rule(`missing-${a}`, 'error', `Offer <o> has no ${a} attribute`);
|
|
19
|
+
for (const e of ELEMENTS)
|
|
20
|
+
rule(`missing-${e}`, 'error', `Offer has no <${e}>`);
|
|
21
|
+
for (const [field, max] of Object.entries(LIMITS))
|
|
22
|
+
rule(`${kebab(field)}-length`, 'error', `${field} is longer than ${max.toLocaleString('en')} characters`);
|
|
23
|
+
const R = {
|
|
24
|
+
encoding: rule('encoding', 'error', 'Encoding is not UTF-8, ISO-8859-2 or Windows-1250'),
|
|
25
|
+
encodingMissing: rule('encoding-missing', 'warning', 'The XML declaration does not state the encoding'),
|
|
26
|
+
version: rule('version', 'warning', 'Root <offers> has no version="1"'),
|
|
27
|
+
duplicateId: rule('duplicate-id', 'error', 'The same offer id is used more than once'),
|
|
28
|
+
urlScheme: rule('url-scheme', 'error', 'Offer url does not start with http:// or https://'),
|
|
29
|
+
priceFormat: rule('price-format', 'error', 'price is not a number with a dot as the decimal separator, e.g. 12.99'),
|
|
30
|
+
priceZero: rule('price-zero', 'warning', 'price is 0 in a full offer (in incremental mode price 0 hides the offer)'),
|
|
31
|
+
avail: rule('avail-value', 'error', 'avail is not one of 1, 3, 7, 14, 90, 99, 110'),
|
|
32
|
+
stock: rule('stock', 'error', 'stock is not a whole number of 0 or more'),
|
|
33
|
+
weight: rule('weight', 'error', 'weight is not a number in kilograms with a dot, e.g. 0.75'),
|
|
34
|
+
basket: rule('basket', 'error', 'basket is not 0 or 1'),
|
|
35
|
+
catGeneric: rule('cat-generic', 'warning', 'Category is temporary or too general (e.g. "Promocja", "Nowości") or has no path'),
|
|
36
|
+
namePromo: rule('name-promo', 'warning', 'Name has commercial additions like "darmowa dostawa" or "-50%"'),
|
|
37
|
+
mainImage: rule('main-image', 'error', 'No <main url="..."> image in <imgs>'),
|
|
38
|
+
imagesCount: rule('images-count', 'error', 'More than 20 images'),
|
|
39
|
+
imageUrl: rule('image-url', 'error', 'Image URL is longer than 2048 characters or not http(s)'),
|
|
40
|
+
descHtml: rule('desc-html', 'warning', 'Description uses HTML tags Ceneo does not allow (only p, h2, h3, br, ol, ul, li, i, b, strong, em)'),
|
|
41
|
+
descCdata: rule('desc-cdata', 'warning', 'Description HTML is not wrapped in CDATA, so it is parsed as XML elements'),
|
|
42
|
+
attrsCount: rule('attrs-count', 'error', 'More than 500 attributes'),
|
|
43
|
+
attrDuplicate: rule('attr-duplicate', 'warning', 'The same attribute name appears twice in one offer'),
|
|
44
|
+
ean: rule('ean', 'error', '"Kod EAN" is not 8 or 13 digits or has a wrong check digit'),
|
|
45
|
+
isbn: rule('isbn', 'error', '"Kod ISBN" is not 13 digits or has a wrong check digit'),
|
|
46
|
+
drugCode: rule('drug-code', 'error', '"Kod leku" is not 7 digits'),
|
|
47
|
+
brand: rule('brand', 'warning', 'No "Marka" or "Producent" attribute'),
|
|
48
|
+
energyClass: rule('energy-class', 'error', '"Klasa energetyczna" is not A to G (or A+++ to A+ on the older scale)'),
|
|
49
|
+
ceMark: rule('ce-mark', 'error', '"Unijny znak CE" is not 0 or 1'),
|
|
50
|
+
ceYear: rule('ce-year', 'warning', '"Unijny znak CE" is 1 but "Rok nadania znaku CE" is missing'),
|
|
51
|
+
gpsr: rule('gpsr-reference', 'error', 'GPSR attribute points to an id missing from <responsibleProducers> or <responsiblePersons>'),
|
|
52
|
+
};
|
|
53
|
+
export function ceneoFileChecks(info, report) {
|
|
54
|
+
const at = (detail) => ({ line: info.rootLine, detail });
|
|
55
|
+
if (!info.encoding)
|
|
56
|
+
report(R.encodingMissing, { line: 1 });
|
|
57
|
+
else if (!ENCODINGS.has(info.encoding.toLowerCase()))
|
|
58
|
+
report(R.encoding, { line: 1, detail: info.encoding });
|
|
59
|
+
if (info.rootAttrs.version !== '1')
|
|
60
|
+
report(R.version, at(info.rootAttrs.version ? `version="${info.rootAttrs.version}"` : undefined));
|
|
61
|
+
const fileVersion = info.rootAttrs.fileVersion;
|
|
62
|
+
if (fileVersion && [...fileVersion].length > LIMITS.fileVersion)
|
|
63
|
+
report('ceneo/file-version-length', at(`${[...fileVersion].length} characters`));
|
|
64
|
+
}
|
|
65
|
+
const NUMBER = /^\d+(?:\.\d+)?$/;
|
|
66
|
+
export function ceneoChecker(report, parsed) {
|
|
67
|
+
const seen = new Map();
|
|
68
|
+
const pending = [];
|
|
69
|
+
// GPSR ids are resolved after the whole file, so lists placed after the offers get a precise message.
|
|
70
|
+
const end = () => {
|
|
71
|
+
for (const { sample, kind, id } of pending) {
|
|
72
|
+
const list = kind === 'producer' ? parsed.responsibleProducers : parsed.responsiblePersons;
|
|
73
|
+
const where = list.has(id) ? 'is declared after the offers; move the list before the first <o>' : 'is not declared';
|
|
74
|
+
report(R.gpsr, { ...sample, detail: `${kind} "${id}" ${where}` });
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const check = (offer) => {
|
|
78
|
+
const a = offer.attrs;
|
|
79
|
+
const id = a.id?.trim() || undefined;
|
|
80
|
+
const update = a.update === '1';
|
|
81
|
+
const at = (n, detail) => ({ line: n?.line ?? offer.line, id, detail });
|
|
82
|
+
const child = (name) => offer.children.find((c) => c.name === name);
|
|
83
|
+
for (const attr of update ? ['id'] : OFFER_ATTRS)
|
|
84
|
+
if (!a[attr]?.trim())
|
|
85
|
+
report(`ceneo/missing-${attr}`, at(undefined));
|
|
86
|
+
if (!update)
|
|
87
|
+
for (const e of ELEMENTS)
|
|
88
|
+
if (!child(e))
|
|
89
|
+
report(`ceneo/missing-${e}`, at(undefined));
|
|
90
|
+
if (id) {
|
|
91
|
+
if ([...id].length > LIMITS.id)
|
|
92
|
+
report('ceneo/id-length', at(undefined, `${[...id].length} characters`));
|
|
93
|
+
const first = seen.get(id);
|
|
94
|
+
if (first !== undefined)
|
|
95
|
+
report(R.duplicateId, at(undefined, `first used on line ${first}`));
|
|
96
|
+
else
|
|
97
|
+
seen.set(id, offer.line);
|
|
98
|
+
}
|
|
99
|
+
if (a.url) {
|
|
100
|
+
if ([...a.url].length > LIMITS.url)
|
|
101
|
+
report('ceneo/url-length', at(undefined, `${[...a.url].length} characters`));
|
|
102
|
+
if (!isHttpUrl(a.url.trim()))
|
|
103
|
+
report(R.urlScheme, at(undefined, a.url.slice(0, 80)));
|
|
104
|
+
}
|
|
105
|
+
if (a.price !== undefined) {
|
|
106
|
+
const price = a.price.trim();
|
|
107
|
+
if (!NUMBER.test(price))
|
|
108
|
+
report(R.priceFormat, at(undefined, `"${a.price}"`));
|
|
109
|
+
else if (Number(price) === 0 && !update)
|
|
110
|
+
report(R.priceZero, at(undefined));
|
|
111
|
+
}
|
|
112
|
+
if (a.avail !== undefined && !AVAIL.includes(a.avail.trim()))
|
|
113
|
+
report(R.avail, at(undefined, `"${a.avail}"`));
|
|
114
|
+
if (a.stock !== undefined && !/^\d+$/.test(a.stock.trim()))
|
|
115
|
+
report(R.stock, at(undefined, `"${a.stock}"`));
|
|
116
|
+
if (a.weight !== undefined && !NUMBER.test(a.weight.trim()))
|
|
117
|
+
report(R.weight, at(undefined, `"${a.weight}"`));
|
|
118
|
+
if (a.basket !== undefined && !['0', '1'].includes(a.basket.trim()))
|
|
119
|
+
report(R.basket, at(undefined, `"${a.basket}"`));
|
|
120
|
+
const cat = child('cat');
|
|
121
|
+
if (cat) {
|
|
122
|
+
const value = cat.text.trim();
|
|
123
|
+
if ([...value].length > LIMITS.cat)
|
|
124
|
+
report('ceneo/cat-length', at(cat, `${[...value].length} characters`));
|
|
125
|
+
const leaf = value.split('/').pop()?.trim().toLowerCase() ?? '';
|
|
126
|
+
if (value && (!value.includes('/') || GENERIC_CATEGORIES.has(leaf)))
|
|
127
|
+
report(R.catGeneric, at(cat, `"${value.slice(0, 80)}"`));
|
|
128
|
+
}
|
|
129
|
+
const name = child('name');
|
|
130
|
+
if (name) {
|
|
131
|
+
const value = name.text.trim();
|
|
132
|
+
if ([...value].length > LIMITS.name)
|
|
133
|
+
report('ceneo/name-length', at(name, `${[...value].length} characters`));
|
|
134
|
+
const promo = promoText(value);
|
|
135
|
+
if (promo)
|
|
136
|
+
report(R.namePromo, at(name, `"${promo}"`));
|
|
137
|
+
}
|
|
138
|
+
const imgs = child('imgs');
|
|
139
|
+
if (imgs) {
|
|
140
|
+
const images = imgs.children.filter((c) => c.name === 'main' || c.name === 'i');
|
|
141
|
+
if (!images.some((c) => c.name === 'main' && c.attrs.url?.trim()))
|
|
142
|
+
report(R.mainImage, at(imgs));
|
|
143
|
+
if (images.length > 20)
|
|
144
|
+
report(R.imagesCount, at(imgs, `${images.length} images`));
|
|
145
|
+
for (const img of images) {
|
|
146
|
+
const url = img.attrs.url?.trim();
|
|
147
|
+
if (url && (url.length > 2048 || !isHttpUrl(url)))
|
|
148
|
+
report(R.imageUrl, at(img, url.slice(0, 80)));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const desc = child('desc');
|
|
152
|
+
if (desc) {
|
|
153
|
+
if ([...desc.text].length > LIMITS.desc)
|
|
154
|
+
report('ceneo/desc-length', at(desc, `${[...desc.text].length} characters`));
|
|
155
|
+
if (desc.children.length)
|
|
156
|
+
report(R.descCdata, at(desc, `<${desc.children[0].name}>`));
|
|
157
|
+
const tags = [...htmlTags(desc.text), ...flatten(desc.children).map((c) => c.name.toLowerCase())];
|
|
158
|
+
const bad = [...new Set(tags.filter((t) => !DESC_TAGS.has(t)))];
|
|
159
|
+
if (bad.length)
|
|
160
|
+
report(R.descHtml, at(desc, `<${bad.slice(0, 5).join('>, <')}>`));
|
|
161
|
+
}
|
|
162
|
+
const attrs = child('attrs');
|
|
163
|
+
if (attrs) {
|
|
164
|
+
const list = attrs.children.filter((c) => c.name === 'a');
|
|
165
|
+
if (list.length > 500)
|
|
166
|
+
report(R.attrsCount, at(attrs, `${list.length} attributes`));
|
|
167
|
+
const values = new Map();
|
|
168
|
+
for (const n of list) {
|
|
169
|
+
const key = (n.attrs.name ?? '').trim().toLowerCase().replace(/_/g, ' ');
|
|
170
|
+
if (!key)
|
|
171
|
+
continue;
|
|
172
|
+
if (values.has(key))
|
|
173
|
+
report(R.attrDuplicate, at(n, `"${n.attrs.name}"`));
|
|
174
|
+
else
|
|
175
|
+
values.set(key, { n, v: n.text.trim() });
|
|
176
|
+
}
|
|
177
|
+
const get = (...names) => names.map((k) => values.get(k)).find((x) => x && x.v);
|
|
178
|
+
const ean = get('kod ean', 'ean');
|
|
179
|
+
if (ean) {
|
|
180
|
+
for (const v of ean.v.split(/[,;]/).map((s) => s.trim()).filter(Boolean)) {
|
|
181
|
+
if (checkGtin(v, [8, 13]) === 'format' || checkGtin(v, [8, 13]) === 'checksum')
|
|
182
|
+
report(R.ean, at(ean.n, `"${v}"`));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const isbn = get('kod isbn', 'isbn');
|
|
186
|
+
if (isbn) {
|
|
187
|
+
const digits = normalizeGtin(isbn.v);
|
|
188
|
+
if (!/^\d{13}$/.test(digits) || checkGtin(digits, [13]) === 'checksum')
|
|
189
|
+
report(R.isbn, at(isbn.n, `"${isbn.v}"`));
|
|
190
|
+
}
|
|
191
|
+
const drug = get('kod leku');
|
|
192
|
+
if (drug && !/^\d{7}$/.test(drug.v))
|
|
193
|
+
report(R.drugCode, at(drug.n, `"${drug.v}"`));
|
|
194
|
+
if (!update && !get('marka', 'producent'))
|
|
195
|
+
report(R.brand, at(attrs));
|
|
196
|
+
const energy = get('klasa energetyczna');
|
|
197
|
+
// Ovens and range hoods still carry the older A+++ to D label legally.
|
|
198
|
+
if (energy && !/^(?:[A-G]|A\+{1,3})$/i.test(energy.v))
|
|
199
|
+
report(R.energyClass, at(energy.n, `"${energy.v}"`));
|
|
200
|
+
const ce = get('unijny znak ce');
|
|
201
|
+
if (ce) {
|
|
202
|
+
if (ce.v !== '0' && ce.v !== '1')
|
|
203
|
+
report(R.ceMark, at(ce.n, `"${ce.v}"`));
|
|
204
|
+
else if (ce.v === '1' && !get('rok nadania znaku ce'))
|
|
205
|
+
report(R.ceYear, at(ce.n));
|
|
206
|
+
}
|
|
207
|
+
const producer = get('producent odpowiedzialny');
|
|
208
|
+
if (producer && !parsed.responsibleProducers.has(producer.v))
|
|
209
|
+
pending.push({ sample: at(producer.n), kind: 'producer', id: producer.v });
|
|
210
|
+
const person = get('podmiot odpowiedzialny');
|
|
211
|
+
if (person && !parsed.responsiblePersons.has(person.v))
|
|
212
|
+
pending.push({ sample: at(person.n), kind: 'person', id: person.v });
|
|
213
|
+
}
|
|
214
|
+
else if (!update) {
|
|
215
|
+
report(R.brand, at(undefined));
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
return Object.assign(check, { end });
|
|
219
|
+
}
|
|
220
|
+
function flatten(nodes) {
|
|
221
|
+
return nodes.flatMap((n) => [n, ...flatten(n.children)]);
|
|
222
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ParseResult, XmlNode } from '../core/parse.js';
|
|
2
|
+
import type { Report, RuleInfo } from '../core/report.js';
|
|
3
|
+
export declare const GOOGLE_RULES: Record<string, RuleInfo>;
|
|
4
|
+
export declare function productFields(item: XmlNode, atom: boolean): Map<string, XmlNode[]>;
|
|
5
|
+
export declare function googleChecker(report: Report, parsed: ParseResult): (item: XmlNode) => void;
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { checkGtin } from '../core/gtin.js';
|
|
2
|
+
import { htmlTags, isAllCaps, isHttpUrl, promoText, urlExtension } from './text.js';
|
|
3
|
+
const SPEC = 'https://support.google.com/merchants/answer/7052112';
|
|
4
|
+
const DOCS = {
|
|
5
|
+
availability: 'https://support.google.com/merchants/answer/12472827',
|
|
6
|
+
price: 'https://support.google.com/merchants/answer/12471842',
|
|
7
|
+
gtin: 'https://support.google.com/merchants/answer/12473440',
|
|
8
|
+
image_link: 'https://support.google.com/merchants/answer/12472547',
|
|
9
|
+
};
|
|
10
|
+
const doc = (field) => DOCS[field] ?? SPEC;
|
|
11
|
+
const REQUIRED = ['id', 'title', 'description', 'link', 'image_link', 'availability', 'price'];
|
|
12
|
+
const ALTERNATIVE = { title: 'structured_title', description: 'structured_description' };
|
|
13
|
+
const LIMITS = {
|
|
14
|
+
id: 50,
|
|
15
|
+
title: 150,
|
|
16
|
+
description: 5000,
|
|
17
|
+
brand: 70,
|
|
18
|
+
mpn: 70,
|
|
19
|
+
item_group_id: 50,
|
|
20
|
+
product_type: 750,
|
|
21
|
+
color: 100,
|
|
22
|
+
size: 100,
|
|
23
|
+
mobile_link: 2000,
|
|
24
|
+
image_link: 2000,
|
|
25
|
+
additional_image_link: 2000,
|
|
26
|
+
availability_date: 25,
|
|
27
|
+
sale_price_effective_date: 51,
|
|
28
|
+
};
|
|
29
|
+
const YES_NO = ['yes', 'no'];
|
|
30
|
+
const ENUMS = {
|
|
31
|
+
availability: ['in_stock', 'out_of_stock', 'preorder', 'backorder'],
|
|
32
|
+
condition: ['new', 'refurbished', 'used'],
|
|
33
|
+
identifier_exists: YES_NO,
|
|
34
|
+
adult: YES_NO,
|
|
35
|
+
is_bundle: YES_NO,
|
|
36
|
+
gender: ['male', 'female', 'unisex'],
|
|
37
|
+
age_group: ['newborn', 'infant', 'toddler', 'kids', 'adult'],
|
|
38
|
+
energy_efficiency_class: ['A+++', 'A++', 'A+', 'A', 'B', 'C', 'D', 'E', 'F', 'G'],
|
|
39
|
+
};
|
|
40
|
+
const IMAGE_EXTENSIONS = new Set(['jpg', 'jpeg', 'webp', 'png', 'gif', 'bmp', 'tif', 'tiff']);
|
|
41
|
+
const BRAND_PLACEHOLDERS = new Set(['n/a', 'na', 'generic', 'no brand', 'does not exist', 'none', 'brak', 'brak marki', 'bez marki']);
|
|
42
|
+
const BAD_COLOR = /^(?:\d+|#?[0-9a-f]{3}|#?[0-9a-f]{6}|\p{L}|multicolou?r|various|n\/?a|see image|wielokolorowy|wielobarwny|różne|inny)$/iu;
|
|
43
|
+
const CURRENCIES = new Set(Intl.supportedValuesOf('currency'));
|
|
44
|
+
export const GOOGLE_RULES = {};
|
|
45
|
+
function rule(id, level, title, url = SPEC) {
|
|
46
|
+
GOOGLE_RULES[`google/${id}`] = { level, title, url };
|
|
47
|
+
return `google/${id}`;
|
|
48
|
+
}
|
|
49
|
+
for (const field of REQUIRED)
|
|
50
|
+
rule(`missing-${field.replace(/_/g, '-')}`, 'error', `Missing g:${field}`, doc(field));
|
|
51
|
+
for (const [field, max] of Object.entries(LIMITS))
|
|
52
|
+
rule(`${field.replace(/_/g, '-')}-length`, 'error', `g:${field} is longer than ${max} characters`, doc(field));
|
|
53
|
+
for (const field of Object.keys(ENUMS))
|
|
54
|
+
rule(`${field.replace(/_/g, '-')}-value`, 'error', `g:${field} has a value Google does not accept`, doc(field));
|
|
55
|
+
const R = {
|
|
56
|
+
duplicateId: rule('duplicate-id', 'error', 'The same g:id is used by more than one product'),
|
|
57
|
+
availabilityDate: rule('availability-date', 'error', 'preorder or backorder without a valid g:availability_date', doc('availability')),
|
|
58
|
+
priceFormat: rule('price-format', 'error', 'Price is not a number with an ISO 4217 currency, e.g. 15.00 PLN', doc('price')),
|
|
59
|
+
pricePrecision: rule('price-precision', 'warning', 'Price has more than 2 decimal places and will be rounded', doc('price')),
|
|
60
|
+
priceZero: rule('price-zero', 'error', 'Price is 0 (only allowed for phones on contract and subscriptions)', doc('price')),
|
|
61
|
+
salePrice: rule('sale-price', 'error', 'g:sale_price is not lower than g:price or uses another currency', doc('price')),
|
|
62
|
+
saleDate: rule('sale-date', 'error', 'g:sale_price_effective_date is not two ISO 8601 dates "start/end" with start before end'),
|
|
63
|
+
gtinFormat: rule('gtin-format', 'error', 'GTIN is not 8, 12, 13 or 14 digits', doc('gtin')),
|
|
64
|
+
gtinChecksum: rule('gtin-checksum', 'error', 'GTIN has a wrong check digit', doc('gtin')),
|
|
65
|
+
gtinRestricted: rule('gtin-restricted', 'error', 'GTIN uses a restricted or coupon prefix (2, 02, 04, 05, 98, 99)', doc('gtin')),
|
|
66
|
+
gtinCount: rule('gtin-count', 'error', 'More than 10 GTINs for one product', doc('gtin')),
|
|
67
|
+
identifiers: rule('identifiers', 'warning', 'No g:gtin, no g:brand with g:mpn and g:identifier_exists is not "no"', doc('gtin')),
|
|
68
|
+
identifierExistsMisused: rule('identifier-exists-misused', 'warning', 'g:identifier_exists is "no" but the product has a valid GTIN', doc('gtin')),
|
|
69
|
+
brandPlaceholder: rule('brand-placeholder', 'error', 'g:brand is a placeholder like "N/A" or "Generic"; leave it empty instead'),
|
|
70
|
+
urlScheme: rule('url-scheme', 'error', 'Link does not start with http:// or https://'),
|
|
71
|
+
http: rule('http', 'warning', 'Link or image uses http:// instead of https://'),
|
|
72
|
+
imageAscii: rule('image-url-ascii', 'error', 'Image URL has spaces or non-ASCII characters; percent-encode them', doc('image_link')),
|
|
73
|
+
imageFormat: rule('image-format', 'warning', 'Image extension is not jpg, png, webp, gif, bmp or tiff', doc('image_link')),
|
|
74
|
+
additionalImages: rule('additional-images-count', 'error', 'More than 10 g:additional_image_link'),
|
|
75
|
+
allCaps: rule('all-caps', 'warning', 'Title or description is written mostly in capital letters'),
|
|
76
|
+
promo: rule('promo-text', 'warning', 'Title has promotional text like "free shipping", "-50%" or "!!"'),
|
|
77
|
+
html: rule('html-in-text', 'warning', 'Title or description has HTML tags; Google expects plain text'),
|
|
78
|
+
color: rule('color-value', 'warning', 'g:color is a number, a code, a single letter or a non-color like "multicolor"'),
|
|
79
|
+
};
|
|
80
|
+
const PRICE = /^(\d+)(?:\.(\d+))?\s*([A-Za-z]{3})$/;
|
|
81
|
+
function parsePrice(value) {
|
|
82
|
+
const m = PRICE.exec(value);
|
|
83
|
+
if (!m) {
|
|
84
|
+
if (/^\d{1,3}(?:,\d{3})+(?:\.\d+)?\s*[A-Za-z]{3}$/.test(value))
|
|
85
|
+
return 'remove the thousands separator';
|
|
86
|
+
if (/^\d+,\d+/.test(value))
|
|
87
|
+
return 'use a dot as the decimal separator';
|
|
88
|
+
if (/^\d+(?:\.\d+)?$/.test(value))
|
|
89
|
+
return 'add a currency code, e.g. PLN';
|
|
90
|
+
return 'expected a format like 15.00 PLN';
|
|
91
|
+
}
|
|
92
|
+
const currency = m[3].toUpperCase();
|
|
93
|
+
if (!CURRENCIES.has(currency))
|
|
94
|
+
return `unknown currency ${m[3]}`;
|
|
95
|
+
return { amount: Number(`${m[1]}.${m[2] ?? '0'}`), decimals: m[2]?.length ?? 0, currency };
|
|
96
|
+
}
|
|
97
|
+
const DATE = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
98
|
+
function toTime(value) {
|
|
99
|
+
if (!DATE.test(value))
|
|
100
|
+
return undefined;
|
|
101
|
+
const time = Date.parse(value.replace(/([+-]\d{2})(\d{2})$/, '$1:$2'));
|
|
102
|
+
return Number.isNaN(time) ? undefined : time;
|
|
103
|
+
}
|
|
104
|
+
// Atom keeps its own <id>, <updated> and <link rel="self"> next to the product fields; Google reads <summary> as the description.
|
|
105
|
+
export function productFields(item, atom) {
|
|
106
|
+
const fields = new Map();
|
|
107
|
+
for (const child of item.children) {
|
|
108
|
+
let name = child.name;
|
|
109
|
+
if (atom && !child.google) {
|
|
110
|
+
if (name === 'id' || name === 'updated')
|
|
111
|
+
continue;
|
|
112
|
+
if (name === 'link' && child.attrs.rel && child.attrs.rel !== 'alternate')
|
|
113
|
+
continue;
|
|
114
|
+
if (name === 'summary')
|
|
115
|
+
name = 'description';
|
|
116
|
+
}
|
|
117
|
+
const list = fields.get(name);
|
|
118
|
+
if (list)
|
|
119
|
+
list.push(child);
|
|
120
|
+
else
|
|
121
|
+
fields.set(name, [child]);
|
|
122
|
+
}
|
|
123
|
+
for (const list of fields.values())
|
|
124
|
+
list.sort((a, b) => Number(Boolean(b.google)) - Number(Boolean(a.google)));
|
|
125
|
+
return fields;
|
|
126
|
+
}
|
|
127
|
+
export function googleChecker(report, parsed) {
|
|
128
|
+
const seen = new Map();
|
|
129
|
+
const atom = parsed.info?.root === 'feed';
|
|
130
|
+
return (item) => {
|
|
131
|
+
const fields = productFields(item, atom);
|
|
132
|
+
const node = (name) => fields.get(name)?.[0];
|
|
133
|
+
// Atom feeds put the product link in <link href="...">.
|
|
134
|
+
const text = (n) => (n ? n.text.trim() || n.attrs.href?.trim() || '' : '');
|
|
135
|
+
const value = (name) => text(node(name));
|
|
136
|
+
const id = value('id') || undefined;
|
|
137
|
+
const at = (n, detail) => ({ line: n?.line ?? item.line, id, detail });
|
|
138
|
+
for (const field of REQUIRED) {
|
|
139
|
+
if (!value(field) && !(ALTERNATIVE[field] && value(ALTERNATIVE[field])))
|
|
140
|
+
report(`google/missing-${field.replace(/_/g, '-')}`, at(undefined));
|
|
141
|
+
}
|
|
142
|
+
for (const [name, list] of fields) {
|
|
143
|
+
const max = LIMITS[name];
|
|
144
|
+
if (max === undefined)
|
|
145
|
+
continue;
|
|
146
|
+
for (const n of list) {
|
|
147
|
+
const length = [...text(n)].length;
|
|
148
|
+
if (length > max)
|
|
149
|
+
report(`google/${name.replace(/_/g, '-')}-length`, at(n, `${length} characters`));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (id) {
|
|
153
|
+
const first = seen.get(id);
|
|
154
|
+
if (first !== undefined)
|
|
155
|
+
report(R.duplicateId, at(node('id'), `first used on line ${first}`));
|
|
156
|
+
else
|
|
157
|
+
seen.set(id, node('id').line);
|
|
158
|
+
}
|
|
159
|
+
for (const [field, allowed] of Object.entries(ENUMS)) {
|
|
160
|
+
const n = node(field);
|
|
161
|
+
const v = text(n);
|
|
162
|
+
if (!v)
|
|
163
|
+
continue;
|
|
164
|
+
const normalized = field === 'energy_efficiency_class' ? v.toUpperCase() : v.toLowerCase().replace(/ /g, '_');
|
|
165
|
+
if (!allowed.includes(normalized))
|
|
166
|
+
report(`google/${field.replace(/_/g, '-')}-value`, at(n, `"${v}", expected ${allowed.join(', ')}`));
|
|
167
|
+
}
|
|
168
|
+
const availability = value('availability').toLowerCase().replace(/ /g, '_');
|
|
169
|
+
if (availability === 'preorder' || availability === 'backorder') {
|
|
170
|
+
const date = value('availability_date');
|
|
171
|
+
if (!date)
|
|
172
|
+
report(R.availabilityDate, at(node('availability'), `missing for ${availability}`));
|
|
173
|
+
else if (toTime(date) === undefined)
|
|
174
|
+
report(R.availabilityDate, at(node('availability_date'), `"${date}" is not ISO 8601`));
|
|
175
|
+
}
|
|
176
|
+
let price;
|
|
177
|
+
for (const field of ['price', 'sale_price']) {
|
|
178
|
+
const n = node(field);
|
|
179
|
+
const v = text(n);
|
|
180
|
+
if (!v)
|
|
181
|
+
continue;
|
|
182
|
+
const parsed = parsePrice(v);
|
|
183
|
+
if (typeof parsed === 'string') {
|
|
184
|
+
report(R.priceFormat, at(n, `g:${field} "${v}": ${parsed}`));
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (parsed.decimals > 2)
|
|
188
|
+
report(R.pricePrecision, at(n, `g:${field} "${v}"`));
|
|
189
|
+
if (field === 'price') {
|
|
190
|
+
price = parsed;
|
|
191
|
+
if (parsed.amount === 0)
|
|
192
|
+
report(R.priceZero, at(n));
|
|
193
|
+
}
|
|
194
|
+
else if (price && (parsed.currency !== price.currency || parsed.amount >= price.amount)) {
|
|
195
|
+
report(R.salePrice, at(n, `sale ${v}, regular ${value('price')}`));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const saleDate = value('sale_price_effective_date');
|
|
199
|
+
if (saleDate) {
|
|
200
|
+
const [start, end, ...rest] = saleDate.split('/').map((s) => s.trim());
|
|
201
|
+
const from = toTime(start ?? '');
|
|
202
|
+
const to = toTime(end ?? '');
|
|
203
|
+
if (rest.length || from === undefined || to === undefined || from >= to)
|
|
204
|
+
report(R.saleDate, at(node('sale_price_effective_date'), `"${saleDate}"`));
|
|
205
|
+
}
|
|
206
|
+
const gtins = (fields.get('gtin') ?? []).flatMap((n) => text(n)
|
|
207
|
+
.split(',')
|
|
208
|
+
.map((v) => v.trim())
|
|
209
|
+
.filter(Boolean)
|
|
210
|
+
.map((v) => ({ n, v })));
|
|
211
|
+
if (gtins.length > 10)
|
|
212
|
+
report(R.gtinCount, at(gtins[10].n, `${gtins.length} GTINs`));
|
|
213
|
+
let validGtin = false;
|
|
214
|
+
for (const { n, v } of gtins) {
|
|
215
|
+
const problem = checkGtin(v);
|
|
216
|
+
if (!problem)
|
|
217
|
+
validGtin = true;
|
|
218
|
+
else if (problem === 'format')
|
|
219
|
+
report(R.gtinFormat, at(n, `"${v}"`));
|
|
220
|
+
else if (problem === 'checksum')
|
|
221
|
+
report(R.gtinChecksum, at(n, `"${v}"`));
|
|
222
|
+
else
|
|
223
|
+
report(R.gtinRestricted, at(n, `"${v}" (${problem} prefix)`));
|
|
224
|
+
}
|
|
225
|
+
const identifierExists = value('identifier_exists').toLowerCase();
|
|
226
|
+
const condition = value('condition').toLowerCase();
|
|
227
|
+
if (!gtins.length && !(value('brand') && value('mpn')) && identifierExists !== 'no' && (condition === '' || condition === 'new')) {
|
|
228
|
+
report(R.identifiers, at(node('identifier_exists') ?? node('id')));
|
|
229
|
+
}
|
|
230
|
+
if (identifierExists === 'no' && validGtin)
|
|
231
|
+
report(R.identifierExistsMisused, at(node('identifier_exists')));
|
|
232
|
+
const brand = value('brand');
|
|
233
|
+
if (brand && BRAND_PLACEHOLDERS.has(brand.toLowerCase()))
|
|
234
|
+
report(R.brandPlaceholder, at(node('brand'), `"${brand}"`));
|
|
235
|
+
for (const field of ['link', 'mobile_link', 'image_link', 'additional_image_link']) {
|
|
236
|
+
for (const n of fields.get(field) ?? []) {
|
|
237
|
+
const url = text(n);
|
|
238
|
+
if (!url)
|
|
239
|
+
continue;
|
|
240
|
+
if (!isHttpUrl(url)) {
|
|
241
|
+
report(R.urlScheme, at(n, `g:${field} "${url.slice(0, 80)}"`));
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if ((field === 'link' || field === 'image_link') && /^http:/i.test(url))
|
|
245
|
+
report(R.http, at(n, `g:${field}`));
|
|
246
|
+
if (field.includes('image')) {
|
|
247
|
+
if (/[^\x21-\x7e]/.test(url))
|
|
248
|
+
report(R.imageAscii, at(n, url.slice(0, 120)));
|
|
249
|
+
const ext = urlExtension(url);
|
|
250
|
+
if (ext && !IMAGE_EXTENSIONS.has(ext))
|
|
251
|
+
report(R.imageFormat, at(n, `.${ext}`));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const additional = fields.get('additional_image_link') ?? [];
|
|
256
|
+
if (additional.length > 10)
|
|
257
|
+
report(R.additionalImages, at(additional[10], `${additional.length} images`));
|
|
258
|
+
for (const field of ['title', 'description']) {
|
|
259
|
+
const n = node(field);
|
|
260
|
+
const v = text(n);
|
|
261
|
+
if (!v)
|
|
262
|
+
continue;
|
|
263
|
+
if (isAllCaps(v))
|
|
264
|
+
report(R.allCaps, at(n, `g:${field}`));
|
|
265
|
+
const tags = htmlTags(v);
|
|
266
|
+
if (tags.length)
|
|
267
|
+
report(R.html, at(n, `g:${field}: <${[...new Set(tags)].slice(0, 5).join('>, <')}>`));
|
|
268
|
+
if (field === 'title') {
|
|
269
|
+
const promo = promoText(v);
|
|
270
|
+
if (promo)
|
|
271
|
+
report(R.promo, at(n, `"${promo}"`));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const color = value('color');
|
|
275
|
+
if (color && color.split('/').some((c) => BAD_COLOR.test(c.trim())))
|
|
276
|
+
report(R.color, at(node('color'), `"${color}"`));
|
|
277
|
+
};
|
|
278
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function promoText(text: string): string | undefined;
|
|
2
|
+
export declare function isAllCaps(text: string): boolean;
|
|
3
|
+
export declare function htmlTags(text: string): string[];
|
|
4
|
+
export declare const isHttpUrl: (url: string) => boolean;
|
|
5
|
+
export declare function urlExtension(url: string): string | undefined;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const PROMO = /\b(?:(?:free shipping|free delivery|best price|on sale|darmow[aey] (?:dostaw|wysy)|promocj|wyprzeda|najtaniej)\p{L}*|gratis(?!\p{L})|super okazj\p{L}*)|(?:^|\s)-\d{1,2}\s?%|!{2,}/iu;
|
|
2
|
+
export function promoText(text) {
|
|
3
|
+
return PROMO.exec(text)?.[0];
|
|
4
|
+
}
|
|
5
|
+
export function isAllCaps(text) {
|
|
6
|
+
const upper = text.match(/\p{Lu}/gu)?.length ?? 0;
|
|
7
|
+
const lower = text.match(/\p{Ll}/gu)?.length ?? 0;
|
|
8
|
+
return upper + lower >= 10 && upper / (upper + lower) > 0.7;
|
|
9
|
+
}
|
|
10
|
+
export function htmlTags(text) {
|
|
11
|
+
return [...text.matchAll(/<\/?([a-z][a-z0-9]*)\b[^>]*>/gi)].map((m) => m[1].toLowerCase());
|
|
12
|
+
}
|
|
13
|
+
export const isHttpUrl = (url) => /^https?:\/\/[^\s/]+/i.test(url);
|
|
14
|
+
export function urlExtension(url) {
|
|
15
|
+
try {
|
|
16
|
+
return new URL(url).pathname.match(/\.([a-z0-9]{2,5})$/i)?.[1].toLowerCase();
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mscode-pl/feed-lint",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Check Google Merchant and Ceneo product feeds before you upload them: missing fields, bad prices, wrong GTINs and broken links, with line numbers.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "MSCode (https://mscode.pl)",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"homepage": "https://github.com/mscode-pl/feed-lint#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/mscode-pl/feed-lint.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/mscode-pl/feed-lint/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"feed",
|
|
16
|
+
"product-feed",
|
|
17
|
+
"lint",
|
|
18
|
+
"linter",
|
|
19
|
+
"google-merchant",
|
|
20
|
+
"google-shopping",
|
|
21
|
+
"merchant-center",
|
|
22
|
+
"ceneo",
|
|
23
|
+
"xml",
|
|
24
|
+
"gtin",
|
|
25
|
+
"ecommerce",
|
|
26
|
+
"cli"
|
|
27
|
+
],
|
|
28
|
+
"bin": {
|
|
29
|
+
"feed-lint": "dist/cli/main.js"
|
|
30
|
+
},
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22"
|
|
42
|
+
},
|
|
43
|
+
"packageManager": "pnpm@12.14.0",
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc -p tsconfig.build.json",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"prepublishOnly": "pnpm typecheck && pnpm test && pnpm build"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"sax": "^1.6.1"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/node": "^24.19.4",
|
|
58
|
+
"@types/sax": "^1.2.7",
|
|
59
|
+
"typescript": "^5.9.3",
|
|
60
|
+
"vitest": "^5.1.2"
|
|
61
|
+
}
|
|
62
|
+
}
|