All files / lib/internal/text-formatter text-formatter.component.ts

94.29% Statements 215/228
100% Branches 34/34
80% Functions 12/15
94.29% Lines 215/228

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 2291x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x       25x 25x 27x 27x 27x 25x 25x 25x 25x 25x 25x 1x 1x 41x 41x 41x 41x 41x 41x 12x 12x 12x 41x 41x 12x 12x 44x 44x 42x 44x 12x 12x 12x 12x 12x 12x 12x 12x 2x 41x 41x 41x 41x 41x 1x 1x 29x 29x 29x 29x 27x 27x 27x 27x 29x 29x 2x 2x 4x 4x 4x 2x 2x 2x 2x 29x 1x 1x 33x 33x 33x 33x 33x 33x 31x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 33x 1x 1x 196x 196x 196x 196x 196x 196x 149x 149x 47x 47x 47x 37x 47x 47x 22x 25x 25x 47x 47x 47x 47x 47x 47x 37x 47x 47x 47x 196x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import { Component, Directive, Input, TemplateRef } from '@angular/core';
 
import { Typography } from '../../typography/typography.directive';
 
type FormattedTextSection = (
  | { kind: 'p'; text: FormattedText }
  | { kind: 'plain'; text: FormattedText }
  | { kind: 'ul'; liArray: FormattedText[] }
)[];
 
type FormattedText = (
  | { kind: 'text'; text: string }
  | { kind: 'bold'; text: FormattedText }
  | { kind: 'italic'; text: FormattedText }
  | { kind: 'br' }
)[];
 
const MDList = /(?<list>^(?:\* .*\n*)+)/;
const MDBold = /\*\*(?<bold>\*.+?\*|.+?)\*\*/;
const MDItalic = /\*(?<italic>.+?)\*/;
const MDEmptyLine = /(?<br>\n(?!(\*\s.)))/;
 
interface FormattedTextContext {
  $implicit: FormattedText;
}
 
interface FormattedSectionContext {
  $implicit: FormattedTextSection;
}
 
/**
 * Typeguard for the formatted text data structure
 */
@Directive({
  selector: 'ng-template[data-gc-formatted-section]',
  standalone: false,
})
export class SectionFormatterDirective {
  constructor(public readonly template: TemplateRef<FormattedSectionContext>) {}
 
  /**
   * @ignore
   */
  static ngTemplateContextGuard(
    _dir: SectionFormatterDirective,
    ctx: unknown,
  ): ctx is FormattedSectionContext {
    return true;
  }
}
 
/**
 * Typeguard for the formatted text data structure
 */
@Directive({
  selector: 'ng-template[data-gc-formatted-text]',
  standalone: false,
})
export class TextFormatterDirective {
  constructor(public readonly template: TemplateRef<FormattedTextContext>) {}
 
  /**
   * @ignore
   */
  static ngTemplateContextGuard(
    _dir: TextFormatterDirective,
    ctx: unknown,
  ): ctx is FormattedTextContext {
    return true;
  }
}
 
/**
 * Text formatter component that is used to convert a markdown text into HTML text
 * Supported formats: bold text, italic text, unordered list (one level only)
 */
@Component({
  selector: 'gc-text-formatter',
  templateUrl: './text-formatter.component.html',
  styleUrls: [
    '../../utils/styles/reset-list.css',
    './text-formatter.component.css',
  ],
  standalone: false,
})
export class TextFormatterComponent {
  /**
   * Sets data-gc-typography
   * Default: 'label'
   */
  @Input()
  public typography: Typography = 'label';
 
  /**
   * Input text in markdown format that will get converted and rendered
   */
  @Input()
  public get markdownText(): string {
    return this._markdownText;
  }
 
  public set markdownText(newText: string) {
    this._markdownText = newText;
    this.formattedText = this.convertList(newText);
  }
 
  /** @ignore */
  protected formattedText: FormattedTextSection = [];
 
  /** @ignore */
  private _markdownText = '';
 
  private convertList(text: string): FormattedTextSection {
    const mdReg = new RegExp(MDList.source, 'm');
 
    const match: ReturnType<typeof mdReg.exec> = mdReg.exec(text);
    if (match === null) {
      return this.convertParagpraphs(text);
    } else {
      let parts: FormattedTextSection = [];
      if (match.index > 0) {
        parts = [...parts, ...this.convertList(text.slice(0, match.index))];
      }
      if (match.groups?.list) {
        const enumList: FormattedText[] = [];
        match[0].split('\n').forEach(line => {
          const textonly = line.replace('* ', '').trim();
          if (textonly.length > 0) {
            enumList.push(this.convert(textonly));
          }
        });
        parts.push({ kind: 'ul', liArray: enumList });
      }
 
      text = text.slice(match.index + match[0].length);
 
      // store text following after the last match
      if (text.length > 0) {
        parts = [...parts, ...this.convertList(text)];
      }
 
      return parts;
    }
  }
 
  private convertParagpraphs(text: string): FormattedTextSection {
    const trimmedText = text.trim();
    const splitText = trimmedText.split(/\n{2,}/);
    if (splitText.length < 2) {
      return [
        {
          kind: 'plain',
          text: this.convertEmptyLines(trimmedText),
        },
      ];
    } else {
      const paragraphs: FormattedTextSection = [];
      splitText.forEach(s =>
        paragraphs.push({
          kind: 'p',
          text: this.convertEmptyLines(s),
        }),
      );
      return paragraphs;
    }
  }
 
  private convertEmptyLines(text: string): FormattedText {
    let parts: FormattedText = [];
 
    const mdReg = new RegExp(MDEmptyLine.source, 'm');
    const match: ReturnType<typeof mdReg.exec> = mdReg.exec(text);
 
    if (match === null) {
      return this.convert(text);
    } else {
      // store text before the first match
      if (match.index > 0) {
        parts = [...parts, ...this.convert(text.slice(0, match.index))];
      }
      if (match.groups?.br) {
        parts.push({ kind: 'br' });
      }
 
      text = text.slice(match.index + match[0].length);
      // store text following after the last match
      if (text.length > 0) {
        parts = [...parts, ...this.convertEmptyLines(text)];
      }
    }
    return parts;
  }
 
  private convert(text: string): FormattedText {
    let parts: FormattedText = [];
 
    const mdReg = new RegExp(MDBold.source + '|' + MDItalic.source, 'm');
    const match: ReturnType<typeof mdReg.exec> = mdReg.exec(text);
 
    if (match === null) {
      parts.push({ kind: 'text', text: text });
      return parts;
    } else {
      // store text before the first match
      if (match.index > 0) {
        parts = [...parts, ...this.convert(text.slice(0, match.index))];
      }
      if (match.groups?.bold) {
        parts.push({ kind: 'bold', text: this.convert(match.groups.bold) });
      } else if (match.groups?.italic) {
        parts.push({ kind: 'italic', text: this.convert(match.groups.italic) });
      }
 
      text = text.slice(match.index + match[0].length);
 
      // store text following after the last match
      if (text.length > 0) {
        parts = [...parts, ...this.convert(text)];
      }
    }
    return parts;
  }
}