Skip to content

Commit

Permalink
refactor: add simpler support for b tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayesh2812 committed Nov 18, 2024
1 parent 8767cea commit d072862
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 30 deletions.
23 changes: 1 addition & 22 deletions src/fromRedactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const TEXT_TAGS: IHtmlToJsonTextTags = {
I: () => ({ italic: true }),
S: () => ({ strikethrough: true }),
STRONG: () => ({ bold: true }),
B: () => ({ bold: true }),
U: () => ({ underline: true }),
SUP: () => ({ superscript: true }),
SUB: () => ({ subscript: true })
Expand Down Expand Up @@ -1085,25 +1086,3 @@ function getTbodyChildren (rows: any[]) {
}, [])
return newTbodyChildren
}

export function replaceNonSemanticTags (el: HTMLElement) {
const nonSematicTagMap = {
b: 'strong',
i: 'em'
} as Record<string, string>

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)
})
}
10 changes: 2 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import "array-flat-polyfill"
import { fromRedactor, replaceNonSemanticTags } from "./fromRedactor"
import { fromRedactor } from "./fromRedactor"
import { toRedactor } from "./toRedactor"
import {jsonToMarkdownSerializer} from './jsonToMarkdown'
import { IHtmlToJsonOptions } from "./types"
export * from "./types"
export { toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown }

export const htmlToJson = (el: any, options?:IHtmlToJsonOptions) => {
replaceNonSemanticTags(el)
return fromRedactor(el, options)
}
export { fromRedactor as htmlToJson, toRedactor as jsonToHtml, jsonToMarkdownSerializer as jsonToMarkdown }

0 comments on commit d072862

Please sign in to comment.