mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 06:34:39 -04:00
feat: import html-to-react from https://github.com/hedgedoc/html-to-react
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
5dc6526278
commit
84527f065c
37 changed files with 1388 additions and 0 deletions
32
html-to-react/src/processNodes.ts
Normal file
32
html-to-react/src/processNodes.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* 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)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue