diff --git a/README.md b/README.md index 7d6f344..2378e36 100644 --- a/README.md +++ b/README.md @@ -268,8 +268,8 @@ selected variant; add `--all-options` to include the other variants. | Command | Description | |---|---| -| `mna options add --from-json= [--free-cancellation-until=] [--details=] [--photos=]` | Add an option from JSON. `--free-cancellation-until`, `--details` and `--photos` (accommodation only) merge into the body. | -| `mna options edit [--from-json=] [--free-cancellation-until=] [--details=] [--photos=]` | Edit an option. For accommodation, each flag works alone, without a JSON file. `--details` changes only the properties it names; `--photos` replaces the list. | +| `mna options add --from-json= [--free-cancellation-until=] [--details=] [--photos=] [--website=]` | Add an option from JSON. `--free-cancellation-until`, `--details` and `--photos` (accommodation only) merge into the body. | +| `mna options edit [--from-json=] [--free-cancellation-until=] [--details=] [--photos=] [--website=]` | Edit an option. For accommodation, each flag works alone, without a JSON file. `--details` changes only the properties it names; `--photos` replaces the list. | | `mna options delete [--yes]` | Delete an option. | | `mna options select ` | Select an option. | | `mna options deselect ` | Deselect the current option. | diff --git a/skills/mna/references/cli-and-schemas.md b/skills/mna/references/cli-and-schemas.md index c7fd803..759c7f9 100644 --- a/skills/mna/references/cli-and-schemas.md +++ b/skills/mna/references/cli-and-schemas.md @@ -13,8 +13,8 @@ variants add --name [--adults --child-ages --no-children - destinations add --place [--start-date --end-date --notes --return-to-home] edit [--place --notes --start-date --end-date --return-to-home/--no-…] reorder --order=k1,k2,k3 | delete -options add --from-json [--free-cancellation-until ] [--details ] [--photos ] - edit [--from-json ] [--free-cancellation-until ] [--details ] [--photos ] +options add --from-json [--free-cancellation-until ] [--details ] [--photos ] [--website ] + edit [--from-json ] [--free-cancellation-until ] [--details ] [--photos ] [--website ] select | deselect delete events add --from-json | edit --from-json @@ -91,7 +91,7 @@ options, and leave out anything equally true of all of them, or unknown. | `dinner`, `bedLayout`, `skiBackNote`, `mainLiftName`, `skiStorage`, `kidsFacilities`, `wellness`, `pool`, `beach`, `parking`, `noise`, `view`, `airConditioning`, `laundry`, `accessibility`, `pets`, `sourceNote` | text, up to 300 characters | On an edit, only the `stayDetails` properties you send change. `null` clears one, and -`--details null` clears them all. `--photos` replaces the whole list, up to 12 photos, and +`--website` is the place's own official site, never a booking or listing site; leave it out if you can't confirm it. Photos from it may then be linked automatically, credited to the site. `--website null` clears it. `--details null` clears them all. `--photos` replaces the whole list, up to 12 photos, and `--photos null` clears it. Unknown properties are rejected. `photos` take `{ "url", "credit"?, "creditUrl"? }`. Each URL must be a directly linkable http or diff --git a/src/commands/options/add.ts b/src/commands/options/add.ts index d894c6b..bd2181d 100644 --- a/src/commands/options/add.ts +++ b/src/commands/options/add.ts @@ -51,6 +51,11 @@ export const optionsAddCommand = defineCommand({ description: 'Accommodation only: photos as a JSON array of { "url", "credit"? }. Only images the owner may use.', }, + website: { + type: 'string', + description: + "Accommodation only: the place's own official website, not a booking site. Photos from it may be linked automatically, credited to the site.", + }, json: { type: 'boolean', default: false, description: 'Output as JSON.' }, }, async run({ args }) { @@ -58,7 +63,7 @@ export const optionsAddCommand = defineCommand({ assertKind(args.kind) const body = await readJsonObject(args['from-json']) applyFreeCancellation(body, args['free-cancellation-until'], args.kind) - applyStayFlags(body, { details: args.details, photos: args.photos }, args.kind) + applyStayFlags(body, { details: args.details, photos: args.photos, website: args.website }, args.kind) const creds = await loadCredentials() const apiKey = resolveApiKey(creds) diff --git a/src/commands/options/edit.ts b/src/commands/options/edit.ts index 54849d5..634d66c 100644 --- a/src/commands/options/edit.ts +++ b/src/commands/options/edit.ts @@ -51,6 +51,11 @@ export const optionsEditCommand = defineCommand({ description: 'Accommodation only: photos as a JSON array of { "url", "credit"? }, replacing the list; null clears it. Only images the owner may use.', }, + website: { + type: 'string', + description: + "Accommodation only: the place's own official website, not a booking site. Photos from it may be linked automatically, credited to the site. null clears it.", + }, json: { type: 'boolean', default: false, description: 'Output as JSON.' }, }, async run({ args }) { @@ -61,16 +66,17 @@ export const optionsEditCommand = defineCommand({ args['free-cancellation-until'], args.details, args.photos, + args.website, ].some((flag) => flag !== undefined) if (!hasFlag) { throw new Error( - 'Specify at least one of --from-json, --free-cancellation-until, --details, --photos.', + 'Specify at least one of --from-json, --free-cancellation-until, --details, --photos, --website.', ) } const body = args['from-json'] !== undefined ? await readJsonObject(args['from-json']) : {} applyFreeCancellation(body, args['free-cancellation-until'], args.kind) - applyStayFlags(body, { details: args.details, photos: args.photos }, args.kind) + applyStayFlags(body, { details: args.details, photos: args.photos, website: args.website }, args.kind) const creds = await loadCredentials() const apiKey = resolveApiKey(creds) diff --git a/src/commands/options/stay-details.test.ts b/src/commands/options/stay-details.test.ts index d4a1805..b6769c8 100644 --- a/src/commands/options/stay-details.test.ts +++ b/src/commands/options/stay-details.test.ts @@ -8,6 +8,20 @@ describe('applyStayFlags', () => { expect(body).toEqual({ name: 'Villa' }) }) + test('sets and clears the website', () => { + const body: Record = {} + applyStayFlags(body, { website: 'https://hotel.example/' }, 'accommodation') + expect(body.website).toBe('https://hotel.example/') + + applyStayFlags(body, { website: 'null' }, 'accommodation') + expect(body.website).toBeNull() + }) + + test('refuses a website that is not a web address, and other kinds', () => { + expect(() => applyStayFlags({}, { website: 'javascript:alert(1)' }, 'accommodation')).toThrow('--website') + expect(() => applyStayFlags({}, { website: 'https://hotel.example/' }, 'transport')).toThrow('accommodation') + }) + test('sets stayDetails and photos', () => { const body: Record = {} applyStayFlags( diff --git a/src/commands/options/stay-details.ts b/src/commands/options/stay-details.ts index d3d177b..e48c813 100644 --- a/src/commands/options/stay-details.ts +++ b/src/commands/options/stay-details.ts @@ -1,18 +1,18 @@ import { parseJsonFlag, parseJsonObjectFlag } from '../../util/json-flag' /** - * Merge the `--details` and `--photos` flags into an option body. Both fields - * exist only on accommodation, so any other kind is rejected. A flag overrides + * Merge the `--details`, `--photos` and `--website` flags into an option body. These + * fields exist only on accommodation, so any other kind is rejected. A flag overrides * the same field in the `--from-json` file. */ export function applyStayFlags( body: Record, - flags: { details?: string; photos?: string }, + flags: { details?: string; photos?: string; website?: string }, kind: string, ): void { - if (flags.details === undefined && flags.photos === undefined) return + if (flags.details === undefined && flags.photos === undefined && flags.website === undefined) return if (kind !== 'accommodation') { - throw new Error('--details and --photos are only valid for the "accommodation" kind.') + throw new Error('--details, --photos and --website are only valid for the "accommodation" kind.') } if (flags.details !== undefined) { @@ -27,4 +27,10 @@ export function applyStayFlags( } body.photos = photos } + if (flags.website !== undefined) { + if (flags.website !== 'null' && !/^https?:\/\/\S+$/.test(flags.website)) { + throw new Error('--website must be an http or https URL, or null to clear it.') + } + body.website = flags.website === 'null' ? null : flags.website + } }