@rebilder/render-md 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -1
- package/dist/index.cjs +78 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +74 -1
- package/dist/index.d.ts +74 -1
- package/dist/index.js +78 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
// universal
|
|
25
25
|
renderDocumentMarkdown, // DocumentSource → any single page: facts, hours, contact, actions, prose
|
|
26
26
|
renderCollectionMarkdown, // CollectionSource → any listing page: link list or fact table
|
|
27
|
+
renderCollectionJsonLd, // CollectionSource → schema.org ItemList of positioned ListItems
|
|
27
28
|
formatMoney, // { amount: 8900, currency: 'USD' } → "$89.00" (minor units in, deterministic, zero-decimal aware: ¥4,900)
|
|
28
29
|
} from '@rebilder/render-md'
|
|
29
30
|
```
|
|
@@ -36,7 +37,37 @@ The fixed label maps (`BOOLEAN_LABELS`, `WEEKDAY_LABELS`, `PERIOD_SUFFIX`, `ACCE
|
|
|
36
37
|
|
|
37
38
|
## Rendering order (product)
|
|
38
39
|
|
|
39
|
-
Title (linked to canonical URL) → brand → price (`~~$120.00~~ $89.00` when a compare-at price exists in source) → availability → shipping → returns → variants table (id / title / options / price / availability) → description → attributes → images as markdown links. An agent reading top-down gets every buying fact in the first ~15 lines.
|
|
40
|
+
Title (linked to canonical URL) → brand → price (`~~$120.00~~ $89.00` when a compare-at price exists in source) → availability → shipping → returns → `- **Updated:**` → variants table (id / title / options / price / availability) → description → attributes → images as markdown links. An agent reading top-down gets every buying fact in the first ~15 lines.
|
|
41
|
+
|
|
42
|
+
`updated` sits at the end of the fact block rather than the start on purpose: it is metadata *about* the buying facts rather than one of them, and every line above `- **Availability:**` pushes a core fact further from the top of the document, which is a byte offset ARS D4 scores.
|
|
43
|
+
|
|
44
|
+
## Freshness and language
|
|
45
|
+
|
|
46
|
+
| Source type | `updated` | `language` |
|
|
47
|
+
|---|---|---|
|
|
48
|
+
| `ProductSource` | `dateModified` in JSON-LD, `- **Updated:**` in markdown | `inLanguage` in JSON-LD |
|
|
49
|
+
| `CollectionSource` | `dateModified` in JSON-LD | `inLanguage` in JSON-LD |
|
|
50
|
+
| `DocumentSource` | `- **Updated:**` in markdown | — |
|
|
51
|
+
|
|
52
|
+
`updated` is an ISO 8601 date or datetime; `language` is a BCP 47 tag (`en`, `pt-BR`, `zh-Hant`, `zh-Hant-TW`, `es-419`).
|
|
53
|
+
|
|
54
|
+
**`DocumentSource` has no `language` field on purpose.** There is no document JSON-LD renderer, so the only thing that consumes `language` would never see it, and a source field that silently renders nothing is a worse contract than an absent one. It arrives with `renderDocumentJsonLd`, which is unbuilt because choosing a schema.org `@type` per `DocumentKind` is an inference this package does not make — `DocumentKind` is an open union of merchant-supplied strings.
|
|
55
|
+
|
|
56
|
+
Both are **gates, never transforms.** A value that passes is emitted byte-for-byte as supplied — casing included, because BCP 47 is case-insensitive and re-casing a merchant's tag would make our JSON-LD disagree with their own `<html lang>`. A value that fails is dropped from *every* representation at once, so a fact can never be good enough for the markdown and not the JSON-LD.
|
|
57
|
+
|
|
58
|
+
The date check is a real-calendar check, not a `Date.parse` call: `Date.parse` rolls a day-of-month overflow silently forward (`2026-02-31` → March 3, `2026-02-29` → March 1 in a non-leap year), which is tolerable in a markdown line and not tolerable as `dateModified` in a product feed.
|
|
59
|
+
|
|
60
|
+
`renderProductJsonLd` feeds `/acp/v0/feed` and the MCP product tool as well as the canonical page, so anything it emits is published as an authoritative fact about the catalog.
|
|
61
|
+
|
|
62
|
+
## Collections as structured data
|
|
63
|
+
|
|
64
|
+
`renderCollectionJsonLd` renders a `CollectionSource` as a schema.org `ItemList` of positioned `ListItem`s. Three refusals are part of the contract:
|
|
65
|
+
|
|
66
|
+
- **`name` is omitted when the source has no title.** The markdown renderer falls back to the fixed `Contents` heading because a markdown document needs a heading; JSON-LD does not, and emitting the fallback would assert a name the merchant never wrote.
|
|
67
|
+
- **Item URLs pass the same scheme allowlist as the markdown path.** A rejected URL drops the link, not the item — the item still renders with its name, because dropping it would misreport the length of the listing.
|
|
68
|
+
- **`numberOfItems` counts what was emitted**, not `items.length`. Past the 500-row cap, a list announcing 900 while carrying 500 would send a paging agent after items that are not in the document.
|
|
69
|
+
|
|
70
|
+
Item facts are deliberately not projected into the list: `ListItem` has no honest slot for an arbitrary labelled fact, and inventing property names for merchant labels is the kind of inference this package does not do. The facts are in the markdown table.
|
|
40
71
|
|
|
41
72
|
## The universal surface
|
|
42
73
|
|
package/dist/index.cjs
CHANGED
|
@@ -58,6 +58,22 @@ function formatMoney(m) {
|
|
|
58
58
|
return symbol !== void 0 ? `${sign}${symbol}${grouped}${frac}` : `${sign}${code} ${grouped}${frac}`;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
// src/internal/validate.ts
|
|
62
|
+
var ISO_DATE = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
63
|
+
function isRealCalendarDate(value) {
|
|
64
|
+
const [year, month, day] = value.slice(0, 10).split("-").map(Number);
|
|
65
|
+
const probe = /* @__PURE__ */ new Date(0);
|
|
66
|
+
probe.setUTCFullYear(year, month - 1, day);
|
|
67
|
+
return probe.getUTCFullYear() === year && probe.getUTCMonth() === month - 1 && probe.getUTCDate() === day;
|
|
68
|
+
}
|
|
69
|
+
function isIsoDate(value) {
|
|
70
|
+
return ISO_DATE.test(value) && !Number.isNaN(Date.parse(value)) && isRealCalendarDate(value);
|
|
71
|
+
}
|
|
72
|
+
var BCP_47 = /^[A-Za-z]{2,3}(-[A-Za-z]{4})?(-(?:[A-Za-z]{2}|\d{3}))?$/;
|
|
73
|
+
function isLanguageTag(value) {
|
|
74
|
+
return BCP_47.test(value);
|
|
75
|
+
}
|
|
76
|
+
|
|
61
77
|
// src/jsonld.ts
|
|
62
78
|
var SCHEMA_AVAILABILITY = {
|
|
63
79
|
in_stock: "https://schema.org/InStock",
|
|
@@ -87,7 +103,9 @@ function renderProductJsonLd(p) {
|
|
|
87
103
|
if (p.brand !== void 0) out.brand = { "@type": "Brand", name: p.brand };
|
|
88
104
|
if (p.description !== void 0 && p.description.length > 0) out.description = p.description;
|
|
89
105
|
if (p.images !== void 0 && p.images.length > 0) out.image = p.images.map((img) => img.url);
|
|
106
|
+
if (p.language !== void 0 && isLanguageTag(p.language)) out.inLanguage = p.language;
|
|
90
107
|
out.offers = p.variants !== void 0 && p.variants.length > 0 ? p.variants.map((v) => offer(v.price, v.availability, { sku: v.sku, name: v.title })) : offer(p.price, p.availability, { url: p.url });
|
|
108
|
+
if (p.updated !== void 0 && isIsoDate(p.updated)) out.dateModified = p.updated;
|
|
91
109
|
return out;
|
|
92
110
|
}
|
|
93
111
|
|
|
@@ -110,6 +128,30 @@ function inline(text) {
|
|
|
110
128
|
function cell(text) {
|
|
111
129
|
return inline(text).replace(/\|/g, "\\|");
|
|
112
130
|
}
|
|
131
|
+
var ALLOWED_LINK_SCHEMES = ["https:", "http:", "mailto:", "tel:"];
|
|
132
|
+
function isSafeUrl(url) {
|
|
133
|
+
try {
|
|
134
|
+
return ALLOWED_LINK_SCHEMES.includes(new URL(url).protocol);
|
|
135
|
+
} catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function linkText(text) {
|
|
140
|
+
return inline(text).replace(/[[\]]/g, (c) => `\\${c}`);
|
|
141
|
+
}
|
|
142
|
+
function safeUrl(url) {
|
|
143
|
+
return url.replace(
|
|
144
|
+
/[()<>\s]/g,
|
|
145
|
+
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
function cellLinkText(text) {
|
|
149
|
+
return linkText(text).replace(/\|/g, "\\|");
|
|
150
|
+
}
|
|
151
|
+
function safeLink(label, url, labelEscaper = linkText) {
|
|
152
|
+
const text = labelEscaper(label);
|
|
153
|
+
return isSafeUrl(url) ? `[${text}](${safeUrl(url)})` : text;
|
|
154
|
+
}
|
|
113
155
|
function tailText(t) {
|
|
114
156
|
return `${t.headingLine}
|
|
115
157
|
|
|
@@ -177,7 +219,7 @@ function renderProductMarkdown(p, opts) {
|
|
|
177
219
|
const maxBytes = opts?.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
178
220
|
const level = clampHeadingLevel(opts?.headingLevel ?? 1);
|
|
179
221
|
const sub = clampHeadingLevel(level + 1);
|
|
180
|
-
const facts = [heading(level,
|
|
222
|
+
const facts = [heading(level, safeLink(p.title, p.url)), ""];
|
|
181
223
|
if (p.brand !== void 0) facts.push(`- **Brand:** ${inline(p.brand)}`);
|
|
182
224
|
const price = formatMoney(p.price);
|
|
183
225
|
facts.push(
|
|
@@ -201,10 +243,13 @@ function renderProductMarkdown(p, opts) {
|
|
|
201
243
|
if (p.returns.windowDays !== void 0) {
|
|
202
244
|
facts.push(` - Return window: ${p.returns.windowDays} days`);
|
|
203
245
|
}
|
|
204
|
-
if (p.returns.url !== void 0) {
|
|
246
|
+
if (p.returns.url !== void 0 && isSafeUrl(p.returns.url)) {
|
|
205
247
|
facts.push(` - Policy: ${p.returns.url}`);
|
|
206
248
|
}
|
|
207
249
|
}
|
|
250
|
+
if (p.updated !== void 0 && isIsoDate(p.updated)) {
|
|
251
|
+
facts.push(`- **Updated:** ${inline(p.updated)}`);
|
|
252
|
+
}
|
|
208
253
|
const requiredBlocks = [facts.join("\n")];
|
|
209
254
|
if (p.variants !== void 0 && p.variants.length > 0) {
|
|
210
255
|
requiredBlocks.push(variantsTable(p.variants, sub));
|
|
@@ -221,8 +266,8 @@ function renderProductMarkdown(p, opts) {
|
|
|
221
266
|
}
|
|
222
267
|
if (p.images !== void 0 && p.images.length > 0) {
|
|
223
268
|
const items = p.images.map((img) => {
|
|
224
|
-
const label = img.alt !== void 0 && img.alt.length > 0 ?
|
|
225
|
-
return `-
|
|
269
|
+
const label = img.alt !== void 0 && img.alt.length > 0 ? img.alt : img.url;
|
|
270
|
+
return `- ${safeLink(label, img.url)}`;
|
|
226
271
|
});
|
|
227
272
|
tail.push({ headingLine: heading(sub, "Images"), items });
|
|
228
273
|
}
|
|
@@ -231,7 +276,7 @@ function renderProductMarkdown(p, opts) {
|
|
|
231
276
|
function renderPolicyMarkdown(policies, opts) {
|
|
232
277
|
const maxBytes = opts?.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
233
278
|
const level = clampHeadingLevel(opts?.headingLevel ?? 1);
|
|
234
|
-
const titleLineFor = (pol) => heading(level,
|
|
279
|
+
const titleLineFor = (pol) => heading(level, safeLink(pol.title, pol.url));
|
|
235
280
|
const blocks = policies.map((pol) => `${titleLineFor(pol)}
|
|
236
281
|
|
|
237
282
|
${pol.body}`);
|
|
@@ -272,7 +317,7 @@ function renderCatalogMarkdown(items, opts) {
|
|
|
272
317
|
"| --- | --- | --- |"
|
|
273
318
|
].join("\n");
|
|
274
319
|
const rows = items.map(
|
|
275
|
-
(item) => `|
|
|
320
|
+
(item) => `| ${safeLink(item.title, item.url, cellLinkText)} | ${formatMoney(item.price)} | ${AVAILABILITY_LABELS[item.availability]} |`
|
|
276
321
|
);
|
|
277
322
|
const full = [head, ...rows].join("\n");
|
|
278
323
|
if (byteLength(full) <= maxBytes) return full;
|
|
@@ -370,10 +415,6 @@ function markdownLink(label, url) {
|
|
|
370
415
|
function cellLink(label, url) {
|
|
371
416
|
return markdownLink(label, url).replace(/\|/g, "\\|");
|
|
372
417
|
}
|
|
373
|
-
var ISO_DATE = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
374
|
-
function isIsoDate(value) {
|
|
375
|
-
return ISO_DATE.test(value) && !Number.isNaN(Date.parse(value));
|
|
376
|
-
}
|
|
377
418
|
function isIsoDay(value) {
|
|
378
419
|
return /^\d{4}-\d{2}-\d{2}$/.test(value) && !Number.isNaN(Date.parse(value));
|
|
379
420
|
}
|
|
@@ -717,6 +758,32 @@ ${TRUNCATION_NOTE}`;
|
|
|
717
758
|
return truncated ? `${out}${noteSuffix}` : out;
|
|
718
759
|
}
|
|
719
760
|
|
|
761
|
+
// src/documents/jsonld.ts
|
|
762
|
+
function listItem(item, position) {
|
|
763
|
+
const out = { "@type": "ListItem", position };
|
|
764
|
+
if (isAllowedUrl(item.url)) out.url = item.url;
|
|
765
|
+
out.name = item.title;
|
|
766
|
+
return out;
|
|
767
|
+
}
|
|
768
|
+
function renderCollectionJsonLd(collection) {
|
|
769
|
+
const out = {
|
|
770
|
+
"@context": "https://schema.org",
|
|
771
|
+
"@type": "ItemList"
|
|
772
|
+
};
|
|
773
|
+
if (isAllowedUrl(collection.url)) out.url = collection.url;
|
|
774
|
+
if (collection.title !== void 0) out.name = collection.title;
|
|
775
|
+
if (collection.language !== void 0 && isLanguageTag(collection.language)) {
|
|
776
|
+
out.inLanguage = collection.language;
|
|
777
|
+
}
|
|
778
|
+
const elements = collection.items.slice(0, MAX_COLLECTION_ROWS).map((item, index) => listItem(item, index + 1));
|
|
779
|
+
out.numberOfItems = elements.length;
|
|
780
|
+
out.itemListElement = elements;
|
|
781
|
+
if (collection.updated !== void 0 && isIsoDate(collection.updated)) {
|
|
782
|
+
out.dateModified = collection.updated;
|
|
783
|
+
}
|
|
784
|
+
return out;
|
|
785
|
+
}
|
|
786
|
+
|
|
720
787
|
exports.ACCESS_LABELS = ACCESS_LABELS;
|
|
721
788
|
exports.ALLOWED_URL_SCHEMES = ALLOWED_URL_SCHEMES;
|
|
722
789
|
exports.BOOLEAN_LABELS = BOOLEAN_LABELS;
|
|
@@ -728,6 +795,7 @@ exports.WEEKDAY_LABELS = WEEKDAY_LABELS;
|
|
|
728
795
|
exports.WEEKDAY_ORDER = WEEKDAY_ORDER;
|
|
729
796
|
exports.formatMoney = formatMoney;
|
|
730
797
|
exports.renderCatalogMarkdown = renderCatalogMarkdown;
|
|
798
|
+
exports.renderCollectionJsonLd = renderCollectionJsonLd;
|
|
731
799
|
exports.renderCollectionMarkdown = renderCollectionMarkdown;
|
|
732
800
|
exports.renderDocumentMarkdown = renderDocumentMarkdown;
|
|
733
801
|
exports.renderPolicyMarkdown = renderPolicyMarkdown;
|