-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
88 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
import { IDomHtmlNode, IDomNode, IDomTextNode, IOutputResult } from "../../parser/index.js" | ||
import { DomHtmlNode, DomNode, DomTextNode, ParseResult } from "../../parser/index.js" | ||
import { isText } from "../../utils/index.js" | ||
|
||
export type ResolveIDomTextNodeType = ((node: IDomTextNode) => unknown) | null | ||
export type ResolveIDomHtmlNodeType = ((node: IDomHtmlNode, traverse: (node: IDomNode) => unknown) => unknown) | null | ||
export type ResolveDomTextNodeType = ((node: DomTextNode) => unknown) | null | ||
export type ResolveDomHtmlNodeType = ((node: DomHtmlNode, traverse: (node: DomNode) => unknown) => unknown) | null | ||
|
||
type CustomResolversType = { | ||
resolveIDomTextNode: ResolveIDomTextNodeType | ||
resolveIDomHtmlNode: ResolveIDomHtmlNodeType | ||
resolveDomTextNode: ResolveDomTextNodeType | ||
resolveDomHtmlNode: ResolveDomHtmlNodeType | ||
} | ||
|
||
export type TransformIDomNodeType = ( | ||
node: IDomNode, | ||
export type TransformDomNodeType = ( | ||
node: DomNode, | ||
customResolvers: CustomResolversType | ||
) => unknown; | ||
|
||
export const transformToJson = ( | ||
result: IOutputResult, | ||
result: ParseResult, | ||
customResolvers?: CustomResolversType | ||
) => { | ||
if (!customResolvers) { | ||
return result.children | ||
} | ||
|
||
return result.children.map(node => transformIDomNode(node, customResolvers)) | ||
return result.children.map(node => transformDomNode(node, customResolvers)) | ||
} | ||
|
||
const nodeIdentity = (node: IDomNode) => node | ||
const nodeIdentity = (node: DomNode) => node | ||
|
||
const transformIDomNode: TransformIDomNodeType = ( | ||
node: IDomNode, | ||
const transformDomNode: TransformDomNodeType = ( | ||
node: DomNode, | ||
customResolvers: CustomResolversType | ||
) => { | ||
const {resolveIDomHtmlNode, resolveIDomTextNode} = customResolvers; | ||
const {resolveDomHtmlNode, resolveDomTextNode} = customResolvers; | ||
if (isText(node)) { | ||
return resolveIDomTextNode ? resolveIDomTextNode(node) : nodeIdentity(node); | ||
return resolveDomTextNode ? resolveDomTextNode(node) : nodeIdentity(node); | ||
} | ||
return resolveIDomHtmlNode ? resolveIDomHtmlNode(node, (node) => transformIDomNode(node, customResolvers)) : nodeIdentity(node) | ||
return resolveDomHtmlNode ? resolveDomHtmlNode(node, (node) => transformDomNode(node, customResolvers)) : nodeIdentity(node) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
import { IDomHtmlNode, IDomNode, IDomTextNode } from "../index.js"; | ||
import { DomHtmlNode, DomNode, DomTextNode } from "../index.js"; | ||
|
||
export const isOrderedListBlock = (node: IDomHtmlNode): boolean => | ||
export const isOrderedListBlock = (node: DomHtmlNode): boolean => | ||
node.tagName === 'ol'; | ||
|
||
export const isUnorderedListBlock = (node: IDomHtmlNode): boolean => | ||
export const isUnorderedListBlock = (node: DomHtmlNode): boolean => | ||
node.tagName === 'ul'; | ||
|
||
export const isListBlock = (node: IDomHtmlNode): boolean => | ||
export const isListBlock = (node: DomHtmlNode): boolean => | ||
isUnorderedListBlock(node) || isOrderedListBlock(node) | ||
|
||
export const isListItem = (node: IDomHtmlNode): boolean => | ||
export const isListItem = (node: DomHtmlNode): boolean => | ||
node.tagName === 'li'; | ||
|
||
export const isExternalLink = (node: IDomHtmlNode): boolean => | ||
export const isExternalLink = (node: DomHtmlNode): boolean => | ||
isAnchor(node) && !node.attributes['data-item-id']; | ||
|
||
export const isAnchor = (node: IDomHtmlNode): boolean => | ||
export const isAnchor = (node: DomHtmlNode): boolean => | ||
node.tagName === 'a'; | ||
|
||
/** | ||
* Returns `true` for text nodes and type guards the node as `IDomTextNode`. | ||
* Returns `true` for text nodes and type guards the node as `DomTextNode`. | ||
*/ | ||
export const isText = (node: IDomNode): node is IDomTextNode => | ||
export const isText = (node: DomNode): node is DomTextNode => | ||
node.type === 'text'; | ||
|
||
/** | ||
* Returns `true` for HTML nodes and type guards the node as `IDomHtmlNode`. | ||
* Returns `true` for HTML nodes and type guards the node as `DomHtmlNode`. | ||
*/ | ||
export const isElement = (node: IDomNode): node is IDomHtmlNode => | ||
export const isElement = (node: DomNode): node is DomHtmlNode => | ||
node.type === 'tag'; | ||
|
||
/** | ||
* Returns `true` if the node is a linked item node (`<object></object>`). | ||
*/ | ||
export const isLinkedItem = (node: IDomNode): boolean => | ||
export const isLinkedItem = (node: DomNode): boolean => | ||
isElement(node) && | ||
node.tagName === 'object' && | ||
node.attributes['type'] === 'application/kenticocloud'; | ||
/** | ||
* Returns `true` if the node is a rich text image node (`<figure></figure>`). | ||
*/ | ||
export const isImage = (node: IDomNode): boolean => | ||
export const isImage = (node: DomNode): boolean => | ||
isElement(node) && | ||
node.tagName === 'figure' && | ||
node.attributes['data-image-id'] ? true : false; | ||
|
||
/** | ||
* Returns `true` if the node is a link to a content item. | ||
*/ | ||
export const isItemLink = (node: IDomHtmlNode): boolean => | ||
export const isItemLink = (node: DomHtmlNode): boolean => | ||
isAnchor(node) && !isExternalLink(node); |
Oops, something went wrong.