69 lines
2.5 KiB
JavaScript
69 lines
2.5 KiB
JavaScript
/** Read a dotted path (e.g. 'meta.noindex') off a loosely-typed object. */ function readPath(obj, path) {
|
|
let cur = obj;
|
|
for (const key of path){
|
|
if (cur == null || typeof cur !== 'object') {
|
|
return undefined;
|
|
}
|
|
cur = cur[key];
|
|
}
|
|
return cur;
|
|
}
|
|
/** Write a dotted path, creating intermediate objects as needed. */ function writePath(obj, path, value) {
|
|
let cur = obj;
|
|
for(let i = 0; i < path.length - 1; i++){
|
|
const key = path[i];
|
|
const next = cur[key];
|
|
if (next == null || typeof next !== 'object') {
|
|
cur[key] = {};
|
|
}
|
|
cur = cur[key];
|
|
}
|
|
cur[path[path.length - 1]] = value;
|
|
}
|
|
/**
|
|
* A sidebar `noindex` checkbox that mirrors into `meta.noindex` (what the
|
|
* sitemap and metadata generators actually read). Replaces the hand-rolled
|
|
* checkbox + `beforeChange` hook with its `(data as any).meta.noindex = …`
|
|
* glue copied into every project — no `any`, one source of truth for editors,
|
|
* one stable read location for the engine.
|
|
*
|
|
* Idempotent and order-independent: if either the checkbox OR an existing
|
|
* `meta.noindex` is set, the page ends up noindex; the two can't drift apart.
|
|
*
|
|
* // collections/Pages.ts
|
|
* import { buildNoindexField } from '@intecion/ipal-kit'
|
|
* fields: [ /* … *\/ buildNoindexField() ],
|
|
*/ export function buildNoindexField(args = {}) {
|
|
const { name = 'noindex', description = 'Check to exclude this page from indexing (robots: noindex, follow) and from sitemap.xml.', label = 'Exclude from indexing (noindex)', metaPath = 'meta.noindex', overrides = {} } = args;
|
|
const segments = metaPath.split('.');
|
|
const mirror = ({ data, value })=>{
|
|
const shouldNoindex = Boolean(value ?? readPath(data, segments));
|
|
if (data && typeof data === 'object') {
|
|
writePath(data, segments, shouldNoindex);
|
|
}
|
|
return shouldNoindex;
|
|
};
|
|
const { admin: adminOverrides, hooks: hooksOverrides, ...restOverrides } = overrides;
|
|
return {
|
|
name,
|
|
type: 'checkbox',
|
|
admin: {
|
|
description,
|
|
position: 'sidebar',
|
|
...adminOverrides
|
|
},
|
|
defaultValue: false,
|
|
hooks: {
|
|
...hooksOverrides,
|
|
// mirror runs first, then any project-supplied beforeChange hooks.
|
|
beforeChange: [
|
|
mirror,
|
|
...hooksOverrides?.beforeChange ?? []
|
|
]
|
|
},
|
|
label,
|
|
...restOverrides
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=buildNoindexField.js.map
|