@prismicio/client 7.6.0-alpha.0 → 7.7.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/README.md +11 -11
- package/dist/_node_modules/hast-util-whitespace/lib/index.cjs +11 -0
- package/dist/_node_modules/hast-util-whitespace/lib/index.cjs.map +1 -0
- package/dist/_node_modules/hast-util-whitespace/lib/index.js +11 -0
- package/dist/_node_modules/hast-util-whitespace/lib/index.js.map +1 -0
- package/dist/buildQueryURL.cjs.map +1 -1
- package/dist/buildQueryURL.js.map +1 -1
- package/dist/createClient.cjs +1 -4
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.js +1 -4
- package/dist/createClient.js.map +1 -1
- package/dist/errors/PrismicError.cjs +1 -4
- package/dist/errors/PrismicError.cjs.map +1 -1
- package/dist/errors/PrismicError.js +1 -4
- package/dist/errors/PrismicError.js.map +1 -1
- package/dist/errors/PrismicRichTextError.cjs +6 -0
- package/dist/errors/PrismicRichTextError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +2 -0
- package/dist/errors/PrismicRichTextError.js +6 -0
- package/dist/errors/PrismicRichTextError.js.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs +7 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +3 -0
- package/dist/errors/PrismicRichTextSerializerError.js +7 -0
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -0
- package/dist/filter.cjs.map +1 -1
- package/dist/filter.js.map +1 -1
- package/dist/getGraphQLEndpoint.cjs.map +1 -1
- package/dist/getGraphQLEndpoint.js.map +1 -1
- package/dist/getRepositoryEndpoint.cjs.map +1 -1
- package/dist/getRepositoryEndpoint.js.map +1 -1
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/getToolbarSrc.cjs.map +1 -1
- package/dist/getToolbarSrc.js.map +1 -1
- package/dist/helpers/asDate.cjs.map +1 -1
- package/dist/helpers/asDate.d.ts +1 -1
- package/dist/helpers/asDate.js.map +1 -1
- package/dist/helpers/asHTML.cjs +1 -1
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.js +1 -1
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/helpers/asImageSrc.cjs.map +1 -1
- package/dist/helpers/asImageSrc.js.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +2 -2
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asLinkAttrs.cjs.map +1 -1
- package/dist/helpers/asLinkAttrs.d.ts +1 -21
- package/dist/helpers/asLinkAttrs.js.map +1 -1
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/documentToLinkField.cjs +1 -0
- package/dist/helpers/documentToLinkField.cjs.map +1 -1
- package/dist/helpers/documentToLinkField.d.ts +1 -1
- package/dist/helpers/documentToLinkField.js +1 -0
- package/dist/helpers/documentToLinkField.js.map +1 -1
- package/dist/helpers/isFilled.cjs.map +1 -1
- package/dist/helpers/isFilled.d.ts +20 -66
- package/dist/helpers/isFilled.js.map +1 -1
- package/dist/helpers/mapSliceZone.cjs.map +1 -1
- package/dist/helpers/mapSliceZone.js.map +1 -1
- package/dist/helpers/unstable_htmlAsRichText.cjs +11 -0
- package/dist/helpers/unstable_htmlAsRichText.cjs.map +1 -0
- package/dist/helpers/unstable_htmlAsRichText.d.ts +81 -0
- package/dist/helpers/unstable_htmlAsRichText.js +11 -0
- package/dist/helpers/unstable_htmlAsRichText.js.map +1 -0
- package/dist/helpers/unstable_markdownAsRichText.cjs +12 -0
- package/dist/helpers/unstable_markdownAsRichText.cjs.map +1 -0
- package/dist/helpers/unstable_markdownAsRichText.d.ts +30 -0
- package/dist/helpers/unstable_markdownAsRichText.js +12 -0
- package/dist/helpers/unstable_markdownAsRichText.js.map +1 -0
- package/dist/index.cjs +8 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/isRepositoryEndpoint.cjs.map +1 -1
- package/dist/isRepositoryEndpoint.js.map +1 -1
- package/dist/lib/RichTextFieldBuilder.cjs +112 -0
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +35 -0
- package/dist/lib/RichTextFieldBuilder.js +112 -0
- package/dist/lib/RichTextFieldBuilder.js.map +1 -0
- package/dist/lib/castThunk.d.ts +1 -1
- package/dist/lib/escapeHTML.cjs.map +1 -1
- package/dist/lib/escapeHTML.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +88 -0
- package/dist/lib/filterRichTextField.cjs.map +1 -0
- package/dist/lib/filterRichTextField.d.ts +12 -0
- package/dist/lib/filterRichTextField.js +88 -0
- package/dist/lib/filterRichTextField.js.map +1 -0
- package/dist/lib/findRef.cjs.map +1 -1
- package/dist/lib/findRef.js.map +1 -1
- package/dist/lib/getPreviewCookie.cjs.map +1 -1
- package/dist/lib/getPreviewCookie.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +96 -0
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -0
- package/dist/lib/hastSerializerHelpers.d.ts +28 -0
- package/dist/lib/hastSerializerHelpers.js +96 -0
- package/dist/lib/hastSerializerHelpers.js.map +1 -0
- package/dist/lib/hastToRichText.cjs +165 -0
- package/dist/lib/hastToRichText.cjs.map +1 -0
- package/dist/lib/hastToRichText.d.ts +30 -0
- package/dist/lib/hastToRichText.js +165 -0
- package/dist/lib/hastToRichText.js.map +1 -0
- package/dist/lib/rehypeRichText.cjs +59 -0
- package/dist/lib/rehypeRichText.cjs.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +66 -0
- package/dist/lib/rehypeRichText.js +59 -0
- package/dist/lib/rehypeRichText.js.map +1 -0
- package/dist/lib/serializerHelpers.cjs.map +1 -1
- package/dist/lib/serializerHelpers.d.ts +1 -1
- package/dist/lib/serializerHelpers.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/richtext/asText.cjs.map +1 -1
- package/dist/richtext/asText.js.map +1 -1
- package/dist/richtext/asTree.cjs.map +1 -1
- package/dist/richtext/asTree.js.map +1 -1
- package/dist/richtext/composeSerializers.cjs.map +1 -1
- package/dist/richtext/composeSerializers.js.map +1 -1
- package/dist/richtext/serialize.cjs.map +1 -1
- package/dist/richtext/serialize.js.map +1 -1
- package/dist/richtext/types.cjs.map +1 -1
- package/dist/richtext/types.d.ts +3 -3
- package/dist/richtext/types.js.map +1 -1
- package/dist/richtext/wrapMapSerializer.cjs.map +1 -1
- package/dist/richtext/wrapMapSerializer.js.map +1 -1
- package/dist/types/model/richText.d.ts +2 -0
- package/dist/types/value/embed.cjs.map +1 -1
- package/dist/types/value/embed.js.map +1 -1
- package/dist/types/value/richText.cjs.map +1 -1
- package/dist/types/value/richText.d.ts +8 -1
- package/dist/types/value/richText.js.map +1 -1
- package/package.json +33 -21
- package/src/createClient.ts +4 -3
- package/src/errors/PrismicRichTextError.ts +1 -0
- package/src/errors/PrismicRichTextSerializerError.ts +3 -0
- package/src/helpers/asHTML.ts +21 -19
- package/src/helpers/asImagePixelDensitySrcSet.ts +14 -13
- package/src/helpers/asImageWidthSrcSet.ts +16 -15
- package/src/helpers/asLinkAttrs.ts +4 -4
- package/src/helpers/mapSliceZone.ts +20 -19
- package/src/helpers/unstable_htmlAsRichText.ts +121 -0
- package/src/helpers/unstable_markdownAsRichText.ts +52 -0
- package/src/index.ts +10 -0
- package/src/lib/RichTextFieldBuilder.ts +164 -0
- package/src/lib/filterRichTextField.ts +159 -0
- package/src/lib/hastSerializerHelpers.ts +153 -0
- package/src/lib/hastToRichText.ts +294 -0
- package/src/lib/rehypeRichText.ts +183 -0
- package/src/richtext/types.ts +3 -2
- package/src/types/model/richText.ts +4 -0
- package/src/types/value/embed.ts +1 -1
- package/src/types/value/geoPoint.ts +1 -1
- package/src/types/value/richText.ts +10 -1
- package/src/types/value/title.ts +1 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["// Imports are used for deprecations.\nimport type { CustomTypeModelIntegrationField } from \"./types/model/integration\";\nimport type { IntegrationField } from \"./types/value/integration\";\nimport { RichTextNodeType } from \"./types/value/richText\";\n\nimport type {\n\tHTMLRichTextFunctionSerializer,\n\tHTMLRichTextMapSerializer,\n} from \"./helpers/asHTML\";\nimport { mapSliceZone } from \"./helpers/mapSliceZone\";\n\nimport { filter } from \"./filter\";\n\n//=============================================================================\n// Client - Query content from Prismic.\n//=============================================================================\n\n// Primary Client API.\nexport { createClient, Client } from \"./createClient\";\n\n// API endpoint helpers.\nexport { getRepositoryEndpoint } from \"./getRepositoryEndpoint\";\nexport { getRepositoryName } from \"./getRepositoryName\";\nexport { getGraphQLEndpoint } from \"./getGraphQLEndpoint\";\nexport { isRepositoryName } from \"./isRepositoryName\";\nexport { isRepositoryEndpoint } from \"./isRepositoryEndpoint\";\nexport { buildQueryURL } from \"./buildQueryURL\";\n\n// Toolbar helpers.\nexport { getToolbarSrc } from \"./getToolbarSrc\";\n\n// Query filters API.\n/**\n * @deprecated Renamed to `filter`\n */\n// TODO: Remove when we remove support for deprecated `predicate` export.\nconst predicate = filter;\nexport { filter, predicate };\n\n// A collection of well-known cookie names shared between Prismic libraries and systems.\nexport * as cookie from \"./cookie\";\n\n// General types used to query content from Prismic. These are made public to allow users to better type their projects.\nexport type {\n\tAbortSignalLike,\n\tClientConfig,\n\tCreateClient,\n\tFetchLike,\n\tHttpRequestLike,\n\tRequestInitLike,\n\tResponseLike,\n} from \"./createClient\";\nexport type {\n\tBuildQueryURLArgs,\n\tOrdering,\n\tQueryParams,\n\tRoute,\n} from \"./buildQueryURL\";\n\n//=============================================================================\n// Helpers - Manipulate content from Prismic.\n//=============================================================================\n\n// Primary Helpers API.\nexport { asDate } from \"./helpers/asDate\";\nexport { asLink } from \"./helpers/asLink\";\nexport { asLinkAttrs } from \"./helpers/asLinkAttrs\";\nexport { asText } from \"./helpers/asText\";\nexport { asHTML } from \"./helpers/asHTML\";\nexport { asImageSrc } from \"./helpers/asImageSrc\";\nexport { asImageWidthSrcSet } from \"./helpers/asImageWidthSrcSet\";\nexport { asImagePixelDensitySrcSet } from \"./helpers/asImagePixelDensitySrcSet\";\nexport * as isFilled from \"./helpers/isFilled\";\n\n/**\n * @deprecated Renamed to `mapSliceZone`\n */\nconst unstable_mapSliceZone = mapSliceZone;\nexport { mapSliceZone, unstable_mapSliceZone };\n\n// Conversion helper.\nexport { documentToLinkField } from \"./helpers/documentToLinkField\";\n\nexport type { LinkResolverFunction } from \"./helpers/asLink\";\nexport type { AsLinkAttrsConfig } from \"./helpers/asLinkAttrs\";\nexport type { SliceMapper } from \"./helpers/mapSliceZone\";\n\n/**\n * @deprecated Renamed to `HTMLRichTextMapSerializer`\n */\ntype HTMLMapSerializer = HTMLRichTextMapSerializer;\n/**\n * @deprecated Renamed to `HTMLRichTextFunctionSerializer`\n */\ntype HTMLFunctionSerializer = HTMLRichTextFunctionSerializer;\nexport type {\n\tHTMLRichTextMapSerializer,\n\tHTMLRichTextFunctionSerializer,\n\tHTMLMapSerializer,\n\tHTMLFunctionSerializer,\n};\nexport type { HTMLRichTextSerializer } from \"./helpers/asHTML\";\n\n//=============================================================================\n// Errors - Custom errors for Prismic APIs.\n//=============================================================================\n\nexport { PrismicError } from \"./errors/PrismicError\";\nexport { ForbiddenError } from \"./errors/ForbiddenError\";\nexport { NotFoundError } from \"./errors/NotFoundError\";\nexport { RefNotFoundError } from \"./errors/RefNotFoundError\";\nexport { RefExpiredError } from \"./errors/RefExpiredError\";\nexport { PreviewTokenExpiredError } from \"./errors/PreviewTokenExpired\";\nexport { ParsingError } from \"./errors/ParsingError\";\nexport { RepositoryNotFoundError } from \"./errors/RepositoryNotFoundError\";\n\n//=============================================================================\n// Types - Types representing Prismic content, models, and API payloads.\n//=============================================================================\n\n// Values - Types representing Prismic content.\n/**\n * @deprecated Use {@link RichTextNodeType} instead.\n */\n// TODO: Remove in v8.\nconst Element = RichTextNodeType;\nexport { RichTextNodeType, Element };\nexport { LinkType } from \"./types/value/link\";\nexport { OEmbedType } from \"./types/value/embed\";\n\nexport type {\n\tPrismicDocument,\n\tPrismicDocumentWithUID,\n\tPrismicDocumentWithoutUID,\n\tPrismicDocumentHeader,\n\tAlternateLanguage,\n} from \"./types/value/document\";\n\nexport type {\n\t// RichText & Title\n\tRichTextField,\n\t// RichText & Title (block nodes)\n\tRTTextNodeBase,\n\tRTHeading1Node,\n\tRTHeading2Node,\n\tRTHeading3Node,\n\tRTHeading4Node,\n\tRTHeading5Node,\n\tRTHeading6Node,\n\tRTParagraphNode,\n\tRTPreformattedNode,\n\tRTListItemNode,\n\tRTOListItemNode,\n\t// RichText & Title (span nodes)\n\tRTSpanNodeBase,\n\tRTStrongNode,\n\tRTEmNode,\n\tRTLabelNode,\n\t// RichText & Title (media nodes)\n\tRTImageNode,\n\tRTEmbedNode,\n\t// RichText & Title (link nodes)\n\tRTLinkNode,\n\t// RichText & Title (serialization related nodes)\n\tRTListNode,\n\tRTOListNode,\n\tRTSpanNode,\n\t// RichText & Title (helpers)\n\tRTNode,\n\tRTTextNode,\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTAnyNode,\n} from \"./types/value/richText\";\nexport type { TitleField } from \"./types/value/title\";\n\nexport type {\n\tImageField,\n\tImageFieldImage,\n\tFilledImageFieldImage,\n\tEmptyImageFieldImage,\n} from \"./types/value/image\";\n\nexport type {\n\tEmptyLinkField,\n\tLinkField,\n\tFilledLinkToWebField,\n} from \"./types/value/link\";\nexport type {\n\tContentRelationshipField,\n\tFilledContentRelationshipField,\n} from \"./types/value/contentRelationship\";\nexport type {\n\tLinkToMediaField,\n\tFilledLinkToMediaField,\n} from \"./types/value/linkToMedia\";\n\nexport type {\n\tOEmbedExtra,\n\tPhotoOEmbed,\n\tVideoOEmbed,\n\tLinkOEmbed,\n\tRichOEmbed,\n\tAnyOEmbed,\n\tEmbedField,\n} from \"./types/value/embed\";\n\nexport type { BooleanField } from \"./types/value/boolean\";\nexport type { ColorField } from \"./types/value/color\";\nexport type { DateField } from \"./types/value/date\";\nexport type { KeyTextField } from \"./types/value/keyText\";\nexport type { NumberField } from \"./types/value/number\";\nexport type { SelectField } from \"./types/value/select\";\nexport type { TimestampField } from \"./types/value/timestamp\";\nexport type { GeoPointField } from \"./types/value/geoPoint\";\n\n/**\n * @deprecated Renamed to `IntegrationField`\n */\n// TODO: Remove when we remove support for deprecated `IntegrationFields` export.\ntype IntegrationFields = IntegrationField;\nexport { IntegrationField, IntegrationFields };\n\nexport type { GroupField, NestedGroupField } from \"./types/value/group\";\n\nexport type { SliceZone } from \"./types/value/sliceZone\";\nexport type { Slice } from \"./types/value/slice\";\nexport type { SharedSlice } from \"./types/value/sharedSlice\";\nexport type { SharedSliceVariation } from \"./types/value/sharedSliceVariation\";\n\nexport type {\n\tFieldState,\n\tAnyRegularField,\n\tAnySlicePrimaryField,\n} from \"./types/value/types\";\n\n// Models - Types representing Prismic content models.\nexport { CustomTypeModelFieldType } from \"./types/model/types\";\nexport { CustomTypeModelLinkSelectType } from \"./types/model/link\";\nexport { CustomTypeModelSliceType } from \"./types/model/sliceZone\";\nexport { CustomTypeModelSliceDisplay } from \"./types/model/slice\";\n\nexport type {\n\tCustomTypeModel,\n\tCustomTypeModelDefinition,\n\tCustomTypeModelTab,\n} from \"./types/model/customType\";\n\nexport type {\n\tCustomTypeModelRichTextField,\n\tCustomTypeModelRichTextMultiField,\n\tCustomTypeModelRichTextSingleField,\n} from \"./types/model/richText\";\nexport type { CustomTypeModelTitleField } from \"./types/model/title\";\n\nexport type {\n\tCustomTypeModelImageField,\n\tCustomTypeModelImageConstraint,\n\tCustomTypeModelImageThumbnail,\n} from \"./types/model/image\";\n\nexport type { CustomTypeModelContentRelationshipField } from \"./types/model/contentRelationship\";\nexport type { CustomTypeModelLinkField } from \"./types/model/link\";\nexport type { CustomTypeModelLinkToMediaField } from \"./types/model/linkToMedia\";\n\nexport type { CustomTypeModelEmbedField } from \"./types/model/embed\";\n\nexport type { CustomTypeModelBooleanField } from \"./types/model/boolean\";\nexport type { CustomTypeModelColorField } from \"./types/model/color\";\nexport type { CustomTypeModelDateField } from \"./types/model/date\";\nexport type { CustomTypeModelKeyTextField } from \"./types/model/keyText\";\nexport type { CustomTypeModelNumberField } from \"./types/model/number\";\nexport type { CustomTypeModelSelectField } from \"./types/model/select\";\nexport type { CustomTypeModelTimestampField } from \"./types/model/timestamp\";\nexport type { CustomTypeModelGeoPointField } from \"./types/model/geoPoint\";\n\n/**\n * @deprecated Renamed to `CustomTypeModelIntegrationField`.\n */\n// TODO: Remove when we remove support for deprecated `CustomTypeModelIntegrationField` export.\ntype CustomTypeModelIntegrationFieldsField = CustomTypeModelIntegrationField;\nexport {\n\tCustomTypeModelIntegrationField,\n\tCustomTypeModelIntegrationFieldsField,\n};\nexport type {\n\tCustomTypeModelGroupField,\n\tCustomTypeModelNestedGroupField,\n} from \"./types/model/group\";\nexport type {\n\tCustomTypeModelSliceZoneField,\n\tCustomTypeModelSliceLabel,\n\tCustomTypeModelSharedSlice,\n} from \"./types/model/sliceZone\";\nexport type {\n\tCustomTypeModelSlice,\n\tCustomTypeModelLegacySlice,\n} from \"./types/model/slice\";\nexport type { SharedSliceModel } from \"./types/model/sharedSlice\";\nexport type { SharedSliceModelVariation } from \"./types/model/sharedSliceVariation\";\n\nexport type { CustomTypeModelUIDField } from \"./types/model/uid\";\n\nexport type { CustomTypeModelRangeField } from \"./types/model/range\";\nexport type { CustomTypeModelSeparatorField } from \"./types/model/separator\";\n\nexport type {\n\tCustomTypeModelField,\n\tCustomTypeModelFieldForGroup,\n\tCustomTypeModelFieldForNestedGroup,\n\tCustomTypeModelFieldForSlicePrimary,\n} from \"./types/model/types\";\n\n// API - Types representing Prismic Rest API V2 responses.\nexport type { Query } from \"./types/api/query\";\n\nexport type { Ref } from \"./types/api/ref\";\n\nexport type { Release } from \"./types/api/release\";\n\nexport type {\n\tRepository,\n\tLanguage,\n\tForm,\n\tFormField,\n} from \"./types/api/repository\";\n\nexport type { Tags } from \"./types/api/tags\";\n\n// Webhook - Types representing Prismic webhooks.\nexport { WebhookType } from \"./types/webhook/types\";\n\nexport type { WebhookBody } from \"./types/webhook/types\";\n\nexport type { WebhookBodyAPIUpdate } from \"./types/webhook/apiUpdate\";\n\nexport type { WebhookBodyTestTrigger } from \"./types/webhook/testTrigger\";\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,MAAM,YAAY;AAyClB,MAAM,wBAAwB;AAgD9B,MAAM,UAAU;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["// Imports are used for deprecations.\nimport type { CustomTypeModelIntegrationField } from \"./types/model/integration\";\nimport type { IntegrationField } from \"./types/value/integration\";\nimport { RichTextNodeType } from \"./types/value/richText\";\n\nimport type {\n\tHTMLRichTextFunctionSerializer,\n\tHTMLRichTextMapSerializer,\n} from \"./helpers/asHTML\";\nimport { mapSliceZone } from \"./helpers/mapSliceZone\";\n\nimport { filter } from \"./filter\";\n\n//=============================================================================\n// Client - Query content from Prismic.\n//=============================================================================\n\n// Primary Client API.\nexport { createClient, Client } from \"./createClient\";\n\n// API endpoint helpers.\nexport { getRepositoryEndpoint } from \"./getRepositoryEndpoint\";\nexport { getRepositoryName } from \"./getRepositoryName\";\nexport { getGraphQLEndpoint } from \"./getGraphQLEndpoint\";\nexport { isRepositoryName } from \"./isRepositoryName\";\nexport { isRepositoryEndpoint } from \"./isRepositoryEndpoint\";\nexport { buildQueryURL } from \"./buildQueryURL\";\n\n// Toolbar helpers.\nexport { getToolbarSrc } from \"./getToolbarSrc\";\n\n// Query filters API.\n/**\n * @deprecated Renamed to `filter`\n */\n// TODO: Remove when we remove support for deprecated `predicate` export.\nconst predicate = filter;\nexport { filter, predicate };\n\n// A collection of well-known cookie names shared between Prismic libraries and systems.\nexport * as cookie from \"./cookie\";\n\n// General types used to query content from Prismic. These are made public to allow users to better type their projects.\nexport type {\n\tAbortSignalLike,\n\tClientConfig,\n\tCreateClient,\n\tFetchLike,\n\tHttpRequestLike,\n\tRequestInitLike,\n\tResponseLike,\n} from \"./createClient\";\nexport type {\n\tBuildQueryURLArgs,\n\tOrdering,\n\tQueryParams,\n\tRoute,\n} from \"./buildQueryURL\";\n\n//=============================================================================\n// Helpers - Manipulate content from Prismic.\n//=============================================================================\n\n// Primary Helpers API.\nexport { asDate } from \"./helpers/asDate\";\nexport { asLink } from \"./helpers/asLink\";\nexport { asLinkAttrs } from \"./helpers/asLinkAttrs\";\nexport { asText } from \"./helpers/asText\";\nexport { asHTML } from \"./helpers/asHTML\";\nexport { asImageSrc } from \"./helpers/asImageSrc\";\nexport { asImageWidthSrcSet } from \"./helpers/asImageWidthSrcSet\";\nexport { asImagePixelDensitySrcSet } from \"./helpers/asImagePixelDensitySrcSet\";\nexport * as isFilled from \"./helpers/isFilled\";\n\n/**\n * @deprecated Renamed to `mapSliceZone`\n */\nconst unstable_mapSliceZone = mapSliceZone;\nexport { mapSliceZone, unstable_mapSliceZone };\n\n// Conversion helper.\nexport { documentToLinkField } from \"./helpers/documentToLinkField\";\nexport { unstable_htmlAsRichText } from \"./helpers/unstable_htmlAsRichText\";\nexport { unstable_markdownAsRichText } from \"./helpers/unstable_markdownAsRichText\";\n\nexport type { LinkResolverFunction } from \"./helpers/asLink\";\nexport type { AsLinkAttrsConfig } from \"./helpers/asLinkAttrs\";\nexport type { SliceMapper } from \"./helpers/mapSliceZone\";\n\n/**\n * @deprecated Renamed to `HTMLRichTextMapSerializer`\n */\ntype HTMLMapSerializer = HTMLRichTextMapSerializer;\n/**\n * @deprecated Renamed to `HTMLRichTextFunctionSerializer`\n */\ntype HTMLFunctionSerializer = HTMLRichTextFunctionSerializer;\nexport type {\n\tHTMLRichTextMapSerializer,\n\tHTMLRichTextFunctionSerializer,\n\tHTMLMapSerializer,\n\tHTMLFunctionSerializer,\n};\nexport type { HTMLRichTextSerializer } from \"./helpers/asHTML\";\n\nexport type { RichTextHTMLMapSerializer } from \"./helpers/unstable_htmlAsRichText\";\n\n//=============================================================================\n// Errors - Custom errors for Prismic APIs.\n//=============================================================================\n\n// Client Errors\nexport { PrismicError } from \"./errors/PrismicError\";\nexport { ForbiddenError } from \"./errors/ForbiddenError\";\nexport { NotFoundError } from \"./errors/NotFoundError\";\nexport { RefNotFoundError } from \"./errors/RefNotFoundError\";\nexport { RefExpiredError } from \"./errors/RefExpiredError\";\nexport { PreviewTokenExpiredError } from \"./errors/PreviewTokenExpired\";\nexport { ParsingError } from \"./errors/ParsingError\";\nexport { RepositoryNotFoundError } from \"./errors/RepositoryNotFoundError\";\n\n// Rich Text Errors\nexport { PrismicRichTextError } from \"./errors/PrismicRichTextError\";\nexport { PrismicRichTextSerializerError } from \"./errors/PrismicRichTextSerializerError\";\n\n//=============================================================================\n// Types - Types representing Prismic content, models, and API payloads.\n//=============================================================================\n\n// Values - Types representing Prismic content.\n/**\n * @deprecated Use {@link RichTextNodeType} instead.\n */\n// TODO: Remove in v8.\nconst Element = RichTextNodeType;\nexport { RichTextNodeType, Element };\nexport { LinkType } from \"./types/value/link\";\nexport { OEmbedType } from \"./types/value/embed\";\n\nexport type {\n\tPrismicDocument,\n\tPrismicDocumentWithUID,\n\tPrismicDocumentWithoutUID,\n\tPrismicDocumentHeader,\n\tAlternateLanguage,\n} from \"./types/value/document\";\n\nexport type {\n\t// RichText & Title\n\tRichTextField,\n\t// RichText & Title (block nodes)\n\tRTTextNodeBase,\n\tRTHeading1Node,\n\tRTHeading2Node,\n\tRTHeading3Node,\n\tRTHeading4Node,\n\tRTHeading5Node,\n\tRTHeading6Node,\n\tRTParagraphNode,\n\tRTPreformattedNode,\n\tRTListItemNode,\n\tRTOListItemNode,\n\t// RichText & Title (span nodes)\n\tRTSpanNodeBase,\n\tRTStrongNode,\n\tRTEmNode,\n\tRTLabelNode,\n\t// RichText & Title (media nodes)\n\tRTImageNode,\n\tRTEmbedNode,\n\t// RichText & Title (link nodes)\n\tRTLinkNode,\n\t// RichText & Title (serialization related nodes)\n\tRTListNode,\n\tRTOListNode,\n\tRTSpanNode,\n\t// RichText & Title (helpers)\n\tRTNode,\n\tRTTextNode,\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTAnyNode,\n\tRichTextNodeTypes,\n} from \"./types/value/richText\";\nexport type { TitleField } from \"./types/value/title\";\n\nexport type {\n\tImageField,\n\tImageFieldImage,\n\tFilledImageFieldImage,\n\tEmptyImageFieldImage,\n} from \"./types/value/image\";\n\nexport type {\n\tEmptyLinkField,\n\tLinkField,\n\tFilledLinkToWebField,\n} from \"./types/value/link\";\nexport type {\n\tContentRelationshipField,\n\tFilledContentRelationshipField,\n} from \"./types/value/contentRelationship\";\nexport type {\n\tLinkToMediaField,\n\tFilledLinkToMediaField,\n} from \"./types/value/linkToMedia\";\n\nexport type {\n\tOEmbedExtra,\n\tPhotoOEmbed,\n\tVideoOEmbed,\n\tLinkOEmbed,\n\tRichOEmbed,\n\tAnyOEmbed,\n\tEmbedField,\n} from \"./types/value/embed\";\n\nexport type { BooleanField } from \"./types/value/boolean\";\nexport type { ColorField } from \"./types/value/color\";\nexport type { DateField } from \"./types/value/date\";\nexport type { KeyTextField } from \"./types/value/keyText\";\nexport type { NumberField } from \"./types/value/number\";\nexport type { SelectField } from \"./types/value/select\";\nexport type { TimestampField } from \"./types/value/timestamp\";\nexport type { GeoPointField } from \"./types/value/geoPoint\";\n\n/**\n * @deprecated Renamed to `IntegrationField`\n */\n// TODO: Remove when we remove support for deprecated `IntegrationFields` export.\ntype IntegrationFields = IntegrationField;\nexport { IntegrationField, IntegrationFields };\n\nexport type { GroupField, NestedGroupField } from \"./types/value/group\";\n\nexport type { SliceZone } from \"./types/value/sliceZone\";\nexport type { Slice } from \"./types/value/slice\";\nexport type { SharedSlice } from \"./types/value/sharedSlice\";\nexport type { SharedSliceVariation } from \"./types/value/sharedSliceVariation\";\n\nexport type {\n\tFieldState,\n\tAnyRegularField,\n\tAnySlicePrimaryField,\n} from \"./types/value/types\";\n\n// Models - Types representing Prismic content models.\nexport { CustomTypeModelFieldType } from \"./types/model/types\";\nexport { CustomTypeModelLinkSelectType } from \"./types/model/link\";\nexport { CustomTypeModelSliceType } from \"./types/model/sliceZone\";\nexport { CustomTypeModelSliceDisplay } from \"./types/model/slice\";\n\nexport type {\n\tCustomTypeModel,\n\tCustomTypeModelDefinition,\n\tCustomTypeModelTab,\n} from \"./types/model/customType\";\n\nexport type {\n\tCustomTypeModelRichTextField,\n\tCustomTypeModelRichTextMultiField,\n\tCustomTypeModelRichTextSingleField,\n} from \"./types/model/richText\";\nexport type { CustomTypeModelTitleField } from \"./types/model/title\";\n\nexport type {\n\tCustomTypeModelImageField,\n\tCustomTypeModelImageConstraint,\n\tCustomTypeModelImageThumbnail,\n} from \"./types/model/image\";\n\nexport type { CustomTypeModelContentRelationshipField } from \"./types/model/contentRelationship\";\nexport type { CustomTypeModelLinkField } from \"./types/model/link\";\nexport type { CustomTypeModelLinkToMediaField } from \"./types/model/linkToMedia\";\n\nexport type { CustomTypeModelEmbedField } from \"./types/model/embed\";\n\nexport type { CustomTypeModelBooleanField } from \"./types/model/boolean\";\nexport type { CustomTypeModelColorField } from \"./types/model/color\";\nexport type { CustomTypeModelDateField } from \"./types/model/date\";\nexport type { CustomTypeModelKeyTextField } from \"./types/model/keyText\";\nexport type { CustomTypeModelNumberField } from \"./types/model/number\";\nexport type { CustomTypeModelSelectField } from \"./types/model/select\";\nexport type { CustomTypeModelTimestampField } from \"./types/model/timestamp\";\nexport type { CustomTypeModelGeoPointField } from \"./types/model/geoPoint\";\n\n/**\n * @deprecated Renamed to `CustomTypeModelIntegrationField`.\n */\n// TODO: Remove when we remove support for deprecated `CustomTypeModelIntegrationField` export.\ntype CustomTypeModelIntegrationFieldsField = CustomTypeModelIntegrationField;\nexport {\n\tCustomTypeModelIntegrationField,\n\tCustomTypeModelIntegrationFieldsField,\n};\nexport type {\n\tCustomTypeModelGroupField,\n\tCustomTypeModelNestedGroupField,\n} from \"./types/model/group\";\nexport type {\n\tCustomTypeModelSliceZoneField,\n\tCustomTypeModelSliceLabel,\n\tCustomTypeModelSharedSlice,\n} from \"./types/model/sliceZone\";\nexport type {\n\tCustomTypeModelSlice,\n\tCustomTypeModelLegacySlice,\n} from \"./types/model/slice\";\nexport type { SharedSliceModel } from \"./types/model/sharedSlice\";\nexport type { SharedSliceModelVariation } from \"./types/model/sharedSliceVariation\";\n\nexport type { CustomTypeModelUIDField } from \"./types/model/uid\";\n\nexport type { CustomTypeModelRangeField } from \"./types/model/range\";\nexport type { CustomTypeModelSeparatorField } from \"./types/model/separator\";\n\nexport type {\n\tCustomTypeModelField,\n\tCustomTypeModelFieldForGroup,\n\tCustomTypeModelFieldForNestedGroup,\n\tCustomTypeModelFieldForSlicePrimary,\n} from \"./types/model/types\";\n\n// API - Types representing Prismic Rest API V2 responses.\nexport type { Query } from \"./types/api/query\";\n\nexport type { Ref } from \"./types/api/ref\";\n\nexport type { Release } from \"./types/api/release\";\n\nexport type {\n\tRepository,\n\tLanguage,\n\tForm,\n\tFormField,\n} from \"./types/api/repository\";\n\nexport type { Tags } from \"./types/api/tags\";\n\n// Webhook - Types representing Prismic webhooks.\nexport { WebhookType } from \"./types/webhook/types\";\n\nexport type { WebhookBody } from \"./types/webhook/types\";\n\nexport type { WebhookBodyAPIUpdate } from \"./types/webhook/apiUpdate\";\n\nexport type { WebhookBodyTestTrigger } from \"./types/webhook/testTrigger\";\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,MAAM,YAAY;AAyClB,MAAM,wBAAwB;AAyD9B,MAAM,UAAU;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isRepositoryEndpoint.cjs","sources":["../../src/isRepositoryEndpoint.ts"],"sourcesContent":["/**\n * Determines if a string if a Prismic Rest API V2 endpoint. Note that any valid\n * URL is a valid endpoint to support network proxies.\n *\n * @param input - Input to test.\n *\n * @returns `true` if `input` is a valid Prismic Rest API V2 endpoint, `false`\n * otherwise.\n */\nexport const isRepositoryEndpoint = (input: string): boolean => {\n\ttry {\n\t\tnew URL(input);\n\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n"],"names":[],"mappings":";;AASa,MAAA,uBAAuB,CAAC,UAA0B;AAC1D,MAAA;AACH,QAAI,IAAI,KAAK;AAEN,WAAA;AAAA,EAAA,QACA;AACA,WAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"isRepositoryEndpoint.cjs","sources":["../../src/isRepositoryEndpoint.ts"],"sourcesContent":["/**\n * Determines if a string if a Prismic Rest API V2 endpoint. Note that any valid\n * URL is a valid endpoint to support network proxies.\n *\n * @param input - Input to test.\n *\n * @returns `true` if `input` is a valid Prismic Rest API V2 endpoint, `false`\n * otherwise.\n */\nexport const isRepositoryEndpoint = (input: string): boolean => {\n\ttry {\n\t\tnew URL(input);\n\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n"],"names":[],"mappings":";;AASa,MAAA,uBAAuB,CAAC,UAA0B;AAC1D,MAAA;AACH,QAAI,IAAI,KAAK;AAEN,WAAA;AAAA,EAAA,QACA;AACA,WAAA;AAAA,EACR;AACD;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isRepositoryEndpoint.js","sources":["../../src/isRepositoryEndpoint.ts"],"sourcesContent":["/**\n * Determines if a string if a Prismic Rest API V2 endpoint. Note that any valid\n * URL is a valid endpoint to support network proxies.\n *\n * @param input - Input to test.\n *\n * @returns `true` if `input` is a valid Prismic Rest API V2 endpoint, `false`\n * otherwise.\n */\nexport const isRepositoryEndpoint = (input: string): boolean => {\n\ttry {\n\t\tnew URL(input);\n\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n"],"names":[],"mappings":"AASa,MAAA,uBAAuB,CAAC,UAA0B;AAC1D,MAAA;AACH,QAAI,IAAI,KAAK;AAEN,WAAA;AAAA,EAAA,QACA;AACA,WAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"isRepositoryEndpoint.js","sources":["../../src/isRepositoryEndpoint.ts"],"sourcesContent":["/**\n * Determines if a string if a Prismic Rest API V2 endpoint. Note that any valid\n * URL is a valid endpoint to support network proxies.\n *\n * @param input - Input to test.\n *\n * @returns `true` if `input` is a valid Prismic Rest API V2 endpoint, `false`\n * otherwise.\n */\nexport const isRepositoryEndpoint = (input: string): boolean => {\n\ttry {\n\t\tnew URL(input);\n\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n"],"names":[],"mappings":"AASa,MAAA,uBAAuB,CAAC,UAA0B;AAC1D,MAAA;AACH,QAAI,IAAI,KAAK;AAEN,WAAA;AAAA,EAAA,QACA;AACA,WAAA;AAAA,EACR;AACD;"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __typeError = (msg) => {
|
|
3
|
+
throw TypeError(msg);
|
|
4
|
+
};
|
|
5
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
+
var _nodes, _RichTextFieldBuilder_instances, last_get;
|
|
9
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
const PrismicRichTextError = require("../errors/PrismicRichTextError.cjs");
|
|
11
|
+
class RichTextFieldBuilder {
|
|
12
|
+
constructor() {
|
|
13
|
+
__privateAdd(this, _RichTextFieldBuilder_instances);
|
|
14
|
+
__privateAdd(this, _nodes, []);
|
|
15
|
+
}
|
|
16
|
+
appendNode(node) {
|
|
17
|
+
this.cleanupLast();
|
|
18
|
+
__privateGet(this, _nodes).push(node);
|
|
19
|
+
}
|
|
20
|
+
appendTextNode(type, direction = "ltr") {
|
|
21
|
+
this.appendNode({
|
|
22
|
+
type,
|
|
23
|
+
text: "",
|
|
24
|
+
spans: [],
|
|
25
|
+
direction
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
appendSpan(spanOrPartialSpan, length) {
|
|
29
|
+
var _a;
|
|
30
|
+
if (!this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
31
|
+
throw new PrismicRichTextError.PrismicRichTextError("Cannot add span to non-text last node");
|
|
32
|
+
}
|
|
33
|
+
let span;
|
|
34
|
+
if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) {
|
|
35
|
+
span = spanOrPartialSpan;
|
|
36
|
+
} else {
|
|
37
|
+
const lastLength = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.length;
|
|
38
|
+
span = {
|
|
39
|
+
...spanOrPartialSpan,
|
|
40
|
+
start: lastLength,
|
|
41
|
+
end: lastLength + length
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
let lastIdenticalSpanIndex = -1;
|
|
45
|
+
for (let i = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans.length - 1; i >= 0; i--) {
|
|
46
|
+
const lastSpan = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[i];
|
|
47
|
+
if (span.type === "strong" || span.type === "em") {
|
|
48
|
+
if (lastSpan.type === span.type) {
|
|
49
|
+
lastIdenticalSpanIndex = i;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
} else if (span.type === "label") {
|
|
53
|
+
if (lastSpan.type === "label" && lastSpan.data.label === span.data.label) {
|
|
54
|
+
lastIdenticalSpanIndex = i;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
} else if (span.type === "hyperlink") {
|
|
58
|
+
if (lastSpan.type === "hyperlink") {
|
|
59
|
+
const isSameLink = Object.entries(span.data).every(([key, value]) => lastSpan.data[key] === value);
|
|
60
|
+
if (isSameLink) {
|
|
61
|
+
lastIdenticalSpanIndex = i;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (((_a = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex]) == null ? void 0 : _a.end) === span.start) {
|
|
68
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex].end = span.end;
|
|
69
|
+
} else {
|
|
70
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).spans.push(span);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
appendText(text) {
|
|
74
|
+
if (!this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
75
|
+
throw new PrismicRichTextError.PrismicRichTextError("Cannot append text to non-text last node");
|
|
76
|
+
}
|
|
77
|
+
if (__privateGet(this, _RichTextFieldBuilder_instances, last_get).text) {
|
|
78
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text += text;
|
|
79
|
+
} else {
|
|
80
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = text.trimStart();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
build() {
|
|
84
|
+
this.cleanupLast();
|
|
85
|
+
return __privateGet(this, _nodes);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Cleans up the current last node.
|
|
89
|
+
*/
|
|
90
|
+
cleanupLast() {
|
|
91
|
+
if (this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
92
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.trimEnd();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Determines if a node is a text node.
|
|
97
|
+
*
|
|
98
|
+
* @param node - rich text node to check.
|
|
99
|
+
*
|
|
100
|
+
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
101
|
+
*/
|
|
102
|
+
isTextNode(node) {
|
|
103
|
+
return !!node && node.type !== "image" && node.type !== "embed";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
_nodes = new WeakMap();
|
|
107
|
+
_RichTextFieldBuilder_instances = new WeakSet();
|
|
108
|
+
last_get = function() {
|
|
109
|
+
return __privateGet(this, _nodes)[__privateGet(this, _nodes).length - 1];
|
|
110
|
+
};
|
|
111
|
+
exports.RichTextFieldBuilder = RichTextFieldBuilder;
|
|
112
|
+
//# sourceMappingURL=RichTextFieldBuilder.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.cjs","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import {\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\";\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\";\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;\n\n/**\n * An inline node without its `start` and `end` properties.\n */\nexport type RTPartialInlineNode = DistributedOmit<\n\tRTInlineNode,\n\t\"start\" | \"end\"\n>;\n\nexport class RichTextFieldBuilder {\n\t#nodes: RTNode[] = [];\n\n\tget #last(): RTNode | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1];\n\t}\n\n\tappendNode(node: RTNode): void {\n\t\tthis.cleanupLast();\n\n\t\tthis.#nodes.push(node);\n\t}\n\n\tappendTextNode(\n\t\ttype: RTTextNode[\"type\"],\n\t\tdirection: \"ltr\" | \"rtl\" = \"ltr\",\n\t): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t});\n\t}\n\n\tappendSpan(span: RTInlineNode): void;\n\tappendSpan(partialSpan: RTPartialInlineNode, length: number): void;\n\tappendSpan(\n\t\tspanOrPartialSpan: RTInlineNode | RTPartialInlineNode,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\");\n\t\t}\n\n\t\tlet span: RTInlineNode;\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan;\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length;\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t};\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1;\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i];\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (\n\t\t\t\t\tlastSpan.type === \"label\" &&\n\t\t\t\t\tlastSpan.data.label === span.data.label\n\t\t\t\t) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink = (\n\t\t\t\t\t\tObject.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]\n\t\t\t\t\t).every(([key, value]) => lastSpan.data[key] === value);\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end;\n\t\t} else {\n\t\t\tthis.#last.spans.push(span);\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\n\t\t\t\t\"Cannot append text to non-text last node\",\n\t\t\t);\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text;\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart();\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast();\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField;\n\t}\n\n\t/**\n\t * Cleans up the current last node.\n\t */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd();\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - rich text node to check.\n\t *\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(node?: RTNode): node is RTTextNode {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\";\n\t}\n}\n"],"names":["PrismicRichTextError"],"mappings":";;;;;;;;;;MA8Ba,qBAAoB;AAAA;;AAChC,+BAAmB,CAAA;AAAA;AAAA,EAMnB,WAAW,MAAY;AACtB,SAAK,YAAW;AAEX,uBAAA,QAAO,KAAK,IAAI;AAAA,EACtB;AAAA,EAEA,eACC,MACA,YAA2B,OAAK;AAEhC,SAAK,WAAW;AAAA,MACf;AAAA,MACA,MAAM;AAAA,MACN,OAAO,CAAE;AAAA,MACT;AAAA,IAAA,CACA;AAAA,EACF;AAAA,EAIA,WACC,mBACA,QAAe;;AAEf,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAIA,qBAAAA,qBAAqB,uCAAuC;AAAA,IACvE;AAEI,QAAA;AACA,QAAA,WAAW,qBAAqB,SAAS,mBAAmB;AACxD,aAAA;AAAA,IAAA,OACD;AACA,YAAA,aAAa,mBAAK,2CAAM,KAAK;AAE5B,aAAA;AAAA,QACN,GAAG;AAAA,QACH,OAAO;AAAA,QACP,KAAK,aAAa;AAAA,MAAA;AAAA,IAEpB;AAEA,QAAI,yBAAyB;AACpB,aAAA,IAAI,mBAAK,2CAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;AACtD,YAAM,WAAW,mBAAK,2CAAM,MAAM,CAAC;AAEnC,UAAI,KAAK,SAAS,YAAY,KAAK,SAAS,MAAM;AAC7C,YAAA,SAAS,SAAS,KAAK,MAAM;AACP,mCAAA;AACzB;AAAA,QACD;AAAA,MAAA,WACU,KAAK,SAAS,SAAS;AAEhC,YAAA,SAAS,SAAS,WAClB,SAAS,KAAK,UAAU,KAAK,KAAK,OACjC;AACwB,mCAAA;AACzB;AAAA,QACD;AAAA,MAAA,WACU,KAAK,SAAS,aAAa;AACjC,YAAA,SAAS,SAAS,aAAa;AAIlC,gBAAM,aACL,OAAO,QAAQ,KAAK,IAAI,EACvB,MAAM,CAAC,CAAC,KAAK,KAAK,MAAM,SAAS,KAAK,GAAG,MAAM,KAAK;AAEtD,cAAI,YAAY;AACU,qCAAA;AACzB;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAIA,UAAI,wBAAK,2CAAM,MAAM,sBAAsB,MAAvC,mBAA0C,SAAQ,KAAK,OAAO;AACjE,yBAAK,2CAAM,MAAM,sBAAsB,EAAE,MAAM,KAAK;AAAA,IAAA,OAC9C;AACD,yBAAA,2CAAM,MAAM,KAAK,IAAI;AAAA,IAC3B;AAAA,EACD;AAAA,EAEA,WAAW,MAAY;AACtB,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAIA,qBAAAA,qBACT,0CAA0C;AAAA,IAE5C;AAEI,QAAA,mBAAK,2CAAM,MAAM;AACpB,yBAAK,2CAAM,QAAQ;AAAA,IAAA,OACb;AACD,yBAAA,2CAAM,OAAO,KAAK,UAAS;AAAA,IACjC;AAAA,EACD;AAAA,EAEA,QAAK;AAEJ,SAAK,YAAW;AAIhB,WAAO,mBAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKQ,cAAW;AAClB,QAAI,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAChC,yBAAK,2CAAM,OAAO,mBAAK,2CAAM,KAAK;IACnC;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,WAAW,MAAa;AAC/B,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AApIA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { RTInlineNode, RTNode, RTTextNode, RichTextField } from "../types/value/richText";
|
|
2
|
+
/**
|
|
3
|
+
* Omits keys from a type, distributing the operation over a union.
|
|
4
|
+
*
|
|
5
|
+
* Taken from the `type-fest` package.
|
|
6
|
+
*
|
|
7
|
+
* @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts
|
|
8
|
+
*/
|
|
9
|
+
type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
|
|
10
|
+
/**
|
|
11
|
+
* An inline node without its `start` and `end` properties.
|
|
12
|
+
*/
|
|
13
|
+
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
14
|
+
export declare class RichTextFieldBuilder {
|
|
15
|
+
#private;
|
|
16
|
+
appendNode(node: RTNode): void;
|
|
17
|
+
appendTextNode(type: RTTextNode["type"], direction?: "ltr" | "rtl"): void;
|
|
18
|
+
appendSpan(span: RTInlineNode): void;
|
|
19
|
+
appendSpan(partialSpan: RTPartialInlineNode, length: number): void;
|
|
20
|
+
appendText(text: string): void;
|
|
21
|
+
build(): RichTextField;
|
|
22
|
+
/**
|
|
23
|
+
* Cleans up the current last node.
|
|
24
|
+
*/
|
|
25
|
+
private cleanupLast;
|
|
26
|
+
/**
|
|
27
|
+
* Determines if a node is a text node.
|
|
28
|
+
*
|
|
29
|
+
* @param node - rich text node to check.
|
|
30
|
+
*
|
|
31
|
+
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
32
|
+
*/
|
|
33
|
+
private isTextNode;
|
|
34
|
+
}
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
|
+
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
+
var _nodes, _RichTextFieldBuilder_instances, last_get;
|
|
8
|
+
import { PrismicRichTextError } from "../errors/PrismicRichTextError.js";
|
|
9
|
+
class RichTextFieldBuilder {
|
|
10
|
+
constructor() {
|
|
11
|
+
__privateAdd(this, _RichTextFieldBuilder_instances);
|
|
12
|
+
__privateAdd(this, _nodes, []);
|
|
13
|
+
}
|
|
14
|
+
appendNode(node) {
|
|
15
|
+
this.cleanupLast();
|
|
16
|
+
__privateGet(this, _nodes).push(node);
|
|
17
|
+
}
|
|
18
|
+
appendTextNode(type, direction = "ltr") {
|
|
19
|
+
this.appendNode({
|
|
20
|
+
type,
|
|
21
|
+
text: "",
|
|
22
|
+
spans: [],
|
|
23
|
+
direction
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
appendSpan(spanOrPartialSpan, length) {
|
|
27
|
+
var _a;
|
|
28
|
+
if (!this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
29
|
+
throw new PrismicRichTextError("Cannot add span to non-text last node");
|
|
30
|
+
}
|
|
31
|
+
let span;
|
|
32
|
+
if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) {
|
|
33
|
+
span = spanOrPartialSpan;
|
|
34
|
+
} else {
|
|
35
|
+
const lastLength = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.length;
|
|
36
|
+
span = {
|
|
37
|
+
...spanOrPartialSpan,
|
|
38
|
+
start: lastLength,
|
|
39
|
+
end: lastLength + length
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
let lastIdenticalSpanIndex = -1;
|
|
43
|
+
for (let i = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans.length - 1; i >= 0; i--) {
|
|
44
|
+
const lastSpan = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[i];
|
|
45
|
+
if (span.type === "strong" || span.type === "em") {
|
|
46
|
+
if (lastSpan.type === span.type) {
|
|
47
|
+
lastIdenticalSpanIndex = i;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
} else if (span.type === "label") {
|
|
51
|
+
if (lastSpan.type === "label" && lastSpan.data.label === span.data.label) {
|
|
52
|
+
lastIdenticalSpanIndex = i;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
} else if (span.type === "hyperlink") {
|
|
56
|
+
if (lastSpan.type === "hyperlink") {
|
|
57
|
+
const isSameLink = Object.entries(span.data).every(([key, value]) => lastSpan.data[key] === value);
|
|
58
|
+
if (isSameLink) {
|
|
59
|
+
lastIdenticalSpanIndex = i;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (((_a = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex]) == null ? void 0 : _a.end) === span.start) {
|
|
66
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex].end = span.end;
|
|
67
|
+
} else {
|
|
68
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).spans.push(span);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
appendText(text) {
|
|
72
|
+
if (!this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
73
|
+
throw new PrismicRichTextError("Cannot append text to non-text last node");
|
|
74
|
+
}
|
|
75
|
+
if (__privateGet(this, _RichTextFieldBuilder_instances, last_get).text) {
|
|
76
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text += text;
|
|
77
|
+
} else {
|
|
78
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = text.trimStart();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
build() {
|
|
82
|
+
this.cleanupLast();
|
|
83
|
+
return __privateGet(this, _nodes);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Cleans up the current last node.
|
|
87
|
+
*/
|
|
88
|
+
cleanupLast() {
|
|
89
|
+
if (this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
90
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.trimEnd();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Determines if a node is a text node.
|
|
95
|
+
*
|
|
96
|
+
* @param node - rich text node to check.
|
|
97
|
+
*
|
|
98
|
+
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
99
|
+
*/
|
|
100
|
+
isTextNode(node) {
|
|
101
|
+
return !!node && node.type !== "image" && node.type !== "embed";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
_nodes = new WeakMap();
|
|
105
|
+
_RichTextFieldBuilder_instances = new WeakSet();
|
|
106
|
+
last_get = function() {
|
|
107
|
+
return __privateGet(this, _nodes)[__privateGet(this, _nodes).length - 1];
|
|
108
|
+
};
|
|
109
|
+
export {
|
|
110
|
+
RichTextFieldBuilder
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=RichTextFieldBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.js","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import {\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\";\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\";\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;\n\n/**\n * An inline node without its `start` and `end` properties.\n */\nexport type RTPartialInlineNode = DistributedOmit<\n\tRTInlineNode,\n\t\"start\" | \"end\"\n>;\n\nexport class RichTextFieldBuilder {\n\t#nodes: RTNode[] = [];\n\n\tget #last(): RTNode | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1];\n\t}\n\n\tappendNode(node: RTNode): void {\n\t\tthis.cleanupLast();\n\n\t\tthis.#nodes.push(node);\n\t}\n\n\tappendTextNode(\n\t\ttype: RTTextNode[\"type\"],\n\t\tdirection: \"ltr\" | \"rtl\" = \"ltr\",\n\t): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t});\n\t}\n\n\tappendSpan(span: RTInlineNode): void;\n\tappendSpan(partialSpan: RTPartialInlineNode, length: number): void;\n\tappendSpan(\n\t\tspanOrPartialSpan: RTInlineNode | RTPartialInlineNode,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\");\n\t\t}\n\n\t\tlet span: RTInlineNode;\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan;\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length;\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t};\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1;\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i];\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (\n\t\t\t\t\tlastSpan.type === \"label\" &&\n\t\t\t\t\tlastSpan.data.label === span.data.label\n\t\t\t\t) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink = (\n\t\t\t\t\t\tObject.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]\n\t\t\t\t\t).every(([key, value]) => lastSpan.data[key] === value);\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end;\n\t\t} else {\n\t\t\tthis.#last.spans.push(span);\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\n\t\t\t\t\"Cannot append text to non-text last node\",\n\t\t\t);\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text;\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart();\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast();\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField;\n\t}\n\n\t/**\n\t * Cleans up the current last node.\n\t */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd();\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - rich text node to check.\n\t *\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(node?: RTNode): node is RTTextNode {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\";\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;MA8Ba,qBAAoB;AAAA;;AAChC,+BAAmB,CAAA;AAAA;AAAA,EAMnB,WAAW,MAAY;AACtB,SAAK,YAAW;AAEX,uBAAA,QAAO,KAAK,IAAI;AAAA,EACtB;AAAA,EAEA,eACC,MACA,YAA2B,OAAK;AAEhC,SAAK,WAAW;AAAA,MACf;AAAA,MACA,MAAM;AAAA,MACN,OAAO,CAAE;AAAA,MACT;AAAA,IAAA,CACA;AAAA,EACF;AAAA,EAIA,WACC,mBACA,QAAe;;AAEf,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAI,qBAAqB,uCAAuC;AAAA,IACvE;AAEI,QAAA;AACA,QAAA,WAAW,qBAAqB,SAAS,mBAAmB;AACxD,aAAA;AAAA,IAAA,OACD;AACA,YAAA,aAAa,mBAAK,2CAAM,KAAK;AAE5B,aAAA;AAAA,QACN,GAAG;AAAA,QACH,OAAO;AAAA,QACP,KAAK,aAAa;AAAA,MAAA;AAAA,IAEpB;AAEA,QAAI,yBAAyB;AACpB,aAAA,IAAI,mBAAK,2CAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;AACtD,YAAM,WAAW,mBAAK,2CAAM,MAAM,CAAC;AAEnC,UAAI,KAAK,SAAS,YAAY,KAAK,SAAS,MAAM;AAC7C,YAAA,SAAS,SAAS,KAAK,MAAM;AACP,mCAAA;AACzB;AAAA,QACD;AAAA,MAAA,WACU,KAAK,SAAS,SAAS;AAEhC,YAAA,SAAS,SAAS,WAClB,SAAS,KAAK,UAAU,KAAK,KAAK,OACjC;AACwB,mCAAA;AACzB;AAAA,QACD;AAAA,MAAA,WACU,KAAK,SAAS,aAAa;AACjC,YAAA,SAAS,SAAS,aAAa;AAIlC,gBAAM,aACL,OAAO,QAAQ,KAAK,IAAI,EACvB,MAAM,CAAC,CAAC,KAAK,KAAK,MAAM,SAAS,KAAK,GAAG,MAAM,KAAK;AAEtD,cAAI,YAAY;AACU,qCAAA;AACzB;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAIA,UAAI,wBAAK,2CAAM,MAAM,sBAAsB,MAAvC,mBAA0C,SAAQ,KAAK,OAAO;AACjE,yBAAK,2CAAM,MAAM,sBAAsB,EAAE,MAAM,KAAK;AAAA,IAAA,OAC9C;AACD,yBAAA,2CAAM,MAAM,KAAK,IAAI;AAAA,IAC3B;AAAA,EACD;AAAA,EAEA,WAAW,MAAY;AACtB,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAI,qBACT,0CAA0C;AAAA,IAE5C;AAEI,QAAA,mBAAK,2CAAM,MAAM;AACpB,yBAAK,2CAAM,QAAQ;AAAA,IAAA,OACb;AACD,yBAAA,2CAAM,OAAO,KAAK,UAAS;AAAA,IACjC;AAAA,EACD;AAAA,EAEA,QAAK;AAEJ,SAAK,YAAW;AAIhB,WAAO,mBAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKQ,cAAW;AAClB,QAAI,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAChC,yBAAK,2CAAM,OAAO,mBAAK,2CAAM,KAAK;IACnC;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,WAAW,MAAa;AAC/B,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AApIA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;"}
|
package/dist/lib/castThunk.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"escapeHTML.cjs","sources":["../../../src/lib/escapeHTML.ts"],"sourcesContent":["/*\n ** Core logic from https://github.com/component/escape-html\n ** Many thanks to @component\n */\n\nconst matchHtmlRegExp = /[\"'&<>]/;\n\nexport const escapeHTML = (string?: string | null): string => {\n\tconst str = \"\" + string;\n\tconst match = matchHtmlRegExp.exec(str);\n\n\tif (!match) {\n\t\treturn str;\n\t}\n\n\tlet escape;\n\tlet html = \"\";\n\tlet index = 0;\n\tlet lastIndex = 0;\n\n\tfor (index = match.index; index < str.length; index++) {\n\t\tswitch (str.charCodeAt(index)) {\n\t\t\tcase 34: // \"\n\t\t\t\tescape = \""\";\n\t\t\t\tbreak;\n\t\t\tcase 38: // &\n\t\t\t\tescape = \"&\";\n\t\t\t\tbreak;\n\t\t\tcase 39: // '\n\t\t\t\tescape = \"'\";\n\t\t\t\tbreak;\n\t\t\tcase 60: // <\n\t\t\t\tescape = \"<\";\n\t\t\t\tbreak;\n\t\t\tcase 62: // >\n\t\t\t\tescape = \">\";\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tcontinue;\n\t\t}\n\n\t\tif (lastIndex !== index) {\n\t\t\thtml += str.substring(lastIndex, index);\n\t\t}\n\n\t\tlastIndex = index + 1;\n\t\thtml += escape;\n\t}\n\n\treturn lastIndex !== index ? html + str.substring(lastIndex, index) : html;\n};\n"],"names":[],"mappings":";;AAKA,MAAM,kBAAkB;AAEX,MAAA,aAAa,CAAC,WAAkC;AAC5D,QAAM,MAAM,KAAK;AACX,QAAA,QAAQ,gBAAgB,KAAK,GAAG;AAEtC,MAAI,CAAC,OAAO;AACJ,WAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"escapeHTML.cjs","sources":["../../../src/lib/escapeHTML.ts"],"sourcesContent":["/*\n ** Core logic from https://github.com/component/escape-html\n ** Many thanks to @component\n */\n\nconst matchHtmlRegExp = /[\"'&<>]/;\n\nexport const escapeHTML = (string?: string | null): string => {\n\tconst str = \"\" + string;\n\tconst match = matchHtmlRegExp.exec(str);\n\n\tif (!match) {\n\t\treturn str;\n\t}\n\n\tlet escape;\n\tlet html = \"\";\n\tlet index = 0;\n\tlet lastIndex = 0;\n\n\tfor (index = match.index; index < str.length; index++) {\n\t\tswitch (str.charCodeAt(index)) {\n\t\t\tcase 34: // \"\n\t\t\t\tescape = \""\";\n\t\t\t\tbreak;\n\t\t\tcase 38: // &\n\t\t\t\tescape = \"&\";\n\t\t\t\tbreak;\n\t\t\tcase 39: // '\n\t\t\t\tescape = \"'\";\n\t\t\t\tbreak;\n\t\t\tcase 60: // <\n\t\t\t\tescape = \"<\";\n\t\t\t\tbreak;\n\t\t\tcase 62: // >\n\t\t\t\tescape = \">\";\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tcontinue;\n\t\t}\n\n\t\tif (lastIndex !== index) {\n\t\t\thtml += str.substring(lastIndex, index);\n\t\t}\n\n\t\tlastIndex = index + 1;\n\t\thtml += escape;\n\t}\n\n\treturn lastIndex !== index ? html + str.substring(lastIndex, index) : html;\n};\n"],"names":[],"mappings":";;AAKA,MAAM,kBAAkB;AAEX,MAAA,aAAa,CAAC,WAAkC;AAC5D,QAAM,MAAM,KAAK;AACX,QAAA,QAAQ,gBAAgB,KAAK,GAAG;AAEtC,MAAI,CAAC,OAAO;AACJ,WAAA;AAAA,EACR;AAEI,MAAA;AACJ,MAAI,OAAO;AACX,MAAI,QAAQ;AACZ,MAAI,YAAY;AAEhB,OAAK,QAAQ,MAAM,OAAO,QAAQ,IAAI,QAAQ,SAAS;AAC9C,YAAA,IAAI,WAAW,KAAK,GAAG;AAAA,MAC9B,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD;AACC;AAAA,IACF;AAEA,QAAI,cAAc,OAAO;AAChB,cAAA,IAAI,UAAU,WAAW,KAAK;AAAA,IACvC;AAEA,gBAAY,QAAQ;AACZ,YAAA;AAAA,EACT;AAEA,SAAO,cAAc,QAAQ,OAAO,IAAI,UAAU,WAAW,KAAK,IAAI;AACvE;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"escapeHTML.js","sources":["../../../src/lib/escapeHTML.ts"],"sourcesContent":["/*\n ** Core logic from https://github.com/component/escape-html\n ** Many thanks to @component\n */\n\nconst matchHtmlRegExp = /[\"'&<>]/;\n\nexport const escapeHTML = (string?: string | null): string => {\n\tconst str = \"\" + string;\n\tconst match = matchHtmlRegExp.exec(str);\n\n\tif (!match) {\n\t\treturn str;\n\t}\n\n\tlet escape;\n\tlet html = \"\";\n\tlet index = 0;\n\tlet lastIndex = 0;\n\n\tfor (index = match.index; index < str.length; index++) {\n\t\tswitch (str.charCodeAt(index)) {\n\t\t\tcase 34: // \"\n\t\t\t\tescape = \""\";\n\t\t\t\tbreak;\n\t\t\tcase 38: // &\n\t\t\t\tescape = \"&\";\n\t\t\t\tbreak;\n\t\t\tcase 39: // '\n\t\t\t\tescape = \"'\";\n\t\t\t\tbreak;\n\t\t\tcase 60: // <\n\t\t\t\tescape = \"<\";\n\t\t\t\tbreak;\n\t\t\tcase 62: // >\n\t\t\t\tescape = \">\";\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tcontinue;\n\t\t}\n\n\t\tif (lastIndex !== index) {\n\t\t\thtml += str.substring(lastIndex, index);\n\t\t}\n\n\t\tlastIndex = index + 1;\n\t\thtml += escape;\n\t}\n\n\treturn lastIndex !== index ? html + str.substring(lastIndex, index) : html;\n};\n"],"names":[],"mappings":"AAKA,MAAM,kBAAkB;AAEX,MAAA,aAAa,CAAC,WAAkC;AAC5D,QAAM,MAAM,KAAK;AACX,QAAA,QAAQ,gBAAgB,KAAK,GAAG;AAEtC,MAAI,CAAC,OAAO;AACJ,WAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"escapeHTML.js","sources":["../../../src/lib/escapeHTML.ts"],"sourcesContent":["/*\n ** Core logic from https://github.com/component/escape-html\n ** Many thanks to @component\n */\n\nconst matchHtmlRegExp = /[\"'&<>]/;\n\nexport const escapeHTML = (string?: string | null): string => {\n\tconst str = \"\" + string;\n\tconst match = matchHtmlRegExp.exec(str);\n\n\tif (!match) {\n\t\treturn str;\n\t}\n\n\tlet escape;\n\tlet html = \"\";\n\tlet index = 0;\n\tlet lastIndex = 0;\n\n\tfor (index = match.index; index < str.length; index++) {\n\t\tswitch (str.charCodeAt(index)) {\n\t\t\tcase 34: // \"\n\t\t\t\tescape = \""\";\n\t\t\t\tbreak;\n\t\t\tcase 38: // &\n\t\t\t\tescape = \"&\";\n\t\t\t\tbreak;\n\t\t\tcase 39: // '\n\t\t\t\tescape = \"'\";\n\t\t\t\tbreak;\n\t\t\tcase 60: // <\n\t\t\t\tescape = \"<\";\n\t\t\t\tbreak;\n\t\t\tcase 62: // >\n\t\t\t\tescape = \">\";\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tcontinue;\n\t\t}\n\n\t\tif (lastIndex !== index) {\n\t\t\thtml += str.substring(lastIndex, index);\n\t\t}\n\n\t\tlastIndex = index + 1;\n\t\thtml += escape;\n\t}\n\n\treturn lastIndex !== index ? html + str.substring(lastIndex, index) : html;\n};\n"],"names":[],"mappings":"AAKA,MAAM,kBAAkB;AAEX,MAAA,aAAa,CAAC,WAAkC;AAC5D,QAAM,MAAM,KAAK;AACX,QAAA,QAAQ,gBAAgB,KAAK,GAAG;AAEtC,MAAI,CAAC,OAAO;AACJ,WAAA;AAAA,EACR;AAEI,MAAA;AACJ,MAAI,OAAO;AACX,MAAI,QAAQ;AACZ,MAAI,YAAY;AAEhB,OAAK,QAAQ,MAAM,OAAO,QAAQ,IAAI,QAAQ,SAAS;AAC9C,YAAA,IAAI,WAAW,KAAK,GAAG;AAAA,MAC9B,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD,KAAK;AACK,iBAAA;AACT;AAAA,MACD;AACC;AAAA,IACF;AAEA,QAAI,cAAc,OAAO;AAChB,cAAA,IAAI,UAAU,WAAW,KAAK;AAAA,IACvC;AAEA,gBAAY,QAAQ;AACZ,YAAA;AAAA,EACT;AAEA,SAAO,cAAc,QAAQ,OAAO,IAAI,UAAU,WAAW,KAAK,IAAI;AACvE;"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const link = require("../types/value/link.cjs");
|
|
4
|
+
const richText = require("../types/value/richText.cjs");
|
|
5
|
+
const getAllowedNodeTypes = (model) => {
|
|
6
|
+
var _a;
|
|
7
|
+
const allowedNodeTypes = [];
|
|
8
|
+
if (model.config) {
|
|
9
|
+
if ("multi" in model.config && model.config.multi) {
|
|
10
|
+
allowedNodeTypes.push(...model.config.multi.split(","));
|
|
11
|
+
} else if ("single" in model.config && model.config.single) {
|
|
12
|
+
allowedNodeTypes.push(...model.config.single.split(","));
|
|
13
|
+
}
|
|
14
|
+
if ((_a = model.config.labels) == null ? void 0 : _a.length) {
|
|
15
|
+
allowedNodeTypes.push("label");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return allowedNodeTypes;
|
|
19
|
+
};
|
|
20
|
+
const filterImageNode = (node, model) => {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && ((_b = node.linkTo) == null ? void 0 : _b.link_type) === link.LinkType.Web) {
|
|
23
|
+
return {
|
|
24
|
+
...node,
|
|
25
|
+
linkTo: {
|
|
26
|
+
...node.linkTo,
|
|
27
|
+
target: void 0
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return node;
|
|
32
|
+
};
|
|
33
|
+
const filterTextNode = (node, model, allowedNodeTypes) => {
|
|
34
|
+
var _a, _b, _c;
|
|
35
|
+
const filteredNode = {
|
|
36
|
+
...node,
|
|
37
|
+
spans: []
|
|
38
|
+
};
|
|
39
|
+
for (let i = 0; i < node.spans.length; i++) {
|
|
40
|
+
const span = node.spans[i];
|
|
41
|
+
if (allowedNodeTypes.includes(span.type)) {
|
|
42
|
+
if (span.type === richText.RichTextNodeType.hyperlink) {
|
|
43
|
+
if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && span.data.link_type === link.LinkType.Web) {
|
|
44
|
+
filteredNode.spans.push({
|
|
45
|
+
...span,
|
|
46
|
+
data: {
|
|
47
|
+
...span.data,
|
|
48
|
+
target: void 0
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
filteredNode.spans.push(span);
|
|
53
|
+
}
|
|
54
|
+
} else if (span.type === richText.RichTextNodeType.label) {
|
|
55
|
+
if ((_c = (_b = model.config) == null ? void 0 : _b.labels) == null ? void 0 : _c.includes(span.data.label)) {
|
|
56
|
+
filteredNode.spans.push(span);
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
filteredNode.spans.push(span);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return filteredNode;
|
|
64
|
+
};
|
|
65
|
+
const filterRichTextField = (richTextField, model) => {
|
|
66
|
+
const nodes = [];
|
|
67
|
+
const allowedNodeTypes = getAllowedNodeTypes(model);
|
|
68
|
+
for (
|
|
69
|
+
let i = 0;
|
|
70
|
+
// Only process the first node if it's a "single" node rich text type
|
|
71
|
+
i < (model.config && "multi" in model.config ? richTextField.length : 1);
|
|
72
|
+
i++
|
|
73
|
+
) {
|
|
74
|
+
const node = richTextField[i];
|
|
75
|
+
if (allowedNodeTypes.includes(node.type)) {
|
|
76
|
+
if (node.type === richText.RichTextNodeType.image) {
|
|
77
|
+
nodes.push(filterImageNode(node, model));
|
|
78
|
+
} else if (node.type === richText.RichTextNodeType.embed) {
|
|
79
|
+
nodes.push(node);
|
|
80
|
+
} else {
|
|
81
|
+
nodes.push(filterTextNode(node, model, allowedNodeTypes));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return nodes;
|
|
86
|
+
};
|
|
87
|
+
exports.filterRichTextField = filterRichTextField;
|
|
88
|
+
//# sourceMappingURL=filterRichTextField.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filterRichTextField.cjs","sources":["../../../src/lib/filterRichTextField.ts"],"sourcesContent":["import { CustomTypeModelRichTextField } from \"../types/model/richText\";\nimport { LinkType } from \"../types/value/link\";\nimport {\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeType,\n\tRichTextNodeTypes,\n} from \"../types/value/richText\";\nimport { TitleField } from \"../types/value/title\";\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n *\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (\n\tmodel: CustomTypeModelRichTextField,\n): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = []; // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.multi.split(\",\") as RichTextNodeTypes[]),\n\t\t\t);\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.single.split(\",\") as RichTextNodeTypes[]),\n\t\t\t);\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\");\n\t\t}\n\t}\n\n\treturn allowedNodeTypes;\n};\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (\n\tnode: RTImageNode,\n\tmodel: CustomTypeModelRichTextField,\n): RTImageNode => {\n\tif (\n\t\t!model.config?.allowTargetBlank &&\n\t\tnode.linkTo?.link_type === LinkType.Web\n\t) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t};\n\t}\n\n\treturn node;\n};\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n *\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t};\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i];\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (\n\t\t\t\t\t!model.config?.allowTargetBlank &&\n\t\t\t\t\tspan.data.link_type === LinkType.Web\n\t\t\t\t) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span);\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode;\n};\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = [];\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model);\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i];\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model));\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node);\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field;\n};\n"],"names":["LinkType","RichTextNodeType"],"mappings":";;;;AAmBA,MAAM,sBAAsB,CAC3B,UACwB;;AACxB,QAAM,mBAAwC,CAAA;AAE9C,MAAI,MAAM,QAAQ;AACjB,QAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAAO;AAClD,uBAAiB,KAChB,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;AAAA,IAAA,WAEhD,YAAY,MAAM,UAAU,MAAM,OAAO,QAAQ;AAC3D,uBAAiB,KAChB,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;AAAA,IAE5D;AAEI,SAAA,WAAM,OAAO,WAAb,mBAAqB,QAAQ;AAChC,uBAAiB,KAAK,OAAO;AAAA,IAC9B;AAAA,EACD;AAEO,SAAA;AACR;AAUA,MAAM,kBAAkB,CACvB,MACA,UACgB;;AAEf,MAAA,GAAC,WAAM,WAAN,mBAAc,uBACf,UAAK,WAAL,mBAAa,eAAcA,cAAS,KACnC;AACM,WAAA;AAAA,MACN,GAAG;AAAA,MACH,QAAQ;AAAA,QACP,GAAG,KAAK;AAAA,QACR,QAAQ;AAAA,MACR;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;AAWA,MAAM,iBAAiB,CACtB,MACA,OACA,qBACe;;AACf,QAAM,eAA2B;AAAA,IAChC,GAAG;AAAA,IACH,OAAO,CAAE;AAAA,EAAA;AAGV,WAAS,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;AACrC,UAAA,OAAO,KAAK,MAAM,CAAC;AAEzB,QAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;AACrC,UAAA,KAAK,SAASC,SAAA,iBAAiB,WAAW;AAE5C,YAAA,GAAC,WAAM,WAAN,mBAAc,qBACf,KAAK,KAAK,cAAcD,cAAS,KAChC;AACD,uBAAa,MAAM,KAAK;AAAA,YACvB,GAAG;AAAA,YACH,MAAM;AAAA,cACL,GAAG,KAAK;AAAA,cACR,QAAQ;AAAA,YACR;AAAA,UAAA,CACD;AAAA,QAAA,OACK;AACO,uBAAA,MAAM,KAAK,IAAI;AAAA,QAC7B;AAAA,MACU,WAAA,KAAK,SAASC,SAAAA,iBAAiB,OAAO;AAChD,aAAI,iBAAM,WAAN,mBAAc,WAAd,mBAAsB,SAAS,KAAK,KAAK,QAAQ;AACvC,uBAAA,MAAM,KAAK,IAAI;AAAA,QAC7B;AAAA,MAAA,OACM;AACO,qBAAA,MAAM,KAAK,IAAI;AAAA,MAC7B;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AACR;AAUa,MAAA,sBAAsB,CAClC,eACA,UACU;AACV,QAAM,QAAkB,CAAA;AAElB,QAAA,mBAAmB,oBAAoB,KAAK;AAElD;AAAA,QACK,IAAI;AAAA;AAAA,IAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS;AAAA,IACtE;AAAA,IACC;AACK,UAAA,OAAO,cAAc,CAAC;AAE5B,QAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;AACrC,UAAA,KAAK,SAASA,SAAA,iBAAiB,OAAO;AACzC,cAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;AAAA,MAC7B,WAAA,KAAK,SAASA,SAAAA,iBAAiB,OAAO;AAChD,cAAM,KAAK,IAAI;AAAA,MAAA,OACT;AACN,cAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;AAAA,MACzD;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AACR;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CustomTypeModelRichTextField } from "../types/model/richText";
|
|
2
|
+
import { RichTextField } from "../types/value/richText";
|
|
3
|
+
import { TitleField } from "../types/value/title";
|
|
4
|
+
/**
|
|
5
|
+
* Filter a rich text field based on given model.
|
|
6
|
+
*
|
|
7
|
+
* @param richTextField - A rich text or title field from Prismic.
|
|
8
|
+
* @param model - A rich text or title field custom type model.
|
|
9
|
+
*
|
|
10
|
+
* @returns A rich text or title field filtered based on the given model.
|
|
11
|
+
*/
|
|
12
|
+
export declare const filterRichTextField: <Field extends RichTextField | TitleField>(richTextField: Field, model: CustomTypeModelRichTextField) => Field;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { LinkType } from "../types/value/link.js";
|
|
2
|
+
import { RichTextNodeType } from "../types/value/richText.js";
|
|
3
|
+
const getAllowedNodeTypes = (model) => {
|
|
4
|
+
var _a;
|
|
5
|
+
const allowedNodeTypes = [];
|
|
6
|
+
if (model.config) {
|
|
7
|
+
if ("multi" in model.config && model.config.multi) {
|
|
8
|
+
allowedNodeTypes.push(...model.config.multi.split(","));
|
|
9
|
+
} else if ("single" in model.config && model.config.single) {
|
|
10
|
+
allowedNodeTypes.push(...model.config.single.split(","));
|
|
11
|
+
}
|
|
12
|
+
if ((_a = model.config.labels) == null ? void 0 : _a.length) {
|
|
13
|
+
allowedNodeTypes.push("label");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return allowedNodeTypes;
|
|
17
|
+
};
|
|
18
|
+
const filterImageNode = (node, model) => {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && ((_b = node.linkTo) == null ? void 0 : _b.link_type) === LinkType.Web) {
|
|
21
|
+
return {
|
|
22
|
+
...node,
|
|
23
|
+
linkTo: {
|
|
24
|
+
...node.linkTo,
|
|
25
|
+
target: void 0
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return node;
|
|
30
|
+
};
|
|
31
|
+
const filterTextNode = (node, model, allowedNodeTypes) => {
|
|
32
|
+
var _a, _b, _c;
|
|
33
|
+
const filteredNode = {
|
|
34
|
+
...node,
|
|
35
|
+
spans: []
|
|
36
|
+
};
|
|
37
|
+
for (let i = 0; i < node.spans.length; i++) {
|
|
38
|
+
const span = node.spans[i];
|
|
39
|
+
if (allowedNodeTypes.includes(span.type)) {
|
|
40
|
+
if (span.type === RichTextNodeType.hyperlink) {
|
|
41
|
+
if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && span.data.link_type === LinkType.Web) {
|
|
42
|
+
filteredNode.spans.push({
|
|
43
|
+
...span,
|
|
44
|
+
data: {
|
|
45
|
+
...span.data,
|
|
46
|
+
target: void 0
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
filteredNode.spans.push(span);
|
|
51
|
+
}
|
|
52
|
+
} else if (span.type === RichTextNodeType.label) {
|
|
53
|
+
if ((_c = (_b = model.config) == null ? void 0 : _b.labels) == null ? void 0 : _c.includes(span.data.label)) {
|
|
54
|
+
filteredNode.spans.push(span);
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
filteredNode.spans.push(span);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return filteredNode;
|
|
62
|
+
};
|
|
63
|
+
const filterRichTextField = (richTextField, model) => {
|
|
64
|
+
const nodes = [];
|
|
65
|
+
const allowedNodeTypes = getAllowedNodeTypes(model);
|
|
66
|
+
for (
|
|
67
|
+
let i = 0;
|
|
68
|
+
// Only process the first node if it's a "single" node rich text type
|
|
69
|
+
i < (model.config && "multi" in model.config ? richTextField.length : 1);
|
|
70
|
+
i++
|
|
71
|
+
) {
|
|
72
|
+
const node = richTextField[i];
|
|
73
|
+
if (allowedNodeTypes.includes(node.type)) {
|
|
74
|
+
if (node.type === RichTextNodeType.image) {
|
|
75
|
+
nodes.push(filterImageNode(node, model));
|
|
76
|
+
} else if (node.type === RichTextNodeType.embed) {
|
|
77
|
+
nodes.push(node);
|
|
78
|
+
} else {
|
|
79
|
+
nodes.push(filterTextNode(node, model, allowedNodeTypes));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return nodes;
|
|
84
|
+
};
|
|
85
|
+
export {
|
|
86
|
+
filterRichTextField
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=filterRichTextField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filterRichTextField.js","sources":["../../../src/lib/filterRichTextField.ts"],"sourcesContent":["import { CustomTypeModelRichTextField } from \"../types/model/richText\";\nimport { LinkType } from \"../types/value/link\";\nimport {\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeType,\n\tRichTextNodeTypes,\n} from \"../types/value/richText\";\nimport { TitleField } from \"../types/value/title\";\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n *\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (\n\tmodel: CustomTypeModelRichTextField,\n): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = []; // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.multi.split(\",\") as RichTextNodeTypes[]),\n\t\t\t);\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.single.split(\",\") as RichTextNodeTypes[]),\n\t\t\t);\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\");\n\t\t}\n\t}\n\n\treturn allowedNodeTypes;\n};\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (\n\tnode: RTImageNode,\n\tmodel: CustomTypeModelRichTextField,\n): RTImageNode => {\n\tif (\n\t\t!model.config?.allowTargetBlank &&\n\t\tnode.linkTo?.link_type === LinkType.Web\n\t) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t};\n\t}\n\n\treturn node;\n};\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n *\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t};\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i];\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (\n\t\t\t\t\t!model.config?.allowTargetBlank &&\n\t\t\t\t\tspan.data.link_type === LinkType.Web\n\t\t\t\t) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span);\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode;\n};\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = [];\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model);\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i];\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model));\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node);\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes));\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field;\n};\n"],"names":[],"mappings":";;AAmBA,MAAM,sBAAsB,CAC3B,UACwB;;AACxB,QAAM,mBAAwC,CAAA;AAE9C,MAAI,MAAM,QAAQ;AACjB,QAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAAO;AAClD,uBAAiB,KAChB,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;AAAA,IAAA,WAEhD,YAAY,MAAM,UAAU,MAAM,OAAO,QAAQ;AAC3D,uBAAiB,KAChB,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;AAAA,IAE5D;AAEI,SAAA,WAAM,OAAO,WAAb,mBAAqB,QAAQ;AAChC,uBAAiB,KAAK,OAAO;AAAA,IAC9B;AAAA,EACD;AAEO,SAAA;AACR;AAUA,MAAM,kBAAkB,CACvB,MACA,UACgB;;AAEf,MAAA,GAAC,WAAM,WAAN,mBAAc,uBACf,UAAK,WAAL,mBAAa,eAAc,SAAS,KACnC;AACM,WAAA;AAAA,MACN,GAAG;AAAA,MACH,QAAQ;AAAA,QACP,GAAG,KAAK;AAAA,QACR,QAAQ;AAAA,MACR;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;AAWA,MAAM,iBAAiB,CACtB,MACA,OACA,qBACe;;AACf,QAAM,eAA2B;AAAA,IAChC,GAAG;AAAA,IACH,OAAO,CAAE;AAAA,EAAA;AAGV,WAAS,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;AACrC,UAAA,OAAO,KAAK,MAAM,CAAC;AAEzB,QAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;AACrC,UAAA,KAAK,SAAS,iBAAiB,WAAW;AAE5C,YAAA,GAAC,WAAM,WAAN,mBAAc,qBACf,KAAK,KAAK,cAAc,SAAS,KAChC;AACD,uBAAa,MAAM,KAAK;AAAA,YACvB,GAAG;AAAA,YACH,MAAM;AAAA,cACL,GAAG,KAAK;AAAA,cACR,QAAQ;AAAA,YACR;AAAA,UAAA,CACD;AAAA,QAAA,OACK;AACO,uBAAA,MAAM,KAAK,IAAI;AAAA,QAC7B;AAAA,MACU,WAAA,KAAK,SAAS,iBAAiB,OAAO;AAChD,aAAI,iBAAM,WAAN,mBAAc,WAAd,mBAAsB,SAAS,KAAK,KAAK,QAAQ;AACvC,uBAAA,MAAM,KAAK,IAAI;AAAA,QAC7B;AAAA,MAAA,OACM;AACO,qBAAA,MAAM,KAAK,IAAI;AAAA,MAC7B;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AACR;AAUa,MAAA,sBAAsB,CAClC,eACA,UACU;AACV,QAAM,QAAkB,CAAA;AAElB,QAAA,mBAAmB,oBAAoB,KAAK;AAElD;AAAA,QACK,IAAI;AAAA;AAAA,IAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS;AAAA,IACtE;AAAA,IACC;AACK,UAAA,OAAO,cAAc,CAAC;AAE5B,QAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;AACrC,UAAA,KAAK,SAAS,iBAAiB,OAAO;AACzC,cAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;AAAA,MAC7B,WAAA,KAAK,SAAS,iBAAiB,OAAO;AAChD,cAAM,KAAK,IAAI;AAAA,MAAA,OACT;AACN,cAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;AAAA,MACzD;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AACR;"}
|