Last active
April 23, 2024 16:00
-
-
Save mrclay/45e5d8f51b16385d097b9fe7d5c24b1d to your computer and use it in GitHub Desktop.
Convert Exceljs rich text values to HTML or plaintext strings
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
function isRichValue(value) { | |
return Boolean(value && Array.isArray(value.richText)); | |
} | |
function richToString(rich) { | |
return rich.richText.map(({ text }) => text).join(''); | |
} | |
function richToHtml(rich) { | |
let str = rich.richText.map(({ font = {}, text }) => { | |
return [ | |
font.bold ? '<strong>' : '', | |
font.italic ? '<em>' : '', | |
text, | |
font.italic ? '</em>' : '', | |
font.bold ? '</strong>' : '', | |
].join(''); | |
}).join(''); | |
// simple tag combining where possible | |
return str.replace(/<\/strong><strong>/g, '').replace(/<\/em><em>/g, ''); | |
} | |
module.exports = { | |
isRichValue, | |
richToHtml, | |
richToString, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much. Saved my time.