mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 14:44:43 -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
38
html-to-react/src/convertHtmlToReact.ts
Normal file
38
html-to-react/src/convertHtmlToReact.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { parseDocument } from 'htmlparser2'
|
||||
import { processNodes } from './processNodes.js'
|
||||
import { ReactElement } from 'react'
|
||||
import { Document } from 'domhandler'
|
||||
import { NodeToReactElementTransformer } from './NodeToReactElementTransformer.js'
|
||||
|
||||
export interface ParserOptions {
|
||||
decodeEntities?: boolean
|
||||
transform?: NodeToReactElementTransformer
|
||||
preprocessNodes?: (nodes: Document) => Document
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an HTML string and returns a list of React components generated from it
|
||||
*
|
||||
* @param {String} html The HTML to convert into React component
|
||||
* @param {Object} options Options to pass
|
||||
* @returns {Array} List of top level React elements
|
||||
*/
|
||||
export function convertHtmlToReact(
|
||||
html: string,
|
||||
options?: ParserOptions
|
||||
): (ReactElement | string | null)[] {
|
||||
const parsedDocument = parseDocument(html, {
|
||||
decodeEntities: options?.decodeEntities ?? true
|
||||
})
|
||||
|
||||
const processedDocument =
|
||||
options?.preprocessNodes?.(parsedDocument) ?? parsedDocument
|
||||
|
||||
return processNodes(processedDocument.childNodes, options?.transform)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue