diff --git a/README.md b/README.md index d3b8cbe..82e915c 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,8 @@ When configuring the plugin in `sanity.config.ts`, these are the global options | Option | Default Value | Description | | ------------- | ------------- | ------------- | | linkableSchemaTypes | `['page']` | An array of schema types that should be allowed in internal links. | +| weakReferences | false | Make internal links use [weak references](https://www.sanity.io/docs/reference-type#f45f659e7b28) | +| referenceFilterOptions | undefined | Custom [filter options](https://www.sanity.io/docs/reference-type#1ecd78ab1655) passed to the reference input component for internal links. Use it to filter the documents that should be available for linking, eg. by locale. | | descriptions | *See [linkField.tsx](https://github.com/winteragency/sanity-plugin-link-field/blob/main/src/linkField.tsx)* | Override the descriptions of the different subfields. | | enableLinkParameters | `true` | Whether the user should be able to set custom URL parameters for internal and external links. | | enableAnchorLinks | `true` | Whether the user should be able to set custom anchors (URL fragments) for internal and external links. | diff --git a/src/linkField.tsx b/src/linkField.tsx index 2035fa3..7a26271 100644 --- a/src/linkField.tsx +++ b/src/linkField.tsx @@ -44,6 +44,8 @@ import type {LinkFieldPluginOptions, LinkValue} from './types' export const linkField = definePlugin( ({ linkableSchemaTypes = ['page'], + weakReferences = false, + referenceFilterOptions, descriptions = { internal: 'Link to another page or document on the website.', external: 'Link to an absolute URL to a page on another website.', @@ -96,8 +98,10 @@ export const linkField = definePlugin( to: linkableSchemaTypes.map((type) => ({ type, })), + weak: weakReferences, options: { disableNew: true, + ...referenceFilterOptions, }, description: descriptions?.internal, hidden: ({parent}) => !!parent?.type && parent?.type !== 'internal', diff --git a/src/types.ts b/src/types.ts index 1ed33e2..96176b1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,12 @@ import {ComponentType} from 'react' -import type {CurrentUser, ObjectInputProps, ObjectSchemaType, Path, SanityDocument} from 'sanity' +import type { + CurrentUser, + ObjectInputProps, + ObjectSchemaType, + Path, + ReferenceFilterOptions, + SanityDocument, +} from 'sanity' export interface CustomizableLink { parameters?: string @@ -76,6 +83,22 @@ export interface LinkFieldPluginOptions { */ linkableSchemaTypes: string[] + /** + * Custom filter options passed to the reference input component for internal links. + * Use it to filter the documents that should be available for linking, eg. by locale. + * + * @see https://www.sanity.io/docs/reference-type#1ecd78ab1655 + * @defaultValue undefined + */ + referenceFilterOptions?: ReferenceFilterOptions + + /** + * Make internal links use weak references + * @see https://www.sanity.io/docs/reference-type#f45f659e7b28 + * @defaultValue false + */ + weakReferences?: boolean + /** Override the descriptions of the different subfields. */ descriptions?: { internal?: string