Skip to content

Commit

Permalink
Merge pull request #82 from oleast/fix/deps-prettier-3
Browse files Browse the repository at this point in the history
  • Loading branch information
oleast authored Jul 31, 2023
2 parents 6506966 + cad68c7 commit fd09ddf
Show file tree
Hide file tree
Showing 9 changed files with 961 additions and 717 deletions.
1,635 changes: 939 additions & 696 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
],
"scripts": {
"lint": "prettier --check ./src",
"lint:fix": "prettier --write ./src",
"build": "tsc --project tsconfig.build.json && tsc --project tsconfig.module.json",
"test": "vitest run",
"test:watch": "vitest watch"
Expand All @@ -51,7 +52,7 @@
"@semantic-release/npm": "^10.0.2",
"@semantic-release/release-notes-generator": "^11.0.1",
"conventional-changelog-conventionalcommits": "^6.1.0",
"prettier": "^2.8.1",
"prettier": "^3.0.0",
"semantic-release": "^21.0.0",
"typescript": "^5.0.2",
"vitest": "^0.33.0"
Expand Down
2 changes: 1 addition & 1 deletion src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DEFAULT_NODE_TYPE_FOR_HTML_TAG: Partial<
};

const getDefaultNodeTypeForHtmlTag = (
tagName: HTMLTagName
tagName: HTMLTagName,
): BLOCKS | MARKS | INLINES | undefined => {
return DEFAULT_NODE_TYPE_FOR_HTML_TAG[tagName];
};
Expand Down
8 changes: 4 additions & 4 deletions src/htmlStringToDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const DEFAULT_TAG_CONVERTERS: Partial<
const mapHtmlNodeToRichTextNode = (
node: HTMLNode,
marks: Mark[],
options: OptionsWithDefaults
options: OptionsWithDefaults,
) => {
const { convertText, convertTag } = options;
if (node.type === "text") {
Expand All @@ -68,7 +68,7 @@ const mapHtmlNodeToRichTextNode = (
const allMarks = newMarks.concat(marks);
if (node.type === "element") {
return node.children.flatMap((child) =>
mapHtmlNodeToRichTextNode(child, allMarks, options)
mapHtmlNodeToRichTextNode(child, allMarks, options),
);
}
return getAsList(mapHtmlNodeToRichTextNode(node, allMarks, options));
Expand All @@ -85,7 +85,7 @@ const mapHtmlNodeToRichTextNode = (

export const htmlStringToDocument = (
htmlString: string,
options: Options = {}
options: Options = {},
): Document => {
const optionsWithDefaults: OptionsWithDefaults = {
convertTag: {
Expand All @@ -96,7 +96,7 @@ export const htmlStringToDocument = (
};
const parsedHtml = parseHtml(htmlString);
const richTextNodes = parsedHtml.flatMap((node) =>
mapHtmlNodeToRichTextNode(node, [], optionsWithDefaults)
mapHtmlNodeToRichTextNode(node, [], optionsWithDefaults),
);
return createDocumentNode(richTextNodes as TopLevelBlock[]);
};
4 changes: 2 additions & 2 deletions src/parseHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const isChildNodeTemplate = (childNode: ChildNode): childNode is Template => {
};

const isChildNodeDocumentType = (
childNode: ChildNode
childNode: ChildNode,
): childNode is DocumentType => {
return childNode.nodeName === "#documentType";
};
Expand All @@ -50,7 +50,7 @@ const mapChildNodeToHtmlNode = (childNode: ChildNode): HTMLNode | null => {
.map((c) => mapChildNodeToHtmlNode(c))
.filter(isNotNull),
attrs: Object.fromEntries(
childNode.attrs.map((attr) => [attr.name, attr.value])
childNode.attrs.map((attr) => [attr.name, attr.value]),
),
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const createText = (value: string, marks?: Mark | Mark[]): Text => {

export const createBlock = (
nodeType: BLOCKS,
content: Text | Block | Inline | Array<Text | Block | Inline>
content: Text | Block | Inline | Array<Text | Block | Inline>,
): Block => {
return {
nodeType,
Expand Down
10 changes: 5 additions & 5 deletions src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ describe("Parse HTML string to Contentful Document", () => {

const matchNode = helpers.createBlock(
BLOCKS.PARAGRAPH,
helpers.createText(matchText)
helpers.createText(matchText),
);

expect(htmlNodes).toMatchObject(
createDocumentNode([matchNode as TopLevelBlock])
createDocumentNode([matchNode as TopLevelBlock]),
);
});

it("Handles a complex convert option from 'span' with bold class to 'paragraph' and 'bold' mark", () => {
const styledSpanToMarkedParagraphConverter: TagConverter<Block> = (
node,
next
next,
) => {
const isBold = node.attrs.class === "bold";
const marks = isBold ? ({ type: "bold" } satisfies Mark) : undefined;
Expand All @@ -100,13 +100,13 @@ describe("Parse HTML string to Contentful Document", () => {
convertTag: {
span: styledSpanToMarkedParagraphConverter,
},
}
},
);

const matchNode = createDocumentNode([
helpers.createBlock(
BLOCKS.PARAGRAPH,
helpers.createText(matchText, { type: "bold" })
helpers.createText(matchText, { type: "bold" }),
),
] as TopLevelBlock[]);

Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ export type ConverterResult<TNodeType extends AnyContentfulNode> =
export type Next<TNodeType extends AnyContentfulNode = Block | Inline | Text> =
(
node: HTMLNode,
marks?: Mark | Mark[]
marks?: Mark | Mark[],
) => Array<ContentfulNodeContent<TNodeType>>;

export type TextConverter = (node: HTMLTextNode, marks: Mark[]) => Text;

export type TagConverter<
TNodeType extends AnyContentfulNode = Block | Inline | Text
TNodeType extends AnyContentfulNode = Block | Inline | Text,
> = (
node: HTMLElementNode,
next: Next<TNodeType>
next: Next<TNodeType>,
) => ConverterResult<TNodeType>;

export type ConvertTagOptions = Record<HTMLTagName | string, TagConverter>;
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export const getAsList = <T>(value: T | T[]): T[] => {
};

export const isBlockType = (
nodeType: BLOCKS | MARKS | INLINES
nodeType: BLOCKS | MARKS | INLINES,
): nodeType is BLOCKS => BLOCK_TYPES.includes(nodeType as BLOCKS);
export const isInlineType = (
nodeType: BLOCKS | MARKS | INLINES
nodeType: BLOCKS | MARKS | INLINES,
): nodeType is INLINES => INLINE_TYPES.includes(nodeType as INLINES);
export const isMarkType = (
nodeType: BLOCKS | MARKS | INLINES
nodeType: BLOCKS | MARKS | INLINES,
): nodeType is MARKS => MARK_TYPES.includes(nodeType as MARKS);

export const isNodeTypeMark = (node: Node | Text | Mark): node is Mark => {
Expand All @@ -47,7 +47,7 @@ export const isNodeTypeText = (node: Node | Text | Mark): node is Text => {

export const createDocumentNode = (
content: TopLevelBlock[],
data: NodeData = {}
data: NodeData = {},
): Document => ({
nodeType: BLOCKS.DOCUMENT,
data,
Expand Down

0 comments on commit fd09ddf

Please sign in to comment.