mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-15 23:54:42 -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
html-to-react/src
38
html-to-react/src/convertNodeToReactElement.ts
Normal file
38
html-to-react/src/convertNodeToReactElement.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { ReactElement } from 'react'
|
||||
import { Node } from 'domhandler'
|
||||
import { ElementType } from 'domelementtype'
|
||||
import { processTextNode } from './elementTypes/ProcessTextNode.js'
|
||||
import { processTagNode } from './elementTypes/ProcessTagNode.js'
|
||||
import { processStyleNode } from './elementTypes/ProcessStyleNode.js'
|
||||
import { NodeToReactElementTransformer } from './NodeToReactElementTransformer.js'
|
||||
|
||||
/**
|
||||
* Converts a htmlparser2 node to a React element
|
||||
*
|
||||
* @param {Object} node The htmlparser2 node to convert
|
||||
* @param {Number} index The index of the current node
|
||||
* @param {Function} transform Transform function to apply to children of the node
|
||||
* @returns {React.Element}
|
||||
*/
|
||||
export function convertNodeToReactElement(
|
||||
node: Node,
|
||||
index: string | number,
|
||||
transform?: NodeToReactElementTransformer
|
||||
): ReactElement | string | null {
|
||||
switch (node.type) {
|
||||
case ElementType.Text:
|
||||
return processTextNode(node)
|
||||
case ElementType.Tag:
|
||||
return processTagNode(node, index, transform)
|
||||
case ElementType.Style:
|
||||
return processStyleNode(node, index)
|
||||
default:
|
||||
return null
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue