diff --git a/src/lib/parseRichText.ts b/src/lib/parseRichText.ts index 17f4475..a1bfb6b 100644 --- a/src/lib/parseRichText.ts +++ b/src/lib/parseRichText.ts @@ -1,6 +1,8 @@ import sanitizeHtml from "sanitize-html"; import { env } from "$env/dynamic/public"; +const isNonEmptyString = (s: string) => s && !s.match(/^\s*$/); + const parseRichText = (richText) => { if (richText) { return richText @@ -80,16 +82,53 @@ const parseRichText = (richText) => { case "upload": { const value = node.value; const src = env.PUBLIC_SERVER_URL + value.url; + + let maxWidth; + let maxHeight; + let isNonImage = false; + let buttonText; + if (node.fields) { + if (node.fields.maxImageSize) { + maxWidth = node.fields.maxImageSize.maxWidth; + maxHeight = node.fields.maxImageSize.maxHeight; + } if (node.fields.isNonImage) { - return `
`; + isNonImage = true; + buttonText = node.fields.buttonText; } } - if (value.mimeType.startsWith("image/")) { - return `${value.alt}`; + if ( + !value.mimeType.startsWith("image/") || + isNonImage + ) { + let text = ""; + if ( + !isNonEmptyString(value.alt) && + !isNonEmptyString(buttonText) + ) + text = "Изтегли"; + else if ( + !isNonEmptyString(value.alt) && + isNonEmptyString(buttonText) + ) { + text = buttonText; + } else { + text = value.alt; + } + return `
`; } else { - if (value.alt.match(/^\s*$/)) value.alt = "Изтегли"; - return `
`; + let style = ""; + + if (maxWidth) { + style += `max-width: ${maxWidth}px; `; + } else { + style += "max-width: 100%; "; + } + if (maxHeight) { + style += `max-height: ${maxHeight}px;`; + } + return `${value.alt}`; } } case "indent": {