postiz/apps/frontend/src/components/launches/helpers/use.formatting.ts

31 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import removeMd from "remove-markdown";
import {useMemo} from "react";
export const useFormatting = (text: Array<{content: string, id?: string}>, params: {
removeMarkdown?: boolean,
saveBreaklines?: boolean,
specialFunc?: (text: string) => string,
}) => {
return useMemo(() => {
return text.map((value) => {
let newText = value.content;
if (params.saveBreaklines) {
newText = newText.replace('\n', '𝔫𝔢𝔴𝔩𝔦𝔫𝔢');
}
if (params.removeMarkdown) {
newText = removeMd(value.content);
}
if (params.saveBreaklines) {
newText = newText.replace('𝔫𝔢𝔴𝔩𝔦𝔫𝔢', '\n');
}
if (params.specialFunc) {
newText = params.specialFunc(newText);
}
return {
id: value.id,
text: newText,
count: params.removeMarkdown && params.saveBreaklines ? newText.replace(/\n/g, ' ').length : newText.length,
}
});
}, [text]);
}