mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-22 03:05:19 -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
50
html-to-react/src/utils/convertInlineStyleToMap.ts
Normal file
50
html-to-react/src/utils/convertInlineStyleToMap.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
/**
|
||||
* Converts an inline style string into an object of React style properties
|
||||
*
|
||||
* @param {String} inlineStyle='' The inline style to convert
|
||||
* @returns {Object} The converted style
|
||||
*/
|
||||
export function convertInlineStyleToMap(
|
||||
inlineStyle = ''
|
||||
): Record<string, string> {
|
||||
if (inlineStyle === '') {
|
||||
return {}
|
||||
}
|
||||
|
||||
return inlineStyle.split(';').reduce(
|
||||
(styleObject, stylePropertyValue) => {
|
||||
// extract the style property name and value
|
||||
const [property, value] = stylePropertyValue
|
||||
.split(/^([^:]+):/)
|
||||
.filter((val, i) => i > 0)
|
||||
.map((item) => item.trim())
|
||||
|
||||
// if there is no value (i.e. no : in the style) then ignore it
|
||||
if (value === undefined) {
|
||||
return styleObject
|
||||
}
|
||||
|
||||
// convert the property name into the correct React format
|
||||
// remove all hyphens and convert the letter immediately after each hyphen to upper case
|
||||
// additionally don't uppercase any -ms- prefix
|
||||
// e.g. -ms-style-property = msStyleProperty
|
||||
// -webkit-style-property = WebkitStyleProperty
|
||||
const replacedProperty = property
|
||||
.toLowerCase()
|
||||
.replace(/^-ms-/, 'ms-')
|
||||
.replace(/-(.)/g, (_, character) => character.toUpperCase())
|
||||
|
||||
// add the new style property and value to the style object
|
||||
styleObject[replacedProperty] = value
|
||||
|
||||
return styleObject
|
||||
},
|
||||
{} as Record<string, string>
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue