Skip to content

Commit

Permalink
feat: add @prismicio/client/richtext entry (#318)
Browse files Browse the repository at this point in the history
* feat: add `@prismicio/client/richtext` entry

* refactor: remove dependency on `@prismicio/richtext`

* chore(deps): maintain dependencies

* chore(deps): pin TypeScript version because ignore comments behavior changed

---------

Co-authored-by: lihbr <lihbr@users.noreply.github.com>
  • Loading branch information
angeloashmore and lihbr authored Oct 6, 2023
1 parent b43557d commit fb555fd
Show file tree
Hide file tree
Showing 38 changed files with 6,388 additions and 594 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
node: [16]
node: [20]
typescript: ["4.6", "4.7", "4.8", "4.9", "5.0"]

steps:
Expand Down Expand Up @@ -110,7 +110,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
node: [16]
node: [20]

steps:
- name: Set up Node
Expand Down
1,391 changes: 880 additions & 511 deletions package-lock.json

Large diffs are not rendered by default.

49 changes: 34 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,32 @@
"default": "./dist/index.js"
}
},
"./richtext": {
"require": {
"types": "./dist/richtext/index.d.ts",
"default": "./dist/richtext.cjs"
},
"import": {
"types": "./dist/richtext/index.d.ts",
"default": "./dist/richtext.js"
}
},
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"react-native": "./dist/index.js",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"dist/index.d.ts"
],
"richtext": [
"dist/richtext/index.d.ts"
]
}
},
"files": [
"dist",
"src"
Expand All @@ -59,38 +79,37 @@
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size"
},
"dependencies": {
"@prismicio/richtext": "^2.1.5",
"imgix-url-builder": "^0.0.4"
},
"devDependencies": {
"@prismicio/mock": "^0.3.1",
"@prismicio/types-internal": "2.0.0",
"@size-limit/preset-small-lib": "^8.2.6",
"@prismicio/types-internal": "2.3.0",
"@size-limit/preset-small-lib": "^9.0.0",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"@vitest/coverage-v8": "^0.34.1",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"@vitest/coverage-v8": "^0.34.6",
"abort-controller": "^3.0.0",
"eslint": "^8.47.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-tsdoc": "^0.2.17",
"msw": "^1.2.3",
"msw": "^1.3.2",
"node-fetch": "^3.3.2",
"prettier": "^3.0.1",
"prettier-plugin-jsdoc": "^1.0.1",
"size-limit": "^8.2.6",
"prettier": "^3.0.3",
"prettier-plugin-jsdoc": "1.0.2",
"size-limit": "^9.0.0",
"standard-version": "^9.5.0",
"ts-expect": "^1.3.0",
"typescript": "^4.9.5",
"vite": "^4.4.9",
"typescript": "~5.0.4",
"vite": "^4.4.11",
"vite-plugin-sdk": "^0.1.1",
"vitest": "^0.34.1"
"vitest": "^0.34.6"
},
"engines": {
"node": ">=14.15.0"
},
"publishConfig": {
"access": "public"
}
}
}
30 changes: 15 additions & 15 deletions src/helpers/asHTML.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import {
RichTextFunctionSerializer,
RichTextMapSerializer,
RichTextMapSerializerFunction,
composeSerializers,
serialize,
wrapMapSerializer,
} from "@prismicio/richtext";

import {
serializeEmbed,
serializeHyperlink,
Expand All @@ -18,14 +9,23 @@ import {

import type { RichTextField } from "../types/value/richText";

import {
RichTextFunctionSerializer,
RichTextMapSerializer,
RichTextMapSerializerFunction,
composeSerializers,
serialize,
wrapMapSerializer,
} from "../richtext";

import { LinkResolverFunction } from "./asLink";

/**
* Serializes a node from a rich text or title field with a function to HTML.
*
* Unlike a typical `@prismicio/richtext` function serializer, this serializer
* converts the `children` argument to a single string rather than an array of
* strings.
* Unlike a typical `@prismicio/client/richtext` function serializer, this
* serializer converts the `children` argument to a single string rather than an
* array of strings.
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
*/
Expand All @@ -40,7 +40,7 @@ export type HTMLRichTextFunctionSerializer = (
/**
* Serializes a node from a rich text or title field with a map to HTML
*
* Unlike a typical `@prismicio/richtext` map serializer, this serializer
* Unlike a typical `@prismicio/client/richtext` map serializer, this serializer
* converts the `children` property to a single string rather than an array of
* strings and accepts shorthand declarations.
*
Expand All @@ -55,7 +55,7 @@ export type HTMLRichTextMapSerializer = {
/**
* Serializes a node from a rich text or title field with a map to HTML
*
* Unlike a typical `@prismicio/richtext` map serializer, this serializer
* Unlike a typical `@prismicio/client/richtext` map serializer, this serializer
* converts the `children` property to a single string rather than an array of
* strings but doesn't accept shorthand declarations.
*
Expand Down Expand Up @@ -258,7 +258,7 @@ const createHTMLRichTextSerializer = (
/**
* Wraps a map serializer into a regular function serializer. The given map
* serializer should accept children as a string, not as an array of strings
* like `@prismicio/richtext`'s `wrapMapSerializer`.
* like `@prismicio/client/richtext`'s `wrapMapSerializer`.
*
* @param mapSerializer - Map serializer to wrap
*
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/asText.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { asText as baseAsText } from "@prismicio/richtext";

import { RichTextField } from "../types/value/richText";

import { asText as baseAsText } from "../richtext/asText";

/**
* Configuration that determines the output of `asText()`.
*/
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Imports are used for deprecations.
import type { CustomTypeModelIntegrationField } from "./types/model/integration";
import type { IntegrationField } from "./types/value/integration";
import { RichTextNodeType } from "./types/value/richText";

import type {
HTMLRichTextFunctionSerializer,
Expand Down Expand Up @@ -74,9 +75,6 @@ export * as isFilled from "./helpers/isFilled";
// Conversion helper.
export { documentToLinkField } from "./helpers/documentToLinkField";

// RichText element types.
export { Element } from "@prismicio/richtext";

export type { LinkResolverFunction } from "./helpers/asLink";
export type { AsLinkAttrsConfig } from "./helpers/asLinkAttrs";

Expand Down Expand Up @@ -110,7 +108,12 @@ export { ParsingError } from "./errors/ParsingError";
//=============================================================================

// Values - Types representing Prismic content.
export { RichTextNodeType } from "./types/value/richText";
/**
* @deprecated Use {@link RichTextNodeType} instead.
*/
// TODO: Remove in v8.
const Element = RichTextNodeType;
export { RichTextNodeType, Element };
export { LinkType } from "./types/value/link";
export { OEmbedType } from "./types/value/embed";

Expand Down
3 changes: 1 addition & 2 deletions src/lib/serializerHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RichTextMapSerializer } from "@prismicio/richtext";

import type { RichTextMapSerializer } from "../richtext/types";
import { LinkType } from "../types/value/link";
import { RTAnyNode } from "../types/value/richText";

Expand Down
27 changes: 27 additions & 0 deletions src/richtext/asText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RTTextNode, RichTextField } from "../types/value/richText";

/**
* Serializes a rich text or title field to a plain text string
*
* @param richTextField - A rich text or title field from Prismic
* @param separator - Separator used to join each element, defaults to a space
*
* @returns Plain text equivalent of the provided rich text or title field
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
*/
export const asText = (
richTextField: RichTextField,
separator = " ",
): string => {
let result = "";

for (let i = 0; i < richTextField.length; i++) {
if ("text" in richTextField[i]) {
result +=
(result ? separator : "") + (richTextField[i] as RTTextNode).text;
}
}

return result;
};
Loading

0 comments on commit fb555fd

Please sign in to comment.