Skip to content

Commit

Permalink
feat: allow passing filter options to the reference field, and add th…
Browse files Browse the repository at this point in the history
…e option to use weak references
  • Loading branch information
marcusforsberg committed May 31, 2024
1 parent 203fe85 commit 2d441a7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
4 changes: 4 additions & 0 deletions src/linkField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import type {LinkFieldPluginOptions, LinkValue} from './types'
export const linkField = definePlugin<LinkFieldPluginOptions>(
({
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.',
Expand Down Expand Up @@ -96,8 +98,10 @@ export const linkField = definePlugin<LinkFieldPluginOptions>(
to: linkableSchemaTypes.map((type) => ({
type,
})),
weak: weakReferences,
options: {
disableNew: true,
...referenceFilterOptions,
},
description: descriptions?.internal,
hidden: ({parent}) => !!parent?.type && parent?.type !== 'internal',
Expand Down
25 changes: 24 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2d441a7

Please sign in to comment.