format.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export function getFormattedContext(formattedContext){
  2. let formatArray = JSON.parse(formattedContext);
  3. // Correct formatting style where needed
  4. formatArray.forEach(
  5. formatObj => {
  6. formatObj.cssStyles = [];
  7. let formatting = formatObj.formatting;
  8. if (formatting.includes('grassetto')){
  9. formatObj.cssStyles.push('bold');
  10. }
  11. if (formatting.includes('corsivo')) {
  12. formatObj.cssStyles.push("italic");
  13. }
  14. // Handle 'barrato' and 'sottolineato' together!
  15. if (formatting.includes('sottolineato') && formatting.includes('barrato')) {
  16. formatObj.cssStyles.push("underlinethrough");
  17. }
  18. else if (formatting.includes('sottolineato')) {
  19. formatObj.cssStyles.push("underline");
  20. }
  21. else if (formatting.includes('barrato')) {
  22. formatObj.cssStyles.push("linethrough");
  23. }
  24. if (formatting.includes('evidenziato')) {
  25. formatObj.cssStyles.push("highlight")
  26. }
  27. }
  28. );
  29. return combineStringPartsWithStyles(formatArray);
  30. }
  31. export function combineStringPartsWithStyles(stringPartArray){
  32. let formattedStringParts = stringPartArray.map(stringPartWithstyle => {
  33. let classString = 'class="' + stringPartWithstyle.cssStyles.join(" ") + '"';
  34. return '<span ' + classString + '>' + stringPartWithstyle.stringPart + '</span>'
  35. })
  36. let outString = formattedStringParts.join('');
  37. return outString.replaceAll('\n', '<br>');
  38. }