@sarimarcus/content-sites-core 0.30.0 → 0.31.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 +18 -0
- package/dist/config/outboundLinkRel.d.ts +2 -2
- package/dist/config/outboundLinkRel.js +11 -32
- package/dist/dates/index.d.ts +2 -0
- package/dist/dates/index.js +1 -0
- package/dist/dates/seasonalDates.d.ts +43 -0
- package/dist/dates/seasonalDates.js +111 -0
- package/dist/interludes/bandSlot.d.ts +8 -0
- package/dist/interludes/bandSlot.js +18 -0
- package/dist/interludes/index.d.ts +1 -1
- package/dist/interludes/index.js +1 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +1 -0
- package/dist/schema/price.d.ts +19 -0
- package/dist/schema/price.js +50 -0
- package/dist/text/cardCopy.d.ts +1 -0
- package/dist/text/cardCopy.js +7 -0
- package/dist/text/index.d.ts +2 -0
- package/dist/text/index.js +2 -0
- package/dist/text/markdown.d.ts +28 -0
- package/dist/text/markdown.js +122 -0
- package/dist/url/index.d.ts +1 -1
- package/dist/url/index.js +1 -1
- package/dist/url/outbound.d.ts +8 -0
- package/dist/url/outbound.js +39 -0
- package/package.json +1 -1
- package/src/config/outboundLinkRel.ts +10 -28
- package/src/dates/index.ts +2 -0
- package/src/dates/seasonalDates.ts +146 -0
- package/src/interludes/bandSlot.ts +14 -0
- package/src/interludes/index.ts +2 -0
- package/src/schema/index.ts +1 -0
- package/src/schema/price.ts +59 -0
- package/src/text/cardCopy.ts +6 -0
- package/src/text/index.ts +2 -0
- package/src/text/markdown.ts +131 -0
- package/src/url/index.ts +1 -1
- package/src/url/outbound.ts +35 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
All packages in the platform release in lockstep; entries are per release version.
|
|
4
4
|
|
|
5
|
+
## 0.31.0 (2026-10-08)
|
|
6
|
+
|
|
7
|
+
- `./text` adds `parseMarkdown`, `parseMarkdownInline`, `parseMarkdownBlock` and `stripMarkdown`, the sites' `src/utils/markdown.ts` (Granada's aligned copy, same signatures). They no longer apply Rule 38 rel at render time (SLA-3016).
|
|
8
|
+
- `./url` adds `applyOutboundRel(html, classify)`, the single Rule 38 anchor rewriter; `outboundLinkRel` (build:done) runs it as the only rel enforcer, with unchanged options (SLA-3016).
|
|
9
|
+
- `outboundLinkRel`: a new or empty `rel` is written as `"<policy> noopener"` (was `"noopener <policy>"`) and an empty `rel=""` is replaced, never duplicated; `href`/`rel` match only as whole attributes (`data-href`/`data-rel` no longer count); the policy check reads rel tokens only, so `nofollow` in an href, title or class no longer suppresses it; an uppercase token counts; single-quoted `rel='…'` is rewritten with double quotes; an anchor whose rel already holds the policy token is left as is (1 Granada anchor no longer gains an inert `noopener`) (SLA-3016).
|
|
10
|
+
- `astro dev` previews no longer show Rule 38 rel on markdown prose links; production dist is unaffected (SLA-3016).
|
|
11
|
+
- `./dates` adds `resolvePeriodDates` and the `Period`/`StayDates` types, `./text` adds `formatFromPrice`, `./schema` adds `parsePriceRange` and `calculateAggregatePriceRange` (ASCII-hyphen ranges only), `./interludes` adds `pickBandSlot`, all moved from the sites' `utils/{seasonalDates,cardCopy,price,bodySections}.ts` (SLA-3017).
|
|
12
|
+
|
|
13
|
+
### Breaking
|
|
14
|
+
|
|
15
|
+
- The sites' `src/utils/markdown.ts`, `utils/seasonalDates.ts`, `utils/cardCopy.ts`, `utils/price.ts` and `bodySections.ts#pickBandSlot` now live in core and `validate-no-recreate` refuses a local copy. Sites run `rewrite-imports.mjs --delete-local`, then `scripts/platform/migrations/core-markdown.mjs` (asserts the markdown move, writes nothing) and `scripts/platform/migrations/promoted-site-files.mjs`. Dark Skies FAQPage JSON-LD on 8 pages loses HTML its old `stripMarkdown` leaked (SLA-3016, SLA-3017).
|
|
16
|
+
- `rewrite-imports.mjs` applies only the extracted.json entries of packages the site's package.json declares, so Dark Skies keeps its own GuideCard and ArticleCardGrid (SLA-3017).
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## 0.30.1 (2026-10-07)
|
|
20
|
+
|
|
21
|
+
- No changes; lockstep release with ui (SLA-2989).
|
|
22
|
+
|
|
5
23
|
## 0.30.0 (2026-10-07)
|
|
6
24
|
|
|
7
25
|
- No changes; lockstep release with ui (SLA-2985).
|
|
@@ -5,7 +5,7 @@ export interface OutboundLinkRelOptions {
|
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
7
|
* Astro integration enforcing the outbound rel policy on every anchor of the built HTML, whichever component
|
|
8
|
-
* rendered it:
|
|
9
|
-
* everything else is untouched.
|
|
8
|
+
* rendered it (markdown prose included: the core renderers add no rel): competitor-classified hosts gain `nofollow`,
|
|
9
|
+
* affiliate-classified hosts gain `sponsored`, and everything else is untouched. Rules: `applyOutboundRel`.
|
|
10
10
|
*/
|
|
11
11
|
export declare function outboundLinkRel({ root }: OutboundLinkRelOptions): AstroIntegration;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
|
-
import { outboundClassifier } from "../url/outbound.js";
|
|
4
|
+
import { applyOutboundRel, outboundClassifier } from "../url/outbound.js";
|
|
5
5
|
function walk(dir, out = []) {
|
|
6
6
|
let entries;
|
|
7
7
|
try {
|
|
@@ -19,19 +19,10 @@ function walk(dir, out = []) {
|
|
|
19
19
|
}
|
|
20
20
|
return out;
|
|
21
21
|
}
|
|
22
|
-
function mergeRel(attrs, tokens) {
|
|
23
|
-
if (/\brel\s*=\s*["'][^"']*["']/i.test(attrs)) {
|
|
24
|
-
return attrs.replace(/\brel\s*=\s*["']([^"']*)["']/i, (_m, existing) => {
|
|
25
|
-
const set = new Set([...existing.split(/\s+/).filter(Boolean), ...tokens]);
|
|
26
|
-
return `rel="${[...set].join(' ')}"`;
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
return `${attrs} rel="${tokens.join(' ')}"`;
|
|
30
|
-
}
|
|
31
22
|
/**
|
|
32
23
|
* Astro integration enforcing the outbound rel policy on every anchor of the built HTML, whichever component
|
|
33
|
-
* rendered it:
|
|
34
|
-
* everything else is untouched.
|
|
24
|
+
* rendered it (markdown prose included: the core renderers add no rel): competitor-classified hosts gain `nofollow`,
|
|
25
|
+
* affiliate-classified hosts gain `sponsored`, and everything else is untouched. Rules: `applyOutboundRel`.
|
|
35
26
|
*/
|
|
36
27
|
export function outboundLinkRel({ root }) {
|
|
37
28
|
const siteRoot = root instanceof URL ? fileURLToPath(root) : root;
|
|
@@ -43,28 +34,16 @@ export function outboundLinkRel({ root }) {
|
|
|
43
34
|
const outDir = fileURLToPath(dir);
|
|
44
35
|
let files = 0;
|
|
45
36
|
let anchors = 0;
|
|
37
|
+
const counted = (url) => {
|
|
38
|
+
const cls = classify(url);
|
|
39
|
+
if (cls === 'competitor' || cls === 'affiliate')
|
|
40
|
+
anchors++;
|
|
41
|
+
return cls;
|
|
42
|
+
};
|
|
46
43
|
for (const file of walk(outDir)) {
|
|
47
44
|
const original = fs.readFileSync(file, 'utf-8');
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const href = attrs.match(/href\s*=\s*"([^"]*)"/i)?.[1] ?? attrs.match(/href\s*=\s*'([^']*)'/i)?.[1];
|
|
51
|
-
if (!href || !/^https?:\/\//i.test(href))
|
|
52
|
-
return tag;
|
|
53
|
-
const cls = classify(href);
|
|
54
|
-
if (cls !== 'competitor' && cls !== 'affiliate')
|
|
55
|
-
return tag;
|
|
56
|
-
anchors++;
|
|
57
|
-
const tokens = [];
|
|
58
|
-
if (cls === 'competitor' && !/\bnofollow\b/i.test(attrs))
|
|
59
|
-
tokens.push('nofollow');
|
|
60
|
-
if (cls === 'affiliate' && !/\bsponsored\b/i.test(attrs))
|
|
61
|
-
tokens.push('sponsored');
|
|
62
|
-
if (!tokens.length)
|
|
63
|
-
return tag;
|
|
64
|
-
changed = true;
|
|
65
|
-
return `<a${mergeRel(attrs, ['noopener', ...tokens])}>`;
|
|
66
|
-
});
|
|
67
|
-
if (changed)
|
|
45
|
+
const html = applyOutboundRel(original, counted);
|
|
46
|
+
if (html !== original)
|
|
68
47
|
fs.writeFileSync(file, html);
|
|
69
48
|
files++;
|
|
70
49
|
}
|
package/dist/dates/index.d.ts
CHANGED
|
@@ -3,3 +3,5 @@ export { buildNow } from './buildClock.ts';
|
|
|
3
3
|
export { maxDate, latestDateModified, NEW_BADGE_DAYS, isNew } from './lastmod.ts';
|
|
4
4
|
export { eventToIcs, eventsToIcs, googleCalendarUrl, monthsCovered } from './ics.ts';
|
|
5
5
|
export type { IcsEvent, IcsCalendar } from './ics.ts';
|
|
6
|
+
export { resolvePeriodDates } from './seasonalDates.ts';
|
|
7
|
+
export type { Period, StayDates } from './seasonalDates.ts';
|
package/dist/dates/index.js
CHANGED
|
@@ -3,3 +3,4 @@ export { formatUtcDate, formatUtcDateRange } from "./formatDate.js";
|
|
|
3
3
|
export { buildNow } from "./buildClock.js";
|
|
4
4
|
export { maxDate, latestDateModified, NEW_BADGE_DAYS, isNew } from "./lastmod.js";
|
|
5
5
|
export { eventToIcs, eventsToIcs, googleCalendarUrl, monthsCovered } from "./ics.js";
|
|
6
|
+
export { resolvePeriodDates } from "./seasonalDates.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Travel dates for a seasonal or monthly guide page (SLA-2541, epic SLA-2530).
|
|
3
|
+
*
|
|
4
|
+
* These pages answer "when should I go?", so the hotel search they offer should already
|
|
5
|
+
* be scoped to the period the reader just settled on. That means turning a slug like
|
|
6
|
+
* `august` or `winter` into a concrete check-in/check-out pair at BUILD time.
|
|
7
|
+
*
|
|
8
|
+
* Two things make this less trivial than it looks:
|
|
9
|
+
*
|
|
10
|
+
* - **The year is not the current year.** A page for August, built in September, must
|
|
11
|
+
* resolve to NEXT August. Booking rejects a past check-in and lands the reader on an
|
|
12
|
+
* error instead of a search, so a naive `new Date().getFullYear()` silently breaks
|
|
13
|
+
* every month that has already passed.
|
|
14
|
+
* - **`today` is a parameter, not a global.** The year-rollover is the whole point of
|
|
15
|
+
* this module and it is only testable if the caller can pin the clock. Reading the
|
|
16
|
+
* clock inside would make the one behaviour worth testing untestable.
|
|
17
|
+
*
|
|
18
|
+
* Everything is UTC. A local-time stamp near midnight resolves to the neighbouring day and
|
|
19
|
+
* has already broken a date gate in this factory once.
|
|
20
|
+
*/
|
|
21
|
+
export type Period = {
|
|
22
|
+
kind: 'month';
|
|
23
|
+
slug: string;
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'season';
|
|
26
|
+
slug: string;
|
|
27
|
+
};
|
|
28
|
+
export interface StayDates {
|
|
29
|
+
/** Check-in, `YYYY-MM-DD` (UTC). */
|
|
30
|
+
checkin: string;
|
|
31
|
+
/** Check-out, `YYYY-MM-DD` (UTC). */
|
|
32
|
+
checkout: string;
|
|
33
|
+
/** Human-readable span for CTA copy, e.g. `14–17 August 2027`. */
|
|
34
|
+
label: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resolve a period slug to the next bookable stay covering it.
|
|
38
|
+
*
|
|
39
|
+
* Throws on an unrecognised slug rather than falling back to a default month: a silent
|
|
40
|
+
* fallback would ship a page about December offering dates in January, and nothing
|
|
41
|
+
* downstream could tell that from a correct link.
|
|
42
|
+
*/
|
|
43
|
+
export declare function resolvePeriodDates(period: Period, today: Date): StayDates;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Travel dates for a seasonal or monthly guide page (SLA-2541, epic SLA-2530).
|
|
3
|
+
*
|
|
4
|
+
* These pages answer "when should I go?", so the hotel search they offer should already
|
|
5
|
+
* be scoped to the period the reader just settled on. That means turning a slug like
|
|
6
|
+
* `august` or `winter` into a concrete check-in/check-out pair at BUILD time.
|
|
7
|
+
*
|
|
8
|
+
* Two things make this less trivial than it looks:
|
|
9
|
+
*
|
|
10
|
+
* - **The year is not the current year.** A page for August, built in September, must
|
|
11
|
+
* resolve to NEXT August. Booking rejects a past check-in and lands the reader on an
|
|
12
|
+
* error instead of a search, so a naive `new Date().getFullYear()` silently breaks
|
|
13
|
+
* every month that has already passed.
|
|
14
|
+
* - **`today` is a parameter, not a global.** The year-rollover is the whole point of
|
|
15
|
+
* this module and it is only testable if the caller can pin the clock. Reading the
|
|
16
|
+
* clock inside would make the one behaviour worth testing untestable.
|
|
17
|
+
*
|
|
18
|
+
* Everything is UTC. A local-time stamp near midnight resolves to the neighbouring day and
|
|
19
|
+
* has already broken a date gate in this factory once.
|
|
20
|
+
*/
|
|
21
|
+
const MS_PER_DAY = 86_400_000;
|
|
22
|
+
/** Month slugs in calendar order — the index IS the month number. */
|
|
23
|
+
const MONTH_ORDER = [
|
|
24
|
+
'january', 'february', 'march', 'april', 'may', 'june',
|
|
25
|
+
'july', 'august', 'september', 'october', 'november', 'december',
|
|
26
|
+
];
|
|
27
|
+
const MONTH_LABELS = [
|
|
28
|
+
'January', 'February', 'March', 'April', 'May', 'June',
|
|
29
|
+
'July', 'August', 'September', 'October', 'November', 'December',
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* One representative month per season.
|
|
33
|
+
*
|
|
34
|
+
* Mid-season rather than first month: a "spring" reader is not told the trip must start in
|
|
35
|
+
* March, and the mid-point is the least misleading single date a four-month span can carry.
|
|
36
|
+
*/
|
|
37
|
+
const SEASON_MONTH = {
|
|
38
|
+
spring: 'april',
|
|
39
|
+
summer: 'july',
|
|
40
|
+
autumn: 'october',
|
|
41
|
+
winter: 'january',
|
|
42
|
+
};
|
|
43
|
+
/** Day of the month to check in on — mid-month, away from both edges. */
|
|
44
|
+
const CHECKIN_DAY = 14;
|
|
45
|
+
/** Length of the sample stay. */
|
|
46
|
+
const STAY_NIGHTS = 3;
|
|
47
|
+
/**
|
|
48
|
+
* Minimum lead time between the build date and the check-in it emits.
|
|
49
|
+
*
|
|
50
|
+
* Without it, a page for the current month built on the 13th would offer a stay starting
|
|
51
|
+
* tomorrow. It also absorbs build staleness: a link stays bookable for two weeks after the
|
|
52
|
+
* build that produced it, which is far longer than this factory goes between deploys.
|
|
53
|
+
*/
|
|
54
|
+
const LEAD_DAYS = 14;
|
|
55
|
+
const toIso = (ms) => new Date(ms).toISOString().slice(0, 10);
|
|
56
|
+
/**
|
|
57
|
+
* Reader-facing span, e.g. `14–17 August 2027` or `30 September – 3 October 2027`.
|
|
58
|
+
*
|
|
59
|
+
* Derived from the resolved dates rather than from CHECKIN_DAY, because the check-in slides
|
|
60
|
+
* when the preferred day is inside the lead window — a label built from the constant would
|
|
61
|
+
* then name a date the link does not actually request.
|
|
62
|
+
*/
|
|
63
|
+
function formatStay(checkinMs, checkoutMs) {
|
|
64
|
+
const a = new Date(checkinMs);
|
|
65
|
+
const b = new Date(checkoutMs);
|
|
66
|
+
if (a.getUTCMonth() === b.getUTCMonth()) {
|
|
67
|
+
return `${a.getUTCDate()}–${b.getUTCDate()} ${MONTH_LABELS[a.getUTCMonth()]} ${b.getUTCFullYear()}`;
|
|
68
|
+
}
|
|
69
|
+
// A stay can cross both a month and a YEAR boundary (check in 30 December, out 2 January).
|
|
70
|
+
// Printing one trailing year there would date the first month to the wrong year.
|
|
71
|
+
if (a.getUTCFullYear() !== b.getUTCFullYear()) {
|
|
72
|
+
return `${a.getUTCDate()} ${MONTH_LABELS[a.getUTCMonth()]} ${a.getUTCFullYear()} – ${b.getUTCDate()} ${MONTH_LABELS[b.getUTCMonth()]} ${b.getUTCFullYear()}`;
|
|
73
|
+
}
|
|
74
|
+
return `${a.getUTCDate()} ${MONTH_LABELS[a.getUTCMonth()]} – ${b.getUTCDate()} ${MONTH_LABELS[b.getUTCMonth()]} ${b.getUTCFullYear()}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Resolve a period slug to the next bookable stay covering it.
|
|
78
|
+
*
|
|
79
|
+
* Throws on an unrecognised slug rather than falling back to a default month: a silent
|
|
80
|
+
* fallback would ship a page about December offering dates in January, and nothing
|
|
81
|
+
* downstream could tell that from a correct link.
|
|
82
|
+
*/
|
|
83
|
+
export function resolvePeriodDates(period, today) {
|
|
84
|
+
const monthSlug = period.kind === 'season' ? SEASON_MONTH[period.slug] : period.slug;
|
|
85
|
+
if (!monthSlug) {
|
|
86
|
+
throw new Error(`seasonalDates: unknown season slug "${period.slug}"`);
|
|
87
|
+
}
|
|
88
|
+
const monthIndex = MONTH_ORDER.indexOf(monthSlug);
|
|
89
|
+
if (monthIndex === -1) {
|
|
90
|
+
throw new Error(`seasonalDates: unknown month slug "${monthSlug}"`);
|
|
91
|
+
}
|
|
92
|
+
const todayUtc = Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
|
|
93
|
+
const earliest = todayUtc + LEAD_DAYS * MS_PER_DAY;
|
|
94
|
+
// Slide within the month before giving up on it. Rolling straight to next year the moment
|
|
95
|
+
// the 14th is too close would make a page advertise dates twelve months out during the very
|
|
96
|
+
// period it is about: /plan/months/september, built on 5 September, would offer September
|
|
97
|
+
// NEXT year while 19–30 September is still perfectly bookable — and that is the page's
|
|
98
|
+
// peak-intent traffic, so it is the worst moment to be a year off.
|
|
99
|
+
const year = today.getUTCFullYear();
|
|
100
|
+
const lastDayOfMonth = new Date(Date.UTC(year, monthIndex + 1, 0)).getUTCDate();
|
|
101
|
+
const preferred = Date.UTC(year, monthIndex, CHECKIN_DAY);
|
|
102
|
+
let checkinMs = Math.max(preferred, earliest);
|
|
103
|
+
// Only when no day left in this year's month clears the lead time does the year roll. The
|
|
104
|
+
// check-out may spill into the next month; a stay starting on the 30th is still a stay in
|
|
105
|
+
// that month, and refusing it would send the reader a year away for no reader-visible gain.
|
|
106
|
+
if (checkinMs > Date.UTC(year, monthIndex, lastDayOfMonth)) {
|
|
107
|
+
checkinMs = Date.UTC(year + 1, monthIndex, CHECKIN_DAY);
|
|
108
|
+
}
|
|
109
|
+
const checkoutMs = checkinMs + STAY_NIGHTS * MS_PER_DAY;
|
|
110
|
+
return { checkin: toIso(checkinMs), checkout: toIso(checkoutMs), label: formatStay(checkinMs, checkoutMs) };
|
|
111
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slot the monument tour band goes after (0 = lede, N = section N): the first slot
|
|
3
|
+
* from `preferred` on with no authored interlude, else the nearest free one before
|
|
4
|
+
* it, else the last. Shared by MonumentBody and the page's ToC so both agree.
|
|
5
|
+
*/
|
|
6
|
+
export declare function pickBandSlot(sectionCount: number, interludes?: {
|
|
7
|
+
afterSection: number;
|
|
8
|
+
}[], preferred?: number): number;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slot the monument tour band goes after (0 = lede, N = section N): the first slot
|
|
3
|
+
* from `preferred` on with no authored interlude, else the nearest free one before
|
|
4
|
+
* it, else the last. Shared by MonumentBody and the page's ToC so both agree.
|
|
5
|
+
*/
|
|
6
|
+
export function pickBandSlot(sectionCount, interludes = [], preferred = 2) {
|
|
7
|
+
const last = sectionCount;
|
|
8
|
+
const clamp = (n) => Math.min(Math.max(n, 0), last);
|
|
9
|
+
const taken = new Set(interludes.map((il) => clamp(il.afterSection)));
|
|
10
|
+
const start = clamp(preferred);
|
|
11
|
+
for (let i = start; i <= last; i++)
|
|
12
|
+
if (!taken.has(i))
|
|
13
|
+
return i;
|
|
14
|
+
for (let i = start - 1; i >= 1; i--)
|
|
15
|
+
if (!taken.has(i))
|
|
16
|
+
return i;
|
|
17
|
+
return last;
|
|
18
|
+
}
|
package/dist/interludes/index.js
CHANGED
package/dist/schema/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export type { JsonLdNode, SchemaEntry, OpeningHoursSpecification, IdRef, SchemaA
|
|
|
2
2
|
export { createSchemaContext, normalizeHomepageUrl, normalizeStructuredDataUrls, formatSchemaDate, createPersonSchema, withAuthorPersonSchema, } from './context.ts';
|
|
3
3
|
export { parsePriceFromString, isFreeEntry, cleanStreetAddress, normalizePriceRange, normalizeHotelPriceRange, postalCodeOf, dataAddressPolicy, postalAddress, geoCoordinates, aggregateRating, } from './values.ts';
|
|
4
4
|
export type { AddressPolicy, PostalAddressParts } from './values.ts';
|
|
5
|
+
export { parsePriceRange, calculateAggregatePriceRange } from './price.ts';
|
|
5
6
|
export { DAY_KEYS, HOURS_ALWAYS_OPEN, parseHoursShifts, closedFieldDays, resolveOpeningHours, assertedOpenDays, generateOpeningHours, parseHoursString, hoursStringOpenDays, } from './hours.ts';
|
|
6
7
|
export type { DayKey, OpeningHoursInput, OpeningHoursBucket } from './hours.ts';
|
|
7
8
|
export { renderSchemaPolicy } from './policy.ts';
|
package/dist/schema/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { createSchemaContext, normalizeHomepageUrl, normalizeStructuredDataUrls, formatSchemaDate, createPersonSchema, withAuthorPersonSchema, } from "./context.js";
|
|
2
2
|
export { parsePriceFromString, isFreeEntry, cleanStreetAddress, normalizePriceRange, normalizeHotelPriceRange, postalCodeOf, dataAddressPolicy, postalAddress, geoCoordinates, aggregateRating, } from "./values.js";
|
|
3
|
+
export { parsePriceRange, calculateAggregatePriceRange } from "./price.js";
|
|
3
4
|
export { DAY_KEYS, HOURS_ALWAYS_OPEN, parseHoursShifts, closedFieldDays, resolveOpeningHours, assertedOpenDays, generateOpeningHours, parseHoursString, hoursStringOpenDays, } from "./hours.js";
|
|
4
5
|
export { renderSchemaPolicy } from "./policy.js";
|
|
5
6
|
export { offer, placeNode, siteOrganizationNode, siteWebsiteNode } from "./nodes.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse restaurant averagePrice string to extract min and max prices
|
|
3
|
+
* Examples:
|
|
4
|
+
* - "8-15 euros" → { min: 8, max: 15 }
|
|
5
|
+
* - "20-35 euros" → { min: 20, max: 35 }
|
|
6
|
+
* - "From 25€" → { min: 25, max: 25 }
|
|
7
|
+
* - "15€" → { min: 15, max: 15 }
|
|
8
|
+
*/
|
|
9
|
+
export declare function parsePriceRange(priceStr: string): {
|
|
10
|
+
min: number;
|
|
11
|
+
max: number;
|
|
12
|
+
} | null;
|
|
13
|
+
/**
|
|
14
|
+
* Calculate aggregate price range from multiple price strings
|
|
15
|
+
*/
|
|
16
|
+
export declare function calculateAggregatePriceRange(priceStrings: string[]): {
|
|
17
|
+
lowPrice: string;
|
|
18
|
+
highPrice: string;
|
|
19
|
+
} | null;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse restaurant averagePrice string to extract min and max prices
|
|
3
|
+
* Examples:
|
|
4
|
+
* - "8-15 euros" → { min: 8, max: 15 }
|
|
5
|
+
* - "20-35 euros" → { min: 20, max: 35 }
|
|
6
|
+
* - "From 25€" → { min: 25, max: 25 }
|
|
7
|
+
* - "15€" → { min: 15, max: 15 }
|
|
8
|
+
*/
|
|
9
|
+
export function parsePriceRange(priceStr) {
|
|
10
|
+
if (!priceStr)
|
|
11
|
+
return null;
|
|
12
|
+
// Remove common words and normalize
|
|
13
|
+
const normalized = priceStr
|
|
14
|
+
.toLowerCase()
|
|
15
|
+
.replace(/euros?/g, '')
|
|
16
|
+
.replace(/€/g, '')
|
|
17
|
+
.replace(/from\s*/gi, '')
|
|
18
|
+
.trim();
|
|
19
|
+
// Match patterns like "8-15", "20-35"
|
|
20
|
+
const rangeMatch = normalized.match(/(\d+)\s*-\s*(\d+)/);
|
|
21
|
+
if (rangeMatch) {
|
|
22
|
+
return {
|
|
23
|
+
min: parseFloat(rangeMatch[1]),
|
|
24
|
+
max: parseFloat(rangeMatch[2]),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
// Match single price like "15", "25"
|
|
28
|
+
const singleMatch = normalized.match(/(\d+)/);
|
|
29
|
+
if (singleMatch) {
|
|
30
|
+
const price = parseFloat(singleMatch[1]);
|
|
31
|
+
return { min: price, max: price };
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Calculate aggregate price range from multiple price strings
|
|
37
|
+
*/
|
|
38
|
+
export function calculateAggregatePriceRange(priceStrings) {
|
|
39
|
+
const parsedPrices = priceStrings
|
|
40
|
+
.map(parsePriceRange)
|
|
41
|
+
.filter((p) => p !== null);
|
|
42
|
+
if (parsedPrices.length === 0)
|
|
43
|
+
return null;
|
|
44
|
+
const allMins = parsedPrices.map(p => p.min);
|
|
45
|
+
const allMaxs = parsedPrices.map(p => p.max);
|
|
46
|
+
return {
|
|
47
|
+
lowPrice: Math.min(...allMins).toString(),
|
|
48
|
+
highPrice: Math.max(...allMaxs).toString(),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatFromPrice(priceText: string | undefined): string | null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// "18,50 euros adulte" → "From 18,50€"; anything without a euro amount keeps its first clause.
|
|
2
|
+
export function formatFromPrice(priceText) {
|
|
3
|
+
if (!priceText)
|
|
4
|
+
return null;
|
|
5
|
+
const match = priceText.match(/(\d+(?:[,.]\d+)?)\s*(?:euros?|€)/i);
|
|
6
|
+
return match ? `From ${match[1]}€` : priceText.split(',')[0];
|
|
7
|
+
}
|
package/dist/text/index.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export { slugify } from './slugify.ts';
|
|
|
2
2
|
export { fitWithinLength } from './fitWithinLength.ts';
|
|
3
3
|
export { fitLength } from './fitLength.ts';
|
|
4
4
|
export { readingTime } from './readingTime.ts';
|
|
5
|
+
export { parseMarkdown, parseMarkdownInline, parseMarkdownBlock, stripMarkdown } from './markdown.ts';
|
|
6
|
+
export { formatFromPrice } from './cardCopy.ts';
|
package/dist/text/index.js
CHANGED
|
@@ -3,3 +3,5 @@ export { slugify } from "./slugify.js";
|
|
|
3
3
|
export { fitWithinLength } from "./fitWithinLength.js";
|
|
4
4
|
export { fitLength } from "./fitLength.js";
|
|
5
5
|
export { readingTime } from "./readingTime.js";
|
|
6
|
+
export { parseMarkdown, parseMarkdownInline, parseMarkdownBlock, stripMarkdown } from "./markdown.js";
|
|
7
|
+
export { formatFromPrice } from "./cardCopy.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses Markdown content to HTML with base path support
|
|
3
|
+
* Automatically prepends the base path to relative internal links
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseMarkdown(markdown: string, basePath?: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Parses Markdown content to HTML with inline mode (no paragraph wrapping)
|
|
8
|
+
* Useful for short descriptions that shouldn't be wrapped in <p> tags
|
|
9
|
+
* Also prepends the base path to relative internal links
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseMarkdownInline(markdown: string, basePath?: string, sourcesLength?: number): string;
|
|
12
|
+
/**
|
|
13
|
+
* Parses block-level Markdown (lists, blockquotes, tables) to HTML.
|
|
14
|
+
* Unlike parseMarkdown, it ALSO converts footnote refs ([^n]) to <sup> anchors —
|
|
15
|
+
* matching parseMarkdownInline — so footnotes inside list items render correctly.
|
|
16
|
+
* Use for article-body chunks that are block constructs; parseMarkdownInline
|
|
17
|
+
* leaves "- " / ">" / "|" as literal text.
|
|
18
|
+
*/
|
|
19
|
+
export declare function parseMarkdownBlock(markdown: string, basePath?: string, sourcesLength?: number): string;
|
|
20
|
+
/**
|
|
21
|
+
* Strips Markdown syntax down to plain text.
|
|
22
|
+
*
|
|
23
|
+
* For contexts that must NOT receive HTML — chiefly Schema.org JSON-LD string
|
|
24
|
+
* values such as FAQPage acceptedAnswer.text, where authors write markdown in
|
|
25
|
+
* `answer` and the raw `**asterisks**` were being emitted into structured data
|
|
26
|
+
* that search engines and answer engines read verbatim.
|
|
27
|
+
*/
|
|
28
|
+
export declare function stripMarkdown(markdown: string): string;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Markdown → HTML for content prose, promoted from the sites' src/utils/markdown.ts (SLA-3016; Granada's copy).
|
|
2
|
+
// No Rule 38 rel here: the outboundLinkRel build integration applies it once to every anchor of the built HTML.
|
|
3
|
+
import { marked } from 'marked';
|
|
4
|
+
/**
|
|
5
|
+
* Fixes relative internal links by prepending the base path
|
|
6
|
+
* Matches href="/path" (not href="//url") and prepends the base
|
|
7
|
+
*/
|
|
8
|
+
function fixInternalLinks(html, basePath = '/') {
|
|
9
|
+
const cleanBase = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
|
|
10
|
+
// Replace href="/path" with href="${base}/path"
|
|
11
|
+
// But NOT href="//external" or href="http"
|
|
12
|
+
return html.replace(/href="\/(?!\/|https?:)/g, `href="${cleanBase}/`);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Parses Markdown content to HTML with base path support
|
|
16
|
+
* Automatically prepends the base path to relative internal links
|
|
17
|
+
*/
|
|
18
|
+
export function parseMarkdown(markdown, basePath = '/') {
|
|
19
|
+
if (!markdown)
|
|
20
|
+
return '';
|
|
21
|
+
const html = marked.parse(markdown, {
|
|
22
|
+
breaks: true,
|
|
23
|
+
gfm: true,
|
|
24
|
+
});
|
|
25
|
+
return fixInternalLinks(html.trim(), basePath);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Converts `[^N]` footnote markers to citation anchors. Drops (rather than
|
|
29
|
+
* renders) any marker that can't point at a real citation — N < 1, or N
|
|
30
|
+
* greater than `sourcesLength` when the caller supplies it — instead of
|
|
31
|
+
* emitting a dangling `<a href="#fn-N">` link to a footnote that doesn't
|
|
32
|
+
* exist. This is defense-in-depth: the real guarantee that markers are
|
|
33
|
+
* well-formed is the validate-footnote-markers.mjs pre-push gate (SLA-1571),
|
|
34
|
+
* but that gate is bypassable (--no-verify, an unconfigured core.hooksPath)
|
|
35
|
+
* and has its own edge cases, so the renderer itself no longer trusts N
|
|
36
|
+
* blindly. `sourcesLength` is optional — omit it (as every non-article
|
|
37
|
+
* caller currently does) to skip the upper-bound check; the N ≥ 1 check
|
|
38
|
+
* always applies since sources[] is 1-indexed everywhere in this codebase.
|
|
39
|
+
*/
|
|
40
|
+
function convertFootnotes(html, sourcesLength) {
|
|
41
|
+
return html.replace(/\[\^(\d+)\]/g, (_match, nStr) => {
|
|
42
|
+
const n = Number(nStr);
|
|
43
|
+
if (n < 1 || (sourcesLength !== undefined && n > sourcesLength))
|
|
44
|
+
return '';
|
|
45
|
+
// No `id="fnref-${n}"`: this fires per marker OCCURRENCE, not per distinct N, so an
|
|
46
|
+
// article citing one source twice shipped duplicate DOM ids — 776 excess on cordoba
|
|
47
|
+
// alone. Nothing ever read the id (AuthoritativeSources emits the `fn-N` targets but no
|
|
48
|
+
// back-links), so it was pure invalid markup. Restore it only alongside a real consumer,
|
|
49
|
+
// and make it unique when you do.
|
|
50
|
+
return `<sup><a href="#fn-${n}" class="footnote-ref">[${n}]</a></sup>`;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Parses Markdown content to HTML with inline mode (no paragraph wrapping)
|
|
55
|
+
* Useful for short descriptions that shouldn't be wrapped in <p> tags
|
|
56
|
+
* Also prepends the base path to relative internal links
|
|
57
|
+
*/
|
|
58
|
+
export function parseMarkdownInline(markdown, basePath = '/', sourcesLength) {
|
|
59
|
+
if (!markdown)
|
|
60
|
+
return '';
|
|
61
|
+
let html = marked.parseInline(markdown);
|
|
62
|
+
html = fixInternalLinks(html.trim(), basePath);
|
|
63
|
+
html = convertFootnotes(html, sourcesLength);
|
|
64
|
+
return html;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Parses block-level Markdown (lists, blockquotes, tables) to HTML.
|
|
68
|
+
* Unlike parseMarkdown, it ALSO converts footnote refs ([^n]) to <sup> anchors —
|
|
69
|
+
* matching parseMarkdownInline — so footnotes inside list items render correctly.
|
|
70
|
+
* Use for article-body chunks that are block constructs; parseMarkdownInline
|
|
71
|
+
* leaves "- " / ">" / "|" as literal text.
|
|
72
|
+
*/
|
|
73
|
+
export function parseMarkdownBlock(markdown, basePath = '/', sourcesLength) {
|
|
74
|
+
if (!markdown)
|
|
75
|
+
return '';
|
|
76
|
+
let html = marked.parse(markdown, {
|
|
77
|
+
breaks: true,
|
|
78
|
+
gfm: true,
|
|
79
|
+
});
|
|
80
|
+
html = fixInternalLinks(html.trim(), basePath);
|
|
81
|
+
html = convertFootnotes(html, sourcesLength);
|
|
82
|
+
return html;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Strips Markdown syntax down to plain text.
|
|
86
|
+
*
|
|
87
|
+
* For contexts that must NOT receive HTML — chiefly Schema.org JSON-LD string
|
|
88
|
+
* values such as FAQPage acceptedAnswer.text, where authors write markdown in
|
|
89
|
+
* `answer` and the raw `**asterisks**` were being emitted into structured data
|
|
90
|
+
* that search engines and answer engines read verbatim.
|
|
91
|
+
*/
|
|
92
|
+
export function stripMarkdown(markdown) {
|
|
93
|
+
if (!markdown)
|
|
94
|
+
return '';
|
|
95
|
+
return markdown
|
|
96
|
+
// Raw HTML tags go FIRST, before any emphasis rule sees them. A hand-authored affiliate
|
|
97
|
+
// <a> whose query carries partner_id and cmp=site__page__place otherwise reaches the bold rule
|
|
98
|
+
// (/(\*\*|__)(.*?)\1/g), which eats the cmp's `__` separators, and the italic rule,
|
|
99
|
+
// which pairs `target="_blank"`'s underscore with `partner_id`'s — shipping
|
|
100
|
+
// `partnerid=X&cmp=siteplaceplace` into FAQPage JSON-LD. That is a real monetization
|
|
101
|
+
// leak, not just cosmetic: the mangled key is not a partner id, so a click from a rich
|
|
102
|
+
// result is unattributed. Tags are dropped and their text kept, which is what every
|
|
103
|
+
// caller of this function wants (schema plain text, alt text).
|
|
104
|
+
// Block boundaries become a SPACE, inline tags collapse to nothing. `</p><p>` removed
|
|
105
|
+
// outright glued sentences together ("…the Realejo.See all itineraries") in 64 FAQPage
|
|
106
|
+
// answers, while `</a>.` and `</strong>'s` must NOT gain a space. The pattern is also
|
|
107
|
+
// anchored to a real tag name so a prose comparison ("groups under <10 people") or a
|
|
108
|
+
// CommonMark autolink is not silently eaten as if it were markup.
|
|
109
|
+
.replace(/<\/(?:p|div|li|h[1-6]|tr|td|th|blockquote|section|article)\s*>|<br\s*\/?>/gi, ' ')
|
|
110
|
+
// The tag name must be alphanumeric AND followed by whitespace, `/` or `>`. A looser
|
|
111
|
+
// `<[a-zA-Z][^>]*>` still ate a CommonMark autolink (`<https://x>`) and a bare email
|
|
112
|
+
// angle (`<name@example.com>`), because both start with a letter.
|
|
113
|
+
.replace(/<\/?[a-zA-Z][a-zA-Z0-9]*(?:\s[^>]*)?\/?>/g, '')
|
|
114
|
+
.replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1') // images -> alt text
|
|
115
|
+
.replace(/\[\^\d+\]/g, '') // footnote markers
|
|
116
|
+
.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1') // links -> anchor text
|
|
117
|
+
.replace(/(\*\*|__)(.*?)\1/g, '$2') // bold
|
|
118
|
+
.replace(/(^|[^*_])[*_]([^*_]+)[*_]/g, '$1$2') // italic
|
|
119
|
+
.replace(/`([^`]+)`/g, '$1') // inline code
|
|
120
|
+
.replace(/\s{2,}/g, ' ')
|
|
121
|
+
.trim();
|
|
122
|
+
}
|
package/dist/url/index.d.ts
CHANGED
|
@@ -5,5 +5,5 @@ export { buildImageMap, keyBySlug } from './imageMap.ts';
|
|
|
5
5
|
export { articleFeedItem } from './feed.ts';
|
|
6
6
|
export type { FeedArticle, FeedItem } from './feed.ts';
|
|
7
7
|
export { pageTypeOf } from './pageType.ts';
|
|
8
|
-
export { outboundClassifier, outboundHostKey } from './outbound.ts';
|
|
8
|
+
export { outboundClassifier, outboundHostKey, applyOutboundRel } from './outbound.ts';
|
|
9
9
|
export type { OutboundDomainStore } from './outbound.ts';
|
package/dist/url/index.js
CHANGED
|
@@ -5,4 +5,4 @@ export { normalizeHref, extractProseHrefs, dedupRelated } from "./relatedDedup.j
|
|
|
5
5
|
export { buildImageMap, keyBySlug } from "./imageMap.js";
|
|
6
6
|
export { articleFeedItem } from "./feed.js";
|
|
7
7
|
export { pageTypeOf } from "./pageType.js";
|
|
8
|
-
export { outboundClassifier, outboundHostKey } from "./outbound.js";
|
|
8
|
+
export { outboundClassifier, outboundHostKey, applyOutboundRel } from "./outbound.js";
|
package/dist/url/outbound.d.ts
CHANGED
|
@@ -11,3 +11,11 @@ export declare function outboundHostKey(host: string): string;
|
|
|
11
11
|
* `other` for an unlisted host or a URL that does not parse. Lookup only: each consumer decides what a class renders.
|
|
12
12
|
*/
|
|
13
13
|
export declare function outboundClassifier(store: OutboundDomainStore | Record<string, string>): (url: string) => string;
|
|
14
|
+
/**
|
|
15
|
+
* The one Rule 38 HTML rewriter: every `<a>` whose absolute http(s) href `classify` maps to `competitor` gains
|
|
16
|
+
* `nofollow`, to `affiliate` `sponsored`. `href` and `rel` are read only as whole attributes (never `data-href`
|
|
17
|
+
* or `data-rel`), and the policy check reads the rel tokens only. An anchor whose rel already holds the policy token
|
|
18
|
+
* is left as it is; a missing or empty rel becomes `rel="<policy> noopener"`; any other rel keeps its tokens and
|
|
19
|
+
* gains `noopener` and the policy token.
|
|
20
|
+
*/
|
|
21
|
+
export declare function applyOutboundRel(html: string, classify: (url: string) => string): string;
|
package/dist/url/outbound.js
CHANGED
|
@@ -20,3 +20,42 @@ export function outboundClassifier(store) {
|
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
+
const ATTR_VALUE = (name) => new RegExp(`(\\s)${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`, 'i');
|
|
24
|
+
const HREF = ATTR_VALUE('href');
|
|
25
|
+
const REL = ATTR_VALUE('rel');
|
|
26
|
+
/**
|
|
27
|
+
* The one Rule 38 HTML rewriter: every `<a>` whose absolute http(s) href `classify` maps to `competitor` gains
|
|
28
|
+
* `nofollow`, to `affiliate` `sponsored`. `href` and `rel` are read only as whole attributes (never `data-href`
|
|
29
|
+
* or `data-rel`), and the policy check reads the rel tokens only. An anchor whose rel already holds the policy token
|
|
30
|
+
* is left as it is; a missing or empty rel becomes `rel="<policy> noopener"`; any other rel keeps its tokens and
|
|
31
|
+
* gains `noopener` and the policy token.
|
|
32
|
+
*/
|
|
33
|
+
export function applyOutboundRel(html, classify) {
|
|
34
|
+
return html.replace(/<a\b([^>]*)>/gi, (tag, attrs) => {
|
|
35
|
+
const h = HREF.exec(attrs);
|
|
36
|
+
const href = h ? (h[2] ?? h[3]) : undefined;
|
|
37
|
+
if (!href)
|
|
38
|
+
return tag;
|
|
39
|
+
let protocol;
|
|
40
|
+
try {
|
|
41
|
+
protocol = new URL(href).protocol;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return tag;
|
|
45
|
+
}
|
|
46
|
+
if (protocol !== 'http:' && protocol !== 'https:')
|
|
47
|
+
return tag;
|
|
48
|
+
const cls = classify(href);
|
|
49
|
+
const policy = cls === 'competitor' ? 'nofollow' : cls === 'affiliate' ? 'sponsored' : null;
|
|
50
|
+
if (!policy)
|
|
51
|
+
return tag;
|
|
52
|
+
const r = REL.exec(attrs);
|
|
53
|
+
if (!r)
|
|
54
|
+
return `<a${attrs} rel="${policy} noopener">`;
|
|
55
|
+
const tokens = (r[2] ?? r[3]).split(/\s+/).filter(Boolean);
|
|
56
|
+
if (tokens.some((t) => t.toLowerCase() === policy))
|
|
57
|
+
return tag;
|
|
58
|
+
const value = tokens.length ? [...new Set([...tokens, 'noopener', policy])].join(' ') : `${policy} noopener`;
|
|
59
|
+
return `<a${attrs.slice(0, r.index)}${r[1]}rel="${value}"${attrs.slice(r.index + r[0].length)}>`;
|
|
60
|
+
});
|
|
61
|
+
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import type { AstroIntegration } from 'astro';
|
|
5
|
-
import { outboundClassifier, type OutboundDomainStore } from '../url/outbound.ts';
|
|
5
|
+
import { applyOutboundRel, outboundClassifier, type OutboundDomainStore } from '../url/outbound.ts';
|
|
6
6
|
|
|
7
7
|
export interface OutboundLinkRelOptions {
|
|
8
8
|
/** Site root; the classification store is read from `src/data/outbound-domains.json` under it. */
|
|
@@ -24,20 +24,10 @@ function walk(dir: string, out: string[] = []): string[] {
|
|
|
24
24
|
return out;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
function mergeRel(attrs: string, tokens: string[]): string {
|
|
28
|
-
if (/\brel\s*=\s*["'][^"']*["']/i.test(attrs)) {
|
|
29
|
-
return attrs.replace(/\brel\s*=\s*["']([^"']*)["']/i, (_m, existing: string) => {
|
|
30
|
-
const set = new Set([...existing.split(/\s+/).filter(Boolean), ...tokens]);
|
|
31
|
-
return `rel="${[...set].join(' ')}"`;
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
return `${attrs} rel="${tokens.join(' ')}"`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
27
|
/**
|
|
38
28
|
* Astro integration enforcing the outbound rel policy on every anchor of the built HTML, whichever component
|
|
39
|
-
* rendered it:
|
|
40
|
-
* everything else is untouched.
|
|
29
|
+
* rendered it (markdown prose included: the core renderers add no rel): competitor-classified hosts gain `nofollow`,
|
|
30
|
+
* affiliate-classified hosts gain `sponsored`, and everything else is untouched. Rules: `applyOutboundRel`.
|
|
41
31
|
*/
|
|
42
32
|
export function outboundLinkRel({ root }: OutboundLinkRelOptions): AstroIntegration {
|
|
43
33
|
const siteRoot = root instanceof URL ? fileURLToPath(root) : root;
|
|
@@ -51,23 +41,15 @@ export function outboundLinkRel({ root }: OutboundLinkRelOptions): AstroIntegrat
|
|
|
51
41
|
const outDir = fileURLToPath(dir);
|
|
52
42
|
let files = 0;
|
|
53
43
|
let anchors = 0;
|
|
44
|
+
const counted = (url: string) => {
|
|
45
|
+
const cls = classify(url);
|
|
46
|
+
if (cls === 'competitor' || cls === 'affiliate') anchors++;
|
|
47
|
+
return cls;
|
|
48
|
+
};
|
|
54
49
|
for (const file of walk(outDir)) {
|
|
55
50
|
const original = fs.readFileSync(file, 'utf-8');
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const href = attrs.match(/href\s*=\s*"([^"]*)"/i)?.[1] ?? attrs.match(/href\s*=\s*'([^']*)'/i)?.[1];
|
|
59
|
-
if (!href || !/^https?:\/\//i.test(href)) return tag;
|
|
60
|
-
const cls = classify(href);
|
|
61
|
-
if (cls !== 'competitor' && cls !== 'affiliate') return tag;
|
|
62
|
-
anchors++;
|
|
63
|
-
const tokens: string[] = [];
|
|
64
|
-
if (cls === 'competitor' && !/\bnofollow\b/i.test(attrs)) tokens.push('nofollow');
|
|
65
|
-
if (cls === 'affiliate' && !/\bsponsored\b/i.test(attrs)) tokens.push('sponsored');
|
|
66
|
-
if (!tokens.length) return tag;
|
|
67
|
-
changed = true;
|
|
68
|
-
return `<a${mergeRel(attrs, ['noopener', ...tokens])}>`;
|
|
69
|
-
});
|
|
70
|
-
if (changed) fs.writeFileSync(file, html);
|
|
51
|
+
const html = applyOutboundRel(original, counted);
|
|
52
|
+
if (html !== original) fs.writeFileSync(file, html);
|
|
71
53
|
files++;
|
|
72
54
|
}
|
|
73
55
|
logger.info(`outbound-link-rel: scanned ${files} html file(s), enforced policy on ${anchors} affiliate/competitor anchor(s)`);
|
package/src/dates/index.ts
CHANGED
|
@@ -4,3 +4,5 @@ export { buildNow } from './buildClock.ts';
|
|
|
4
4
|
export { maxDate, latestDateModified, NEW_BADGE_DAYS, isNew } from './lastmod.ts';
|
|
5
5
|
export { eventToIcs, eventsToIcs, googleCalendarUrl, monthsCovered } from './ics.ts';
|
|
6
6
|
export type { IcsEvent, IcsCalendar } from './ics.ts';
|
|
7
|
+
export { resolvePeriodDates } from './seasonalDates.ts';
|
|
8
|
+
export type { Period, StayDates } from './seasonalDates.ts';
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Travel dates for a seasonal or monthly guide page (SLA-2541, epic SLA-2530).
|
|
3
|
+
*
|
|
4
|
+
* These pages answer "when should I go?", so the hotel search they offer should already
|
|
5
|
+
* be scoped to the period the reader just settled on. That means turning a slug like
|
|
6
|
+
* `august` or `winter` into a concrete check-in/check-out pair at BUILD time.
|
|
7
|
+
*
|
|
8
|
+
* Two things make this less trivial than it looks:
|
|
9
|
+
*
|
|
10
|
+
* - **The year is not the current year.** A page for August, built in September, must
|
|
11
|
+
* resolve to NEXT August. Booking rejects a past check-in and lands the reader on an
|
|
12
|
+
* error instead of a search, so a naive `new Date().getFullYear()` silently breaks
|
|
13
|
+
* every month that has already passed.
|
|
14
|
+
* - **`today` is a parameter, not a global.** The year-rollover is the whole point of
|
|
15
|
+
* this module and it is only testable if the caller can pin the clock. Reading the
|
|
16
|
+
* clock inside would make the one behaviour worth testing untestable.
|
|
17
|
+
*
|
|
18
|
+
* Everything is UTC. A local-time stamp near midnight resolves to the neighbouring day and
|
|
19
|
+
* has already broken a date gate in this factory once.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const MS_PER_DAY = 86_400_000;
|
|
23
|
+
|
|
24
|
+
/** Month slugs in calendar order — the index IS the month number. */
|
|
25
|
+
const MONTH_ORDER = [
|
|
26
|
+
'january', 'february', 'march', 'april', 'may', 'june',
|
|
27
|
+
'july', 'august', 'september', 'october', 'november', 'december',
|
|
28
|
+
] as const;
|
|
29
|
+
|
|
30
|
+
const MONTH_LABELS = [
|
|
31
|
+
'January', 'February', 'March', 'April', 'May', 'June',
|
|
32
|
+
'July', 'August', 'September', 'October', 'November', 'December',
|
|
33
|
+
] as const;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* One representative month per season.
|
|
37
|
+
*
|
|
38
|
+
* Mid-season rather than first month: a "spring" reader is not told the trip must start in
|
|
39
|
+
* March, and the mid-point is the least misleading single date a four-month span can carry.
|
|
40
|
+
*/
|
|
41
|
+
const SEASON_MONTH: Record<string, (typeof MONTH_ORDER)[number]> = {
|
|
42
|
+
spring: 'april',
|
|
43
|
+
summer: 'july',
|
|
44
|
+
autumn: 'october',
|
|
45
|
+
winter: 'january',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Day of the month to check in on — mid-month, away from both edges. */
|
|
49
|
+
const CHECKIN_DAY = 14;
|
|
50
|
+
|
|
51
|
+
/** Length of the sample stay. */
|
|
52
|
+
const STAY_NIGHTS = 3;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Minimum lead time between the build date and the check-in it emits.
|
|
56
|
+
*
|
|
57
|
+
* Without it, a page for the current month built on the 13th would offer a stay starting
|
|
58
|
+
* tomorrow. It also absorbs build staleness: a link stays bookable for two weeks after the
|
|
59
|
+
* build that produced it, which is far longer than this factory goes between deploys.
|
|
60
|
+
*/
|
|
61
|
+
const LEAD_DAYS = 14;
|
|
62
|
+
|
|
63
|
+
export type Period =
|
|
64
|
+
| { kind: 'month'; slug: string }
|
|
65
|
+
| { kind: 'season'; slug: string };
|
|
66
|
+
|
|
67
|
+
export interface StayDates {
|
|
68
|
+
/** Check-in, `YYYY-MM-DD` (UTC). */
|
|
69
|
+
checkin: string;
|
|
70
|
+
/** Check-out, `YYYY-MM-DD` (UTC). */
|
|
71
|
+
checkout: string;
|
|
72
|
+
/** Human-readable span for CTA copy, e.g. `14–17 August 2027`. */
|
|
73
|
+
label: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const toIso = (ms: number) => new Date(ms).toISOString().slice(0, 10);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Reader-facing span, e.g. `14–17 August 2027` or `30 September – 3 October 2027`.
|
|
80
|
+
*
|
|
81
|
+
* Derived from the resolved dates rather than from CHECKIN_DAY, because the check-in slides
|
|
82
|
+
* when the preferred day is inside the lead window — a label built from the constant would
|
|
83
|
+
* then name a date the link does not actually request.
|
|
84
|
+
*/
|
|
85
|
+
function formatStay(checkinMs: number, checkoutMs: number): string {
|
|
86
|
+
const a = new Date(checkinMs);
|
|
87
|
+
const b = new Date(checkoutMs);
|
|
88
|
+
if (a.getUTCMonth() === b.getUTCMonth()) {
|
|
89
|
+
return `${a.getUTCDate()}–${b.getUTCDate()} ${MONTH_LABELS[a.getUTCMonth()]} ${b.getUTCFullYear()}`;
|
|
90
|
+
}
|
|
91
|
+
// A stay can cross both a month and a YEAR boundary (check in 30 December, out 2 January).
|
|
92
|
+
// Printing one trailing year there would date the first month to the wrong year.
|
|
93
|
+
if (a.getUTCFullYear() !== b.getUTCFullYear()) {
|
|
94
|
+
return `${a.getUTCDate()} ${MONTH_LABELS[a.getUTCMonth()]} ${a.getUTCFullYear()} – ${b.getUTCDate()} ${MONTH_LABELS[b.getUTCMonth()]} ${b.getUTCFullYear()}`;
|
|
95
|
+
}
|
|
96
|
+
return `${a.getUTCDate()} ${MONTH_LABELS[a.getUTCMonth()]} – ${b.getUTCDate()} ${MONTH_LABELS[b.getUTCMonth()]} ${b.getUTCFullYear()}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Resolve a period slug to the next bookable stay covering it.
|
|
102
|
+
*
|
|
103
|
+
* Throws on an unrecognised slug rather than falling back to a default month: a silent
|
|
104
|
+
* fallback would ship a page about December offering dates in January, and nothing
|
|
105
|
+
* downstream could tell that from a correct link.
|
|
106
|
+
*/
|
|
107
|
+
export function resolvePeriodDates(period: Period, today: Date): StayDates {
|
|
108
|
+
const monthSlug =
|
|
109
|
+
period.kind === 'season' ? SEASON_MONTH[period.slug] : period.slug;
|
|
110
|
+
|
|
111
|
+
if (!monthSlug) {
|
|
112
|
+
throw new Error(`seasonalDates: unknown season slug "${period.slug}"`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const monthIndex = MONTH_ORDER.indexOf(monthSlug as (typeof MONTH_ORDER)[number]);
|
|
116
|
+
if (monthIndex === -1) {
|
|
117
|
+
throw new Error(`seasonalDates: unknown month slug "${monthSlug}"`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const todayUtc = Date.UTC(
|
|
121
|
+
today.getUTCFullYear(),
|
|
122
|
+
today.getUTCMonth(),
|
|
123
|
+
today.getUTCDate(),
|
|
124
|
+
);
|
|
125
|
+
const earliest = todayUtc + LEAD_DAYS * MS_PER_DAY;
|
|
126
|
+
|
|
127
|
+
// Slide within the month before giving up on it. Rolling straight to next year the moment
|
|
128
|
+
// the 14th is too close would make a page advertise dates twelve months out during the very
|
|
129
|
+
// period it is about: /plan/months/september, built on 5 September, would offer September
|
|
130
|
+
// NEXT year while 19–30 September is still perfectly bookable — and that is the page's
|
|
131
|
+
// peak-intent traffic, so it is the worst moment to be a year off.
|
|
132
|
+
const year = today.getUTCFullYear();
|
|
133
|
+
const lastDayOfMonth = new Date(Date.UTC(year, monthIndex + 1, 0)).getUTCDate();
|
|
134
|
+
const preferred = Date.UTC(year, monthIndex, CHECKIN_DAY);
|
|
135
|
+
let checkinMs = Math.max(preferred, earliest);
|
|
136
|
+
|
|
137
|
+
// Only when no day left in this year's month clears the lead time does the year roll. The
|
|
138
|
+
// check-out may spill into the next month; a stay starting on the 30th is still a stay in
|
|
139
|
+
// that month, and refusing it would send the reader a year away for no reader-visible gain.
|
|
140
|
+
if (checkinMs > Date.UTC(year, monthIndex, lastDayOfMonth)) {
|
|
141
|
+
checkinMs = Date.UTC(year + 1, monthIndex, CHECKIN_DAY);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const checkoutMs = checkinMs + STAY_NIGHTS * MS_PER_DAY;
|
|
145
|
+
return { checkin: toIso(checkinMs), checkout: toIso(checkoutMs), label: formatStay(checkinMs, checkoutMs) };
|
|
146
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slot the monument tour band goes after (0 = lede, N = section N): the first slot
|
|
3
|
+
* from `preferred` on with no authored interlude, else the nearest free one before
|
|
4
|
+
* it, else the last. Shared by MonumentBody and the page's ToC so both agree.
|
|
5
|
+
*/
|
|
6
|
+
export function pickBandSlot(sectionCount: number, interludes: { afterSection: number }[] = [], preferred = 2): number {
|
|
7
|
+
const last = sectionCount;
|
|
8
|
+
const clamp = (n: number) => Math.min(Math.max(n, 0), last);
|
|
9
|
+
const taken = new Set(interludes.map((il) => clamp(il.afterSection)));
|
|
10
|
+
const start = clamp(preferred);
|
|
11
|
+
for (let i = start; i <= last; i++) if (!taken.has(i)) return i;
|
|
12
|
+
for (let i = start - 1; i >= 1; i--) if (!taken.has(i)) return i;
|
|
13
|
+
return last;
|
|
14
|
+
}
|
package/src/interludes/index.ts
CHANGED
package/src/schema/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ export {
|
|
|
12
12
|
dataAddressPolicy, postalAddress, geoCoordinates, aggregateRating,
|
|
13
13
|
} from './values.ts';
|
|
14
14
|
export type { AddressPolicy, PostalAddressParts } from './values.ts';
|
|
15
|
+
export { parsePriceRange, calculateAggregatePriceRange } from './price.ts';
|
|
15
16
|
export {
|
|
16
17
|
DAY_KEYS, HOURS_ALWAYS_OPEN, parseHoursShifts, closedFieldDays, resolveOpeningHours, assertedOpenDays,
|
|
17
18
|
generateOpeningHours, parseHoursString, hoursStringOpenDays,
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse restaurant averagePrice string to extract min and max prices
|
|
3
|
+
* Examples:
|
|
4
|
+
* - "8-15 euros" → { min: 8, max: 15 }
|
|
5
|
+
* - "20-35 euros" → { min: 20, max: 35 }
|
|
6
|
+
* - "From 25€" → { min: 25, max: 25 }
|
|
7
|
+
* - "15€" → { min: 15, max: 15 }
|
|
8
|
+
*/
|
|
9
|
+
export function parsePriceRange(priceStr: string): { min: number; max: number } | null {
|
|
10
|
+
if (!priceStr) return null;
|
|
11
|
+
|
|
12
|
+
// Remove common words and normalize
|
|
13
|
+
const normalized = priceStr
|
|
14
|
+
.toLowerCase()
|
|
15
|
+
.replace(/euros?/g, '')
|
|
16
|
+
.replace(/€/g, '')
|
|
17
|
+
.replace(/from\s*/gi, '')
|
|
18
|
+
.trim();
|
|
19
|
+
|
|
20
|
+
// Match patterns like "8-15", "20-35"
|
|
21
|
+
const rangeMatch = normalized.match(/(\d+)\s*-\s*(\d+)/);
|
|
22
|
+
if (rangeMatch) {
|
|
23
|
+
return {
|
|
24
|
+
min: parseFloat(rangeMatch[1]),
|
|
25
|
+
max: parseFloat(rangeMatch[2]),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Match single price like "15", "25"
|
|
30
|
+
const singleMatch = normalized.match(/(\d+)/);
|
|
31
|
+
if (singleMatch) {
|
|
32
|
+
const price = parseFloat(singleMatch[1]);
|
|
33
|
+
return { min: price, max: price };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Calculate aggregate price range from multiple price strings
|
|
41
|
+
*/
|
|
42
|
+
export function calculateAggregatePriceRange(priceStrings: string[]): {
|
|
43
|
+
lowPrice: string;
|
|
44
|
+
highPrice: string;
|
|
45
|
+
} | null {
|
|
46
|
+
const parsedPrices = priceStrings
|
|
47
|
+
.map(parsePriceRange)
|
|
48
|
+
.filter((p): p is NonNullable<typeof p> => p !== null);
|
|
49
|
+
|
|
50
|
+
if (parsedPrices.length === 0) return null;
|
|
51
|
+
|
|
52
|
+
const allMins = parsedPrices.map(p => p.min);
|
|
53
|
+
const allMaxs = parsedPrices.map(p => p.max);
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
lowPrice: Math.min(...allMins).toString(),
|
|
57
|
+
highPrice: Math.max(...allMaxs).toString(),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// "18,50 euros adulte" → "From 18,50€"; anything without a euro amount keeps its first clause.
|
|
2
|
+
export function formatFromPrice(priceText: string | undefined): string | null {
|
|
3
|
+
if (!priceText) return null;
|
|
4
|
+
const match = priceText.match(/(\d+(?:[,.]\d+)?)\s*(?:euros?|€)/i);
|
|
5
|
+
return match ? `From ${match[1]}€` : priceText.split(',')[0];
|
|
6
|
+
}
|
package/src/text/index.ts
CHANGED
|
@@ -3,3 +3,5 @@ export { slugify } from './slugify.ts';
|
|
|
3
3
|
export { fitWithinLength } from './fitWithinLength.ts';
|
|
4
4
|
export { fitLength } from './fitLength.ts';
|
|
5
5
|
export { readingTime } from './readingTime.ts';
|
|
6
|
+
export { parseMarkdown, parseMarkdownInline, parseMarkdownBlock, stripMarkdown } from './markdown.ts';
|
|
7
|
+
export { formatFromPrice } from './cardCopy.ts';
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Markdown → HTML for content prose, promoted from the sites' src/utils/markdown.ts (SLA-3016; Granada's copy).
|
|
2
|
+
// No Rule 38 rel here: the outboundLinkRel build integration applies it once to every anchor of the built HTML.
|
|
3
|
+
import { marked } from 'marked';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fixes relative internal links by prepending the base path
|
|
7
|
+
* Matches href="/path" (not href="//url") and prepends the base
|
|
8
|
+
*/
|
|
9
|
+
function fixInternalLinks(html: string, basePath: string = '/'): string {
|
|
10
|
+
const cleanBase = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
|
|
11
|
+
|
|
12
|
+
// Replace href="/path" with href="${base}/path"
|
|
13
|
+
// But NOT href="//external" or href="http"
|
|
14
|
+
return html.replace(/href="\/(?!\/|https?:)/g, `href="${cleanBase}/`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parses Markdown content to HTML with base path support
|
|
19
|
+
* Automatically prepends the base path to relative internal links
|
|
20
|
+
*/
|
|
21
|
+
export function parseMarkdown(markdown: string, basePath: string = '/'): string {
|
|
22
|
+
if (!markdown) return '';
|
|
23
|
+
|
|
24
|
+
const html = marked.parse(markdown, {
|
|
25
|
+
breaks: true,
|
|
26
|
+
gfm: true,
|
|
27
|
+
}) as string;
|
|
28
|
+
|
|
29
|
+
return fixInternalLinks(html.trim(), basePath);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Converts `[^N]` footnote markers to citation anchors. Drops (rather than
|
|
34
|
+
* renders) any marker that can't point at a real citation — N < 1, or N
|
|
35
|
+
* greater than `sourcesLength` when the caller supplies it — instead of
|
|
36
|
+
* emitting a dangling `<a href="#fn-N">` link to a footnote that doesn't
|
|
37
|
+
* exist. This is defense-in-depth: the real guarantee that markers are
|
|
38
|
+
* well-formed is the validate-footnote-markers.mjs pre-push gate (SLA-1571),
|
|
39
|
+
* but that gate is bypassable (--no-verify, an unconfigured core.hooksPath)
|
|
40
|
+
* and has its own edge cases, so the renderer itself no longer trusts N
|
|
41
|
+
* blindly. `sourcesLength` is optional — omit it (as every non-article
|
|
42
|
+
* caller currently does) to skip the upper-bound check; the N ≥ 1 check
|
|
43
|
+
* always applies since sources[] is 1-indexed everywhere in this codebase.
|
|
44
|
+
*/
|
|
45
|
+
function convertFootnotes(html: string, sourcesLength?: number): string {
|
|
46
|
+
return html.replace(/\[\^(\d+)\]/g, (_match, nStr: string) => {
|
|
47
|
+
const n = Number(nStr);
|
|
48
|
+
if (n < 1 || (sourcesLength !== undefined && n > sourcesLength)) return '';
|
|
49
|
+
// No `id="fnref-${n}"`: this fires per marker OCCURRENCE, not per distinct N, so an
|
|
50
|
+
// article citing one source twice shipped duplicate DOM ids — 776 excess on cordoba
|
|
51
|
+
// alone. Nothing ever read the id (AuthoritativeSources emits the `fn-N` targets but no
|
|
52
|
+
// back-links), so it was pure invalid markup. Restore it only alongside a real consumer,
|
|
53
|
+
// and make it unique when you do.
|
|
54
|
+
return `<sup><a href="#fn-${n}" class="footnote-ref">[${n}]</a></sup>`;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Parses Markdown content to HTML with inline mode (no paragraph wrapping)
|
|
60
|
+
* Useful for short descriptions that shouldn't be wrapped in <p> tags
|
|
61
|
+
* Also prepends the base path to relative internal links
|
|
62
|
+
*/
|
|
63
|
+
export function parseMarkdownInline(markdown: string, basePath: string = '/', sourcesLength?: number): string {
|
|
64
|
+
if (!markdown) return '';
|
|
65
|
+
|
|
66
|
+
let html = marked.parseInline(markdown) as string;
|
|
67
|
+
html = fixInternalLinks(html.trim(), basePath);
|
|
68
|
+
html = convertFootnotes(html, sourcesLength);
|
|
69
|
+
|
|
70
|
+
return html;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parses block-level Markdown (lists, blockquotes, tables) to HTML.
|
|
75
|
+
* Unlike parseMarkdown, it ALSO converts footnote refs ([^n]) to <sup> anchors —
|
|
76
|
+
* matching parseMarkdownInline — so footnotes inside list items render correctly.
|
|
77
|
+
* Use for article-body chunks that are block constructs; parseMarkdownInline
|
|
78
|
+
* leaves "- " / ">" / "|" as literal text.
|
|
79
|
+
*/
|
|
80
|
+
export function parseMarkdownBlock(markdown: string, basePath: string = '/', sourcesLength?: number): string {
|
|
81
|
+
if (!markdown) return '';
|
|
82
|
+
|
|
83
|
+
let html = marked.parse(markdown, {
|
|
84
|
+
breaks: true,
|
|
85
|
+
gfm: true,
|
|
86
|
+
}) as string;
|
|
87
|
+
|
|
88
|
+
html = fixInternalLinks(html.trim(), basePath);
|
|
89
|
+
html = convertFootnotes(html, sourcesLength);
|
|
90
|
+
|
|
91
|
+
return html;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Strips Markdown syntax down to plain text.
|
|
96
|
+
*
|
|
97
|
+
* For contexts that must NOT receive HTML — chiefly Schema.org JSON-LD string
|
|
98
|
+
* values such as FAQPage acceptedAnswer.text, where authors write markdown in
|
|
99
|
+
* `answer` and the raw `**asterisks**` were being emitted into structured data
|
|
100
|
+
* that search engines and answer engines read verbatim.
|
|
101
|
+
*/
|
|
102
|
+
export function stripMarkdown(markdown: string): string {
|
|
103
|
+
if (!markdown) return '';
|
|
104
|
+
return markdown
|
|
105
|
+
// Raw HTML tags go FIRST, before any emphasis rule sees them. A hand-authored affiliate
|
|
106
|
+
// <a> whose query carries partner_id and cmp=site__page__place otherwise reaches the bold rule
|
|
107
|
+
// (/(\*\*|__)(.*?)\1/g), which eats the cmp's `__` separators, and the italic rule,
|
|
108
|
+
// which pairs `target="_blank"`'s underscore with `partner_id`'s — shipping
|
|
109
|
+
// `partnerid=X&cmp=siteplaceplace` into FAQPage JSON-LD. That is a real monetization
|
|
110
|
+
// leak, not just cosmetic: the mangled key is not a partner id, so a click from a rich
|
|
111
|
+
// result is unattributed. Tags are dropped and their text kept, which is what every
|
|
112
|
+
// caller of this function wants (schema plain text, alt text).
|
|
113
|
+
// Block boundaries become a SPACE, inline tags collapse to nothing. `</p><p>` removed
|
|
114
|
+
// outright glued sentences together ("…the Realejo.See all itineraries") in 64 FAQPage
|
|
115
|
+
// answers, while `</a>.` and `</strong>'s` must NOT gain a space. The pattern is also
|
|
116
|
+
// anchored to a real tag name so a prose comparison ("groups under <10 people") or a
|
|
117
|
+
// CommonMark autolink is not silently eaten as if it were markup.
|
|
118
|
+
.replace(/<\/(?:p|div|li|h[1-6]|tr|td|th|blockquote|section|article)\s*>|<br\s*\/?>/gi, ' ')
|
|
119
|
+
// The tag name must be alphanumeric AND followed by whitespace, `/` or `>`. A looser
|
|
120
|
+
// `<[a-zA-Z][^>]*>` still ate a CommonMark autolink (`<https://x>`) and a bare email
|
|
121
|
+
// angle (`<name@example.com>`), because both start with a letter.
|
|
122
|
+
.replace(/<\/?[a-zA-Z][a-zA-Z0-9]*(?:\s[^>]*)?\/?>/g, '')
|
|
123
|
+
.replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1') // images -> alt text
|
|
124
|
+
.replace(/\[\^\d+\]/g, '') // footnote markers
|
|
125
|
+
.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1') // links -> anchor text
|
|
126
|
+
.replace(/(\*\*|__)(.*?)\1/g, '$2') // bold
|
|
127
|
+
.replace(/(^|[^*_])[*_]([^*_]+)[*_]/g, '$1$2') // italic
|
|
128
|
+
.replace(/`([^`]+)`/g, '$1') // inline code
|
|
129
|
+
.replace(/\s{2,}/g, ' ')
|
|
130
|
+
.trim();
|
|
131
|
+
}
|
package/src/url/index.ts
CHANGED
|
@@ -6,5 +6,5 @@ export { buildImageMap, keyBySlug } from './imageMap.ts';
|
|
|
6
6
|
export { articleFeedItem } from './feed.ts';
|
|
7
7
|
export type { FeedArticle, FeedItem } from './feed.ts';
|
|
8
8
|
export { pageTypeOf } from './pageType.ts';
|
|
9
|
-
export { outboundClassifier, outboundHostKey } from './outbound.ts';
|
|
9
|
+
export { outboundClassifier, outboundHostKey, applyOutboundRel } from './outbound.ts';
|
|
10
10
|
export type { OutboundDomainStore } from './outbound.ts';
|
package/src/url/outbound.ts
CHANGED
|
@@ -25,3 +25,38 @@ export function outboundClassifier(store: OutboundDomainStore | Record<string, s
|
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
const ATTR_VALUE = (name: string) => new RegExp(`(\\s)${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`, 'i');
|
|
30
|
+
const HREF = ATTR_VALUE('href');
|
|
31
|
+
const REL = ATTR_VALUE('rel');
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The one Rule 38 HTML rewriter: every `<a>` whose absolute http(s) href `classify` maps to `competitor` gains
|
|
35
|
+
* `nofollow`, to `affiliate` `sponsored`. `href` and `rel` are read only as whole attributes (never `data-href`
|
|
36
|
+
* or `data-rel`), and the policy check reads the rel tokens only. An anchor whose rel already holds the policy token
|
|
37
|
+
* is left as it is; a missing or empty rel becomes `rel="<policy> noopener"`; any other rel keeps its tokens and
|
|
38
|
+
* gains `noopener` and the policy token.
|
|
39
|
+
*/
|
|
40
|
+
export function applyOutboundRel(html: string, classify: (url: string) => string): string {
|
|
41
|
+
return html.replace(/<a\b([^>]*)>/gi, (tag, attrs: string) => {
|
|
42
|
+
const h = HREF.exec(attrs);
|
|
43
|
+
const href = h ? (h[2] ?? h[3]) : undefined;
|
|
44
|
+
if (!href) return tag;
|
|
45
|
+
let protocol: string;
|
|
46
|
+
try {
|
|
47
|
+
protocol = new URL(href).protocol;
|
|
48
|
+
} catch {
|
|
49
|
+
return tag;
|
|
50
|
+
}
|
|
51
|
+
if (protocol !== 'http:' && protocol !== 'https:') return tag;
|
|
52
|
+
const cls = classify(href);
|
|
53
|
+
const policy = cls === 'competitor' ? 'nofollow' : cls === 'affiliate' ? 'sponsored' : null;
|
|
54
|
+
if (!policy) return tag;
|
|
55
|
+
const r = REL.exec(attrs);
|
|
56
|
+
if (!r) return `<a${attrs} rel="${policy} noopener">`;
|
|
57
|
+
const tokens = (r[2] ?? r[3]).split(/\s+/).filter(Boolean);
|
|
58
|
+
if (tokens.some((t) => t.toLowerCase() === policy)) return tag;
|
|
59
|
+
const value = tokens.length ? [...new Set([...tokens, 'noopener', policy])].join(' ') : `${policy} noopener`;
|
|
60
|
+
return `<a${attrs.slice(0, r.index)}${r[1]}rel="${value}"${attrs.slice(r.index + r[0].length)}>`;
|
|
61
|
+
});
|
|
62
|
+
}
|