diff --git a/src/fromRedactor.tsx b/src/fromRedactor.tsx index 525f27a..68fd671 100644 --- a/src/fromRedactor.tsx +++ b/src/fromRedactor.tsx @@ -863,7 +863,7 @@ export const fromRedactor = (el: any, options?:IHtmlToJsonOptions) : IAnyObject } let noOfInlineElement = 0 Array.from(el.parentNode?.childNodes || []).forEach((child: any) => { - if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline'))) { + if (child.nodeType === 3 || child.nodeName === 'SPAN' || child.nodeName === 'A' || (options?.allowNonStandardTags && child.getAttribute('inline')) || child.nodeName in TEXT_TAGS) { noOfInlineElement += 1 } }) @@ -1084,4 +1084,26 @@ function getTbodyChildren (rows: any[]) { }, []) return newTbodyChildren +} + +export function replaceNonSemanticTags (el: HTMLElement) { + const nonSematicTagMap = { + b: 'strong', + i: 'em' + } as Record + + const nodes = el.querySelectorAll(Object.keys(nonSematicTagMap).join(',')) + + nodes.forEach((node) => { + const nodeName = node.nodeName.toLowerCase() + if(!(nodeName in nonSematicTagMap)) return + + const strong = el.ownerDocument.createElement(nonSematicTagMap[nodeName]) + strong.innerHTML = node.innerHTML + Array.from(node.attributes).forEach((attr) => { + if(attr.nodeValue) + strong.setAttribute(attr.nodeName, attr.nodeValue) + }) + node.replaceWith(strong) + }) } \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index d1e48ff..d86d512 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,6 +1,12 @@ import "array-flat-polyfill" -import { fromRedactor } from "./fromRedactor" +import { fromRedactor, replaceNonSemanticTags } from "./fromRedactor" import { toRedactor } from "./toRedactor" import {jsonToMarkdownSerializer} from './jsonToMarkdown' +import { IHtmlToJsonOptions } from "./types" export * from "./types" -export { fromRedactor as htmlToJson, toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown } \ No newline at end of file +export { toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown } + +export const htmlToJson = (el: any, options?:IHtmlToJsonOptions) => { + replaceNonSemanticTags(el) + return fromRedactor(el, options) +} \ No newline at end of file