hedgedoc/html-to-react/src/processNodes.ts
Tilman Vatteroth 84527f065c feat: import html-to-react from https://github.com/hedgedoc/html-to-react
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2023-08-26 15:07:49 +02:00

32 lines
1 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { convertNodeToReactElement } from './convertNodeToReactElement.js'
import { Node } from 'domhandler'
import { ReactElement } from 'react'
import { NodeToReactElementTransformer } from './NodeToReactElementTransformer.js'
/**
* Processes the nodes generated by htmlparser2 and convert them all into React elements
*
* @param {Object[]} nodes List of nodes to process
* @param {Function} transform Transform function to optionally apply to nodes
* @returns {React.Element[]} The list of processed React elements
*/
export function processNodes(
nodes: Node[],
transform?: NodeToReactElementTransformer
): (ReactElement | string | null)[] {
return nodes.map((node, index) => {
if (transform) {
const transformed = transform(node, index)
if (transformed === null || !!transformed) {
return transformed
}
}
return convertNodeToReactElement(node, index, transform)
})
}