@rebilder/agent-readability 0.2.0 → 0.4.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.
@@ -221,33 +221,61 @@ var ACTION_LEXICON = [
221
221
  "book now",
222
222
  "buy",
223
223
  "buy now",
224
+ "call",
224
225
  "call now",
226
+ "check availability",
227
+ "check out",
225
228
  "checkout",
226
229
  "contact",
227
230
  "contact us",
231
+ "create account",
232
+ "create an account",
228
233
  "donate",
229
234
  "download",
230
235
  "email us",
236
+ "enquire",
237
+ "enrol",
231
238
  "enroll",
239
+ "free trial",
240
+ "get a demo",
232
241
  "get a quote",
242
+ "get directions",
243
+ "get in touch",
233
244
  "get quote",
234
245
  "get started",
246
+ "get the app",
247
+ "get tickets",
248
+ "hire",
249
+ "inquire",
250
+ "install",
235
251
  "join",
236
252
  "make an appointment",
253
+ "message us",
237
254
  "order",
238
255
  "order now",
256
+ "order online",
257
+ "pre-order",
258
+ "preorder",
239
259
  "purchase",
240
260
  "register",
261
+ "rent",
241
262
  "request",
242
263
  "request a demo",
243
264
  "request a quote",
244
265
  "reserve",
245
266
  "schedule",
267
+ "shop",
268
+ "shop now",
246
269
  "sign up",
247
270
  "signup",
248
271
  "start",
249
272
  "start free trial",
250
- "subscribe"
273
+ "subscribe",
274
+ "talk to sales",
275
+ "talk to us",
276
+ "text us",
277
+ "try",
278
+ "upgrade"
251
279
  ];
252
280
  var BANDS = [
253
281
  {
@@ -287,7 +315,7 @@ var BANDS = [
287
315
  }
288
316
  ];
289
317
  var DEFAULT_RULESET = deepFreeze({
290
- version: "0.2.0",
318
+ version: "0.3.0",
291
319
  weights: WEIGHTS,
292
320
  thresholds: THRESHOLDS,
293
321
  audienceTokens: AUDIENCE_TOKENS,
@@ -303,6 +331,494 @@ var DEFAULT_RULESET = deepFreeze({
303
331
  bands: BANDS
304
332
  });
305
333
 
334
+ // src/html.ts
335
+ var Utf8Cursor = class {
336
+ constructor(source) {
337
+ this.source = source;
338
+ }
339
+ source;
340
+ charIndex = 0;
341
+ byteIndex = 0;
342
+ at(target) {
343
+ if (target < this.charIndex) {
344
+ this.charIndex = 0;
345
+ this.byteIndex = 0;
346
+ }
347
+ const limit = Math.min(target, this.source.length);
348
+ while (this.charIndex < limit) {
349
+ const code = this.source.charCodeAt(this.charIndex);
350
+ if (code < 128) {
351
+ this.byteIndex += 1;
352
+ this.charIndex += 1;
353
+ } else if (code < 2048) {
354
+ this.byteIndex += 2;
355
+ this.charIndex += 1;
356
+ } else if (code >= 55296 && code <= 56319) {
357
+ const next = this.charIndex + 1 < this.source.length ? this.source.charCodeAt(this.charIndex + 1) : 0;
358
+ if (next >= 56320 && next <= 57343 && this.charIndex + 1 < limit) {
359
+ this.byteIndex += 4;
360
+ this.charIndex += 2;
361
+ } else {
362
+ this.byteIndex += 3;
363
+ this.charIndex += 1;
364
+ }
365
+ } else {
366
+ this.byteIndex += 3;
367
+ this.charIndex += 1;
368
+ }
369
+ }
370
+ return this.byteIndex;
371
+ }
372
+ };
373
+ var NAMED_ENTITIES = {
374
+ amp: "&",
375
+ lt: "<",
376
+ gt: ">",
377
+ quot: '"',
378
+ apos: "'",
379
+ nbsp: "\xA0",
380
+ ensp: "\u2002",
381
+ emsp: "\u2003",
382
+ thinsp: "\u2009",
383
+ shy: "\xAD",
384
+ zwnj: "\u200C",
385
+ zwj: "\u200D",
386
+ // Currency
387
+ cent: "\xA2",
388
+ pound: "\xA3",
389
+ curren: "\xA4",
390
+ yen: "\xA5",
391
+ euro: "\u20AC",
392
+ dollar: "$",
393
+ // Typography
394
+ ndash: "\u2013",
395
+ mdash: "\u2014",
396
+ hellip: "\u2026",
397
+ lsquo: "\u2018",
398
+ rsquo: "\u2019",
399
+ sbquo: "\u201A",
400
+ ldquo: "\u201C",
401
+ rdquo: "\u201D",
402
+ bdquo: "\u201E",
403
+ laquo: "\xAB",
404
+ raquo: "\xBB",
405
+ lsaquo: "\u2039",
406
+ rsaquo: "\u203A",
407
+ bull: "\u2022",
408
+ middot: "\xB7",
409
+ dagger: "\u2020",
410
+ Dagger: "\u2021",
411
+ prime: "\u2032",
412
+ Prime: "\u2033",
413
+ // Marks and math
414
+ copy: "\xA9",
415
+ reg: "\xAE",
416
+ trade: "\u2122",
417
+ sect: "\xA7",
418
+ para: "\xB6",
419
+ deg: "\xB0",
420
+ plusmn: "\xB1",
421
+ times: "\xD7",
422
+ divide: "\xF7",
423
+ minus: "\u2212",
424
+ permil: "\u2030",
425
+ frac12: "\xBD",
426
+ frac14: "\xBC",
427
+ frac34: "\xBE",
428
+ sup2: "\xB2",
429
+ sup3: "\xB3",
430
+ le: "\u2264",
431
+ ge: "\u2265",
432
+ ne: "\u2260",
433
+ // Directionality (invisible, but they do occupy bytes)
434
+ lrm: "\u200E",
435
+ rlm: "\u200F"
436
+ };
437
+ var C1_REPLACEMENTS = {
438
+ 128: 8364,
439
+ 130: 8218,
440
+ 131: 402,
441
+ 132: 8222,
442
+ 133: 8230,
443
+ 134: 8224,
444
+ 135: 8225,
445
+ 136: 710,
446
+ 137: 8240,
447
+ 138: 352,
448
+ 139: 8249,
449
+ 140: 338,
450
+ 142: 381,
451
+ 145: 8216,
452
+ 146: 8217,
453
+ 147: 8220,
454
+ 148: 8221,
455
+ 149: 8226,
456
+ 150: 8211,
457
+ 151: 8212,
458
+ 152: 732,
459
+ 153: 8482,
460
+ 154: 353,
461
+ 155: 8250,
462
+ 156: 339,
463
+ 158: 382,
464
+ 159: 376
465
+ };
466
+ var ENTITY_RE = /&(#[xX][0-9a-fA-F]{1,6}|#[0-9]{1,7}|[a-zA-Z][a-zA-Z0-9]{0,31});/g;
467
+ function codePointToString(raw) {
468
+ const mapped = C1_REPLACEMENTS[raw];
469
+ const code = mapped ?? raw;
470
+ if (code === 0 || code > 1114111 || code >= 55296 && code <= 57343) return "\uFFFD";
471
+ return String.fromCodePoint(code);
472
+ }
473
+ function decodeEntities(text) {
474
+ if (!text.includes("&")) return text;
475
+ return text.replace(ENTITY_RE, (match, body) => {
476
+ if (body.startsWith("#")) {
477
+ const isHex = body[1] === "x" || body[1] === "X";
478
+ const digits = isHex ? body.slice(2) : body.slice(1);
479
+ const value = Number.parseInt(digits, isHex ? 16 : 10);
480
+ return Number.isNaN(value) ? match : codePointToString(value);
481
+ }
482
+ return NAMED_ENTITIES[body] ?? match;
483
+ });
484
+ }
485
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
486
+ "area",
487
+ "base",
488
+ "br",
489
+ "col",
490
+ "embed",
491
+ "hr",
492
+ "img",
493
+ "input",
494
+ "link",
495
+ "meta",
496
+ "param",
497
+ "source",
498
+ "track",
499
+ "wbr"
500
+ ]);
501
+ var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style"]);
502
+ var RCDATA_ELEMENTS = /* @__PURE__ */ new Set(["textarea", "title"]);
503
+ var IMPLIED_END_TAGS = {
504
+ li: ["li"],
505
+ dt: ["dt", "dd"],
506
+ dd: ["dt", "dd"],
507
+ option: ["option"],
508
+ optgroup: ["option", "optgroup"],
509
+ tr: ["td", "th", "tr"],
510
+ td: ["td", "th"],
511
+ th: ["td", "th"],
512
+ tbody: ["td", "th", "tr", "thead", "tbody"],
513
+ tfoot: ["td", "th", "tr", "thead", "tbody"]
514
+ };
515
+ var CLOSES_OPEN_P = /* @__PURE__ */ new Set([
516
+ "address",
517
+ "article",
518
+ "aside",
519
+ "blockquote",
520
+ "details",
521
+ "div",
522
+ "dl",
523
+ "fieldset",
524
+ "figcaption",
525
+ "figure",
526
+ "footer",
527
+ "form",
528
+ "h1",
529
+ "h2",
530
+ "h3",
531
+ "h4",
532
+ "h5",
533
+ "h6",
534
+ "header",
535
+ "hr",
536
+ "main",
537
+ "nav",
538
+ "ol",
539
+ "p",
540
+ "pre",
541
+ "section",
542
+ "table",
543
+ "ul"
544
+ ]);
545
+ var MAX_DEPTH = 256;
546
+ function isAsciiAlpha(ch) {
547
+ if (ch === void 0) return false;
548
+ return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z";
549
+ }
550
+ function isHtmlSpace(ch) {
551
+ return ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "\f";
552
+ }
553
+ var DISPLAY_NONE_RE = /(^|;)display:none(!important)?(;|$)/;
554
+ function hasInlineDisplayNone(style) {
555
+ if (style === void 0) return false;
556
+ return DISPLAY_NONE_RE.test(style.toLowerCase().replace(/\s+/g, ""));
557
+ }
558
+ function parseStartTag(html, start) {
559
+ let i = start + 1;
560
+ const nameStart = i;
561
+ while (i < html.length) {
562
+ const ch = html[i] ?? "";
563
+ if (isHtmlSpace(ch) || ch === "/" || ch === ">") break;
564
+ i++;
565
+ }
566
+ const tag = html.slice(nameStart, i).toLowerCase();
567
+ const attrs = {};
568
+ let selfClosing = false;
569
+ while (i < html.length) {
570
+ while (i < html.length && isHtmlSpace(html[i] ?? "")) i++;
571
+ if (i >= html.length) break;
572
+ const ch = html[i] ?? "";
573
+ if (ch === ">") {
574
+ i++;
575
+ return { tag, attrs, selfClosing, end: i };
576
+ }
577
+ if (ch === "/") {
578
+ selfClosing = true;
579
+ i++;
580
+ continue;
581
+ }
582
+ const attrNameStart = i;
583
+ while (i < html.length) {
584
+ const c = html[i] ?? "";
585
+ if (isHtmlSpace(c) || c === "=" || c === ">" || c === "/") break;
586
+ i++;
587
+ }
588
+ if (i === attrNameStart) {
589
+ i++;
590
+ continue;
591
+ }
592
+ const attrName = html.slice(attrNameStart, i).toLowerCase();
593
+ let value = "";
594
+ while (i < html.length && isHtmlSpace(html[i] ?? "")) i++;
595
+ if (html[i] === "=") {
596
+ i++;
597
+ while (i < html.length && isHtmlSpace(html[i] ?? "")) i++;
598
+ const quote = html[i];
599
+ if (quote === '"' || quote === "'") {
600
+ i++;
601
+ const valueStart = i;
602
+ const close = html.indexOf(quote, i);
603
+ if (close === -1) {
604
+ value = html.slice(valueStart);
605
+ i = html.length;
606
+ } else {
607
+ value = html.slice(valueStart, close);
608
+ i = close + 1;
609
+ }
610
+ } else {
611
+ const valueStart = i;
612
+ while (i < html.length) {
613
+ const c = html[i] ?? "";
614
+ if (isHtmlSpace(c) || c === ">") break;
615
+ i++;
616
+ }
617
+ value = html.slice(valueStart, i);
618
+ }
619
+ }
620
+ if (!(attrName in attrs)) attrs[attrName] = decodeEntities(value);
621
+ }
622
+ return { tag, attrs, selfClosing, end: i };
623
+ }
624
+ function findRawTextEnd(html, from, tag) {
625
+ const needle = `</${tag}`;
626
+ const lower = html.toLowerCase();
627
+ const at = lower.indexOf(needle, from);
628
+ return at === -1 ? -1 : at;
629
+ }
630
+ function ownExclusion(tag, attrs) {
631
+ if (tag === "script") {
632
+ const type = (attrs["type"] ?? "").toLowerCase().split(";")[0]?.trim() ?? "";
633
+ return type === "application/ld+json" ? "script-ld-json" : "script";
634
+ }
635
+ if (tag === "style") return "style";
636
+ if (tag === "template") return "template";
637
+ if ("hidden" in attrs) return "hidden-attribute";
638
+ if (hasInlineDisplayNone(attrs["style"])) return "inline-display-none";
639
+ return null;
640
+ }
641
+ function tokenizeHtml(html) {
642
+ const elements = [];
643
+ const texts = [];
644
+ const jsonLd = [];
645
+ const metas = [];
646
+ const links = [];
647
+ const stack = [];
648
+ const cursor = new Utf8Cursor(html);
649
+ const topIndex = () => stack[stack.length - 1] ?? -1;
650
+ const exclusionAt = (elementIndex) => elementIndex >= 0 ? elements[elementIndex]?.excludedBy ?? null : null;
651
+ const emitText = (from, to, decode, override) => {
652
+ if (to <= from) return;
653
+ const raw = html.slice(from, to);
654
+ const parent = topIndex();
655
+ const offset = cursor.at(from);
656
+ texts.push({
657
+ text: decode ? decodeEntities(raw) : raw,
658
+ offset,
659
+ bytes: cursor.at(to) - offset,
660
+ parent,
661
+ excludedBy: override ?? exclusionAt(parent)
662
+ });
663
+ };
664
+ const popTo = (tag) => {
665
+ for (let depth = stack.length - 1; depth >= 0; depth--) {
666
+ const candidate = stack[depth];
667
+ if (candidate === void 0) continue;
668
+ if (elements[candidate]?.tag === tag) {
669
+ stack.length = depth;
670
+ return;
671
+ }
672
+ }
673
+ };
674
+ const applyImpliedEndTags = (tag) => {
675
+ const closes = IMPLIED_END_TAGS[tag];
676
+ if (closes !== void 0) {
677
+ while (stack.length > 0) {
678
+ const top = stack[stack.length - 1];
679
+ const openTag = top === void 0 ? void 0 : elements[top]?.tag;
680
+ if (openTag === void 0 || !closes.includes(openTag)) break;
681
+ stack.pop();
682
+ }
683
+ }
684
+ if (CLOSES_OPEN_P.has(tag)) {
685
+ const top = stack[stack.length - 1];
686
+ if (top !== void 0 && elements[top]?.tag === "p") stack.pop();
687
+ }
688
+ };
689
+ let i = 0;
690
+ let textStart = 0;
691
+ while (i < html.length) {
692
+ const lt = html.indexOf("<", i);
693
+ if (lt === -1) break;
694
+ const next = html[lt + 1];
695
+ if (next !== "!" && next !== "/" && !isAsciiAlpha(next)) {
696
+ i = lt + 1;
697
+ continue;
698
+ }
699
+ emitText(textStart, lt, true);
700
+ if (next === "!") {
701
+ if (html.startsWith("<!--", lt)) {
702
+ const close = html.indexOf("-->", lt + 4);
703
+ i = close === -1 ? html.length : close + 3;
704
+ } else {
705
+ const close = html.indexOf(">", lt + 2);
706
+ i = close === -1 ? html.length : close + 1;
707
+ }
708
+ textStart = i;
709
+ continue;
710
+ }
711
+ if (next === "/") {
712
+ const close = html.indexOf(">", lt + 2);
713
+ const end = close === -1 ? html.length : close + 1;
714
+ const rawName = html.slice(lt + 2, close === -1 ? html.length : close).trim();
715
+ const tag = rawName.toLowerCase();
716
+ if (tag.length > 0) popTo(tag);
717
+ i = end;
718
+ textStart = i;
719
+ continue;
720
+ }
721
+ const parsed = parseStartTag(html, lt);
722
+ applyImpliedEndTags(parsed.tag);
723
+ const parent = topIndex();
724
+ const own = ownExclusion(parsed.tag, parsed.attrs);
725
+ const element = {
726
+ index: elements.length,
727
+ tag: parsed.tag,
728
+ attrs: parsed.attrs,
729
+ offset: cursor.at(lt),
730
+ parent,
731
+ depth: stack.length,
732
+ excludedBy: exclusionAt(parent) ?? own
733
+ };
734
+ elements.push(element);
735
+ collectMetaOrLink(element, metas, links);
736
+ i = parsed.end;
737
+ const isVoid = VOID_ELEMENTS.has(parsed.tag);
738
+ const isRawText = RAW_TEXT_ELEMENTS.has(parsed.tag);
739
+ const isRcdata = RCDATA_ELEMENTS.has(parsed.tag);
740
+ if (isRawText || isRcdata) {
741
+ const contentStart = i;
742
+ const close = findRawTextEnd(html, contentStart, parsed.tag);
743
+ const contentEnd = close === -1 ? html.length : close;
744
+ if (contentEnd > contentStart) {
745
+ const wasTop = stack.length;
746
+ stack.push(element.index);
747
+ emitText(contentStart, contentEnd, isRcdata, element.excludedBy ?? own ?? void 0);
748
+ stack.length = wasTop;
749
+ if (own === "script-ld-json") {
750
+ jsonLd.push({
751
+ text: html.slice(contentStart, contentEnd),
752
+ offset: cursor.at(contentStart),
753
+ element: element.index
754
+ });
755
+ }
756
+ }
757
+ if (close === -1) {
758
+ i = html.length;
759
+ } else {
760
+ const gt = html.indexOf(">", close);
761
+ i = gt === -1 ? html.length : gt + 1;
762
+ }
763
+ textStart = i;
764
+ continue;
765
+ }
766
+ if (!isVoid && !parsed.selfClosing && stack.length < MAX_DEPTH) stack.push(element.index);
767
+ textStart = i;
768
+ }
769
+ emitText(textStart, html.length, true);
770
+ return {
771
+ elements,
772
+ texts,
773
+ jsonLd,
774
+ metas,
775
+ links,
776
+ bytes: cursor.at(html.length)
777
+ };
778
+ }
779
+ function collectMetaOrLink(element, metas, links) {
780
+ const lower = (value) => value === void 0 ? null : value.toLowerCase();
781
+ if (element.tag === "meta") {
782
+ metas.push({
783
+ name: lower(element.attrs["name"]),
784
+ property: lower(element.attrs["property"]),
785
+ httpEquiv: lower(element.attrs["http-equiv"]),
786
+ itemprop: lower(element.attrs["itemprop"]),
787
+ charset: lower(element.attrs["charset"]),
788
+ content: element.attrs["content"] ?? null,
789
+ element: element.index,
790
+ offset: element.offset
791
+ });
792
+ return;
793
+ }
794
+ if (element.tag === "link") {
795
+ const rawRel = element.attrs["rel"] ?? "";
796
+ const rel = [];
797
+ for (const token of rawRel.toLowerCase().split(/\s+/)) {
798
+ if (token.length > 0 && !rel.includes(token)) rel.push(token);
799
+ }
800
+ links.push({
801
+ rel,
802
+ href: element.attrs["href"] ?? null,
803
+ type: lower(element.attrs["type"]),
804
+ hreflang: lower(element.attrs["hreflang"]),
805
+ title: element.attrs["title"] ?? null,
806
+ element: element.index,
807
+ offset: element.offset
808
+ });
809
+ }
810
+ }
811
+ function joinText(parts) {
812
+ return parts.join(" ").replace(/\s+/g, " ").trim();
813
+ }
814
+ function countedText(doc) {
815
+ const parts = [];
816
+ for (const node of doc.texts) {
817
+ if (node.excludedBy === null) parts.push(node.text);
818
+ }
819
+ return joinText(parts);
820
+ }
821
+
306
822
  // src/extract.ts
307
823
  function utf8Length(text) {
308
824
  let bytes = 0;
@@ -320,6 +836,100 @@ function utf8Length(text) {
320
836
  }
321
837
  return bytes;
322
838
  }
839
+ var SCHEMA_PREFIX = /^https?:\/\/schema\.org\//;
840
+ function stripSchemaPrefix(type) {
841
+ return type.replace(SCHEMA_PREFIX, "");
842
+ }
843
+ function typesOf(value) {
844
+ const raw = value["@type"];
845
+ if (typeof raw === "string") return [stripSchemaPrefix(raw)];
846
+ if (Array.isArray(raw)) {
847
+ const out = [];
848
+ for (const entry of raw) if (typeof entry === "string") out.push(stripSchemaPrefix(entry));
849
+ return out;
850
+ }
851
+ return [];
852
+ }
853
+ var JSON_LD_MAX_DEPTH = 12;
854
+ function collectNodes(value, offset, depth, out, seen) {
855
+ if (depth > JSON_LD_MAX_DEPTH || value === null || typeof value !== "object") return;
856
+ if (seen.has(value)) return;
857
+ seen.add(value);
858
+ if (Array.isArray(value)) {
859
+ for (const entry of value) collectNodes(entry, offset, depth + 1, out, seen);
860
+ return;
861
+ }
862
+ const record = value;
863
+ const types = typesOf(record);
864
+ if (types.length > 0) out.push({ types, value: record, offset });
865
+ for (const [key, child] of Object.entries(record)) {
866
+ if (key === "@context") continue;
867
+ collectNodes(child, offset, depth + 1, out, seen);
868
+ }
869
+ }
870
+ function parseJsonLd(blocks) {
871
+ const nodes = [];
872
+ let parsed = 0;
873
+ for (const block of blocks) {
874
+ let value;
875
+ try {
876
+ value = JSON.parse(block.text);
877
+ } catch {
878
+ continue;
879
+ }
880
+ parsed++;
881
+ collectNodes(value, block.offset, 0, nodes, /* @__PURE__ */ new Set());
882
+ }
883
+ return { nodes, blocks: blocks.length, parsed };
884
+ }
885
+ function header(headers, name) {
886
+ const values = headers[name];
887
+ return values !== void 0 && values.length > 0 ? values[0] ?? null : null;
888
+ }
889
+ function headerAll(headers, name) {
890
+ return headers[name] ?? [];
891
+ }
892
+ function mediaType(contentType) {
893
+ if (contentType === null) return null;
894
+ const semi = contentType.indexOf(";");
895
+ const value = (semi === -1 ? contentType : contentType.slice(0, semi)).trim().toLowerCase();
896
+ return value.length > 0 ? value : null;
897
+ }
898
+ function classifyMediaType(type) {
899
+ if (type === null) return "other";
900
+ if (type === "text/html" || type === "application/xhtml+xml") return "html";
901
+ if (type === "text/markdown" || type === "text/x-markdown") return "markdown";
902
+ if (type === "text/plain") return "text";
903
+ if (type === "application/json" || type === "application/ld+json" || type.endsWith("+json")) {
904
+ return "json";
905
+ }
906
+ return "other";
907
+ }
908
+ function bodyLooksLikeHtml(body) {
909
+ const head = body.slice(0, 1024).trimStart().toLowerCase();
910
+ return head.startsWith("<!doctype html") || head.startsWith("<html") || head.startsWith("<?xml") ? true : head.includes("<html") || head.includes("<head>") || head.includes("<body");
911
+ }
912
+ function buildRepresentation(capture) {
913
+ if (typeof capture.body !== "string") return null;
914
+ const contentType = mediaType(header(capture.headers, "content-type"));
915
+ const declared = classifyMediaType(contentType);
916
+ const looksLikeHtml = bodyLooksLikeHtml(capture.body);
917
+ const doc = declared === "html" || looksLikeHtml ? tokenizeHtml(capture.body) : null;
918
+ const jsonLd = doc !== null ? parseJsonLd(doc.jsonLd.map((block) => ({ text: block.text, offset: block.offset }))) : declared === "json" ? parseJsonLd([{ text: capture.body, offset: 0 }]) : { nodes: [], blocks: 0, parsed: 0 };
919
+ return {
920
+ kind: declared,
921
+ contentType,
922
+ body: capture.body,
923
+ bytes: capture.bytes,
924
+ doc,
925
+ jsonLd,
926
+ text: doc !== null ? countedText(doc) : capture.body,
927
+ looksLikeHtml,
928
+ truncated: capture.truncated,
929
+ finalUrl: capture.finalUrl,
930
+ headers: capture.headers
931
+ };
932
+ }
323
933
  function parseRobots(text) {
324
934
  const groups = [];
325
935
  const sitemaps = [];
@@ -443,8 +1053,14 @@ deepFreeze2({
443
1053
  band: [100, 7, 75, 5, 50, 3, 25, 1],
444
1054
  noscriptFloor: 1
445
1055
  },
446
- /** D2.1, 9. All-or-nothing: a machine media type AND a body that is not HTML. */
447
- negotiatedResponse: { full: 9 },
1056
+ /**
1057
+ * D2.1, 9 for a machine copy served at the page's own address on `Accept`
1058
+ * negotiation: a machine media type AND a body that is not HTML. ARS 0.3: 6
1059
+ * when the page does not negotiate but the Markdown copy it declares as an
1060
+ * alternate was fetched and is real. Less than full because only agents that
1061
+ * look for the link reach it, and they pay a second request to do so.
1062
+ */
1063
+ negotiatedResponse: { full: 9, linkedCopy: 6 },
448
1064
  /** D2.2, 3. All-or-nothing. */
449
1065
  declaredAlternates: { full: 3 },
450
1066
  /** D2.3, 3. All-or-nothing, and 0 unless D2.1 > 0. */
@@ -596,7 +1212,107 @@ function sha256Hex(message) {
596
1212
  for (const word of h) hex += word.toString(16).padStart(8, "0");
597
1213
  return hex;
598
1214
  }
599
- sha256Hex("ars-0.2-corpus-unfrozen");
1215
+ sha256Hex("ars-0.3-corpus-unfrozen");
1216
+ function parseLinkHeaders(values) {
1217
+ const out = [];
1218
+ for (const value of values) {
1219
+ for (const entry of splitLinkEntries(value)) {
1220
+ const open = entry.indexOf("<");
1221
+ const close = entry.indexOf(">", open + 1);
1222
+ if (open === -1 || close === -1) continue;
1223
+ const url = entry.slice(open + 1, close).trim();
1224
+ if (url.length === 0) continue;
1225
+ let rel = [];
1226
+ let type = null;
1227
+ for (const param of entry.slice(close + 1).split(";")) {
1228
+ const equals = param.indexOf("=");
1229
+ if (equals === -1) continue;
1230
+ const name = param.slice(0, equals).trim().toLowerCase();
1231
+ const raw = param.slice(equals + 1).trim();
1232
+ const unquoted = raw.startsWith('"') && raw.endsWith('"') ? raw.slice(1, -1) : raw;
1233
+ if (name === "rel")
1234
+ rel = unquoted.toLowerCase().split(/\s+/).filter((t) => t.length > 0);
1235
+ else if (name === "type") type = unquoted.trim().toLowerCase();
1236
+ }
1237
+ out.push({ url, rel, type });
1238
+ }
1239
+ }
1240
+ return out;
1241
+ }
1242
+ function splitLinkEntries(value) {
1243
+ const entries = [];
1244
+ let depth = 0;
1245
+ let quoted = false;
1246
+ let start = 0;
1247
+ for (let i = 0; i < value.length; i++) {
1248
+ const char = value.charAt(i);
1249
+ if (quoted) {
1250
+ if (char === '"') quoted = false;
1251
+ continue;
1252
+ }
1253
+ if (char === '"') quoted = true;
1254
+ else if (char === "<") depth++;
1255
+ else if (char === ">") depth = depth > 0 ? depth - 1 : 0;
1256
+ else if (char === "," && depth === 0) {
1257
+ entries.push(value.slice(start, i));
1258
+ start = i + 1;
1259
+ }
1260
+ }
1261
+ entries.push(value.slice(start));
1262
+ return entries.map((entry) => entry.trim()).filter((entry) => entry.length > 0);
1263
+ }
1264
+ var MACHINE_KINDS = /* @__PURE__ */ new Set(["markdown", "text", "json"]);
1265
+ function isMachineCopy(capture) {
1266
+ const representation = buildRepresentation(capture);
1267
+ return representation !== null && MACHINE_KINDS.has(representation.kind) && !representation.looksLikeHtml;
1268
+ }
1269
+ var MARKDOWN_MEDIA = /* @__PURE__ */ new Set(["text/markdown", "text/x-markdown"]);
1270
+ function isMarkdownType(type) {
1271
+ return type !== null && MARKDOWN_MEDIA.has((type.split(";")[0] ?? "").trim().toLowerCase());
1272
+ }
1273
+ function declaredMarkdownAlternates(captures) {
1274
+ const out = [];
1275
+ const add = (raw, base) => {
1276
+ try {
1277
+ const url = new URL(raw, base);
1278
+ url.hash = "";
1279
+ const href = url.toString();
1280
+ if (!out.includes(href)) out.push(href);
1281
+ } catch {
1282
+ }
1283
+ };
1284
+ for (const capture of captures) {
1285
+ if (capture === null) continue;
1286
+ for (const link of parseLinkHeaders(headerAll(capture.headers, "link"))) {
1287
+ if (link.rel.includes("alternate") && isMarkdownType(link.type)) add(link.url, capture.finalUrl);
1288
+ }
1289
+ }
1290
+ for (const capture of captures) {
1291
+ if (capture === null) continue;
1292
+ const representation = buildRepresentation(capture);
1293
+ if (representation === null || representation.doc === null) continue;
1294
+ for (const link of representation.doc.links) {
1295
+ if (!link.rel.includes("alternate") || link.href === null) continue;
1296
+ if (isMarkdownType(link.type)) add(link.href, capture.finalUrl);
1297
+ }
1298
+ }
1299
+ return out;
1300
+ }
1301
+ function markdownAlternateTarget(agent, browser, origin) {
1302
+ if (agent === null || agent.status < 200 || agent.status >= 300) return null;
1303
+ if (isMachineCopy(agent)) return null;
1304
+ const own = /* @__PURE__ */ new Set([agent.finalUrl, agent.requestedUrl]);
1305
+ for (const href of declaredMarkdownAlternates([browser, agent])) {
1306
+ let sameOrigin = false;
1307
+ try {
1308
+ sameOrigin = new URL(href).origin === origin;
1309
+ } catch {
1310
+ sameOrigin = false;
1311
+ }
1312
+ if (sameOrigin && !own.has(href)) return href;
1313
+ }
1314
+ return null;
1315
+ }
600
1316
  var ALLOW_PRIVATE_HOSTS = /* @__PURE__ */ Symbol("rebilder.ars.probe.allow-private-hosts");
601
1317
  var allowsPrivateHosts = (policy) => policy === ALLOW_PRIVATE_HOSTS;
602
1318
  var NOT_AN_IP = { family: null, reserved: false, label: "hostname" };
@@ -1044,7 +1760,7 @@ function createLimiter(options = {}) {
1044
1760
  }
1045
1761
 
1046
1762
  // src/probe/index.ts
1047
- var ARS_USER_AGENT = "rebilder-ars/0.2 (+https://rebilder.com/bots)";
1763
+ var ARS_USER_AGENT = "rebilder-ars/0.3 (+https://rebilder.com/bots)";
1048
1764
  var AGENT_IDENTITY = {
1049
1765
  role: "agent",
1050
1766
  userAgent: ARS_USER_AGENT,
@@ -1384,6 +2100,16 @@ async function probeWithHostPolicy(target, policy, hostPolicy, options = {}) {
1384
2100
  }
1385
2101
  const agent = await run(url.toString(), agentHeaders);
1386
2102
  const browser = await run(url.toString(), browserHeaders);
2103
+ const linked = markdownAlternateTarget(
2104
+ agent.result.ok ? agent.result.capture : null,
2105
+ browser.result.ok ? browser.result.capture : null,
2106
+ origin
2107
+ );
2108
+ let markdownAlternate = null;
2109
+ if (linked !== null) {
2110
+ const rule = disallowedFor(new URL(linked).pathname);
2111
+ markdownAlternate = rule !== null ? skipped(agentHeaders, `robots.txt disallows ${ARS_ROBOTS_TOKEN} for ${linked}`) : await run(linked, agentHeaders);
2112
+ }
1387
2113
  const fetchAsset = async (path) => {
1388
2114
  const rule = disallowedFor(path);
1389
2115
  if (rule !== null)
@@ -1392,7 +2118,17 @@ async function probeWithHostPolicy(target, policy, hostPolicy, options = {}) {
1392
2118
  };
1393
2119
  const llmsTxt = await fetchAsset("/llms.txt");
1394
2120
  const wellKnownUcp = await fetchAsset("/.well-known/ucp");
1395
- return evidenceOf({ agent, browser, parityConfirm: null, robotsTxt, llmsTxt, wellKnownUcp });
2121
+ return evidenceOf({
2122
+ agent,
2123
+ browser,
2124
+ parityConfirm: null,
2125
+ // Omitted when not taken, so a page with no linked copy produces exactly
2126
+ // the bundle (and the evidence hash) it produced under 0.2.
2127
+ ...markdownAlternate === null ? {} : { markdownAlternate },
2128
+ robotsTxt,
2129
+ llmsTxt,
2130
+ wellKnownUcp
2131
+ });
1396
2132
  } catch (error) {
1397
2133
  if (error instanceof ProbeBudgetExceededError) {
1398
2134
  return { ok: false, rejection: "budget-exceeded", detail: error.message };