All files / app/component/md md.component.ts

77.41% Statements 24/31
70% Branches 14/20
80% Functions 4/5
76% Lines 19/25

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 7867x         67x                         67x     5x       5x   5x   5x   5x   5x             5x       5x 5x     5x 5x 5x                 4x 4x       18x                 18x        
import { Component, ElementRef, Input, Output } from '@angular/core';
import { MermaidConfig } from 'mermaid';
import { MarkdownComponent, MermaidAPI } from 'ngx-markdown';
import { Subject } from 'rxjs';
import * as XLSX from 'xlsx';
import { MdPostDirective } from '../../directive/md-post.directive';
import { AdminService } from '../../service/admin.service';
import { Store } from '../../store/store';
 
@Component({
  selector: 'app-md',
  templateUrl: './md.component.html',
  styleUrls: ['./md.component.scss'],
  imports: [
    MarkdownComponent,
    MdPostDirective,
  ]
})
export class MdComponent {
 
  @Input()
  origin? = '';
  @Input()
  plugins?: string[];
  @Input()
  disableSanitizer = false;
  @Output()
  postProcessMarkdown: Subject<void> = new Subject();
  @Input()
  mermaid = true;
  @Input()
  clipboard = true;
 
  katexOptions = {
    throwOnError: false,
    delimiters: [
      {left: "$$", right: "$$", display: true},
      {left: "$", right: "$", display: false},
    ],
  };
  mermaidOptions: MermaidConfig & MermaidAPI.MermaidConfig = {
    theme: this.store.darkTheme ? 'dark' : 'default',
  };
 
  private _text = '';
  private _value? = '';
 
  constructor(
    public admin: AdminService,
    public store: Store,
    public el: ElementRef,
  ) { }
 
  get text(): string {
    return this._text;
  }
 
  @Input()
  set text(value: string | undefined) {
    this._text = value || '';
    delete this._value;
  }
 
  get value() {
    Iif (this.plugins?.includes('plugin/table')) {
      if (this._value) return this._value;
      try {
        const wb = XLSX.read(this._text, {type: 'string'});
        return this._value = XLSX.utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]], {header: ''});
      } catch (e: any) {
        return `<p class="error">${e.message}</p>`
      }
    }
    return this._value = this._text;
  }
 
}