-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable rhyme copy-paste; jsonify errors
- Loading branch information
1 parent
28b7cbb
commit 8826032
Showing
7 changed files
with
184 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* As title. ChatGPT wrote this. | ||
* | ||
* @param {string} html | ||
* @returns {string} | ||
*/ | ||
export const convertHtmlToRtf = (html: string): string => { | ||
let rtfHeader = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\nouicompat\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}"; | ||
let colorTable = "{\\colortbl ;"; | ||
let content = "\\viewkind4\\uc1\\pard\\f0\\fs22 "; | ||
let colorMap: Record<string, number> = {}; | ||
|
||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(html, 'text/html'); | ||
const spans = doc.querySelectorAll('span'); | ||
let colorIndex = 1; | ||
|
||
spans.forEach((span: HTMLSpanElement) => { | ||
let bgColor = span.style.backgroundColor; | ||
let rtfColorIndex = 0; | ||
|
||
if (bgColor && bgColor !== 'inherit' && bgColor.startsWith('rgb')) { | ||
// Convert RGB CSS color to RTF color table entry and index | ||
if (!colorMap[bgColor]) { | ||
const rgbMatch = bgColor.match(/\d+/g); // Extract RGB values | ||
if (rgbMatch) { | ||
const [r, g, b] = rgbMatch.map(Number); | ||
colorTable += `\\red${r}\\green${g}\\blue${b};`; | ||
colorMap[bgColor] = colorIndex++; | ||
} | ||
} | ||
rtfColorIndex = colorMap[bgColor]; | ||
} | ||
|
||
const text = span.textContent?.replace(/\n/g, '\\par\n') || ''; | ||
content += rtfColorIndex > 0 ? `\\highlight${rtfColorIndex} ${text}` : `${text}`; | ||
content += " "; | ||
}); | ||
|
||
const rtfContent = `${rtfHeader}${colorTable}}${content}\\cf0\\highlight0 }`; | ||
|
||
console.log({ html, rtfContent }); | ||
return rtfContent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters