Skip to content

Commit

Permalink
feat: update htmlAsRichText API
Browse files Browse the repository at this point in the history
- remove `sync` helpers
- rename `converter` option to `serializer`
- add `direction`, `include` options
- remove `silent` option
- allow deep selectors in the `serializer` map
- refactor helpers return type
- refactor `rehypeRichText` plugin
- refactor tests
  • Loading branch information
lihbr committed Jul 16, 2024
1 parent aa0180d commit 7d14962
Show file tree
Hide file tree
Showing 14 changed files with 856 additions and 744 deletions.
145 changes: 2 additions & 143 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@
"remark-rehype": "^11.1.0",
"unified": "^11.0.5",
"unist-util-remove": "^4.0.0",
"unist-util-visit": "^5.0.0",
"unist-util-visit-parents": "^6.0.1",
"vfile": "^6.0.1",
"vfile-reporter": "^8.1.1"
"vfile": "^6.0.1"
},
"devDependencies": {
"@prismicio/mock": "^0.3.1",
Expand Down
28 changes: 7 additions & 21 deletions src/richtext/htmlAsRichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { unified } from "unified";

import { AsRichTextConfig, AsRichTextReturnType } from "./types";

import { rehypeRichText } from "./unified/rehypeRichText";

const htmlProcessor = (config?: AsRichTextConfig) =>
unified().use(rehypeParse).use(rehypeRichText, config);
import { rehypeRichText } from "./utils/rehypeRichText";

/**
* Converts an HTML string to a rich text field.
Expand All @@ -17,25 +14,14 @@ const htmlProcessor = (config?: AsRichTextConfig) =>
*
* @returns Rich text field equivalent of the provided HTML string.
*/
export const htmlAsRichText = (
export const htmlAsRichText = async (
html: string,
config?: AsRichTextConfig,
): Promise<AsRichTextReturnType> => {
return htmlProcessor(config).process(html);
};
const { result, messages } = await unified()
.use(rehypeParse, { emitParseErrors: true })
.use(rehypeRichText, config)
.process(html);

/**
* Converts an HTML string to a rich text field synchronously.
*
* @param html - An HTML string
* @param config - Configuration that determines the output of
* `htmlAsRichTextSync()`
*
* @returns Rich text field equivalent of the provided HTML string.
*/
export const htmlAsRichTextSync = (
html: string,
config?: AsRichTextConfig,
): AsRichTextReturnType => {
return htmlProcessor(config).processSync(html);
return { result, warnings: messages };
};
7 changes: 2 additions & 5 deletions src/richtext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ export { composeSerializers } from "./composeSerializers";

export { RichTextNodeType as Element } from "../types/value/richText";

export { htmlAsRichText, htmlAsRichTextSync } from "./htmlAsRichText";
export {
markdownAsRichText,
markdownAsRichTextSync,
} from "./markdownAsRichText";
export { htmlAsRichText } from "./htmlAsRichText";
export { markdownAsRichText } from "./markdownAsRichText";

export type {
AsRichTextConfig,
Expand Down
35 changes: 9 additions & 26 deletions src/richtext/markdownAsRichText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import { unified } from "unified";

import { AsRichTextConfig, AsRichTextReturnType } from "./types";

import { rehypeRichText } from "./unified/rehypeRichText";
import { rehypeRichText } from "./utils/rehypeRichText";

// Used for TSDocs only.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { htmlAsRichText, htmlAsRichTextSync } from "./htmlAsRichText";

const markdownProcessor = (config?: AsRichTextConfig) =>
unified().use(remarkParse).use(remarkRehype).use(rehypeRichText, config);
import type { htmlAsRichText } from "./htmlAsRichText";

/**
* Converts a markdown string to a rich text field.
Expand All @@ -26,29 +23,15 @@ const markdownProcessor = (config?: AsRichTextConfig) =>
*
* @returns Rich text field equivalent of the provided markdown string.
*/
export const markdownAsRichText = (
export const markdownAsRichText = async (
markdown: string,
config?: AsRichTextConfig,
): Promise<AsRichTextReturnType> => {
return markdownProcessor(config).process(markdown);
};
const { result, messages } = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeRichText, config)
.process(markdown);

/**
* Converts an markdown string to a rich text field synchronously.
*
* @remarks
* To convert markdown to a rich text field, this function first converts it to
* HTML. It's essentially a sugar above {@link htmlAsRichTextSync}.
*
* @param markdown - An markdown string
* @param config - Configuration that determines the output of
* `markdownAsRichTextSync()`
*
* @returns Rich text field equivalent of the provided markdown string.
*/
export const markdownAsRichTextSync = (
markdown: string,
config?: AsRichTextConfig,
): AsRichTextReturnType => {
return markdownProcessor(config).processSync(markdown);
return { result, warnings: messages };
};
7 changes: 5 additions & 2 deletions src/richtext/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
RichTextNodeType,
} from "../types/value/richText";

import { RehypeRichTextConfig } from "./unified/rehypeRichText";
import { RehypeRichTextConfig } from "./utils/rehypeRichText";

// Serializers

Expand Down Expand Up @@ -165,4 +165,7 @@ export type AsRichTextConfig = RehypeRichTextConfig;
/**
* The return type of `*AsRichText` functions.
*/
export type AsRichTextReturnType = VFile & { result: RichTextField };
export type AsRichTextReturnType = {
result: RichTextField;
warnings: VFile["messages"];
};
Loading

0 comments on commit 7d14962

Please sign in to comment.