import { Component , NgZone , OnInit } from '@angular/core' ; import { MatDialogRef } from '@angular/material/dialog' ; import * as marked from 'marked' ; import { ApplicationService } from "@services/electron/application.service" ; const fs = fsLib ; @ Component ( { templateUrl : './changelog.component.html' , styleUrls : [ './changelog.component.scss' ] } ) export class ChangeLogComponent implements OnInit { public versions : Array < string > ; public versionList : Array < Object > ; public versionContent : string ; public versionNumber : string ; public versionDate : Date ; public lexedContent : Array < Object > ; constructor ( public dialogRef , private zone , private applicationService ) { } ngOnInit ( ) { marked . setOptions ( { gfm : true , tables : true , breaks : true , sanitize : true , smartLists : true , smartypants : true } ) ; fs . readFile ( this . applicationService . appPath + '/CHANGELOG.md' , { encoding : 'utf-8' } , ( err , data ) => { this . zone . run ( ( ) => { this . lexedContent = marked . lexer ( data ) ; this . populateVersion ( ) ; this . populateList ( ) ; this . changeVersion ( this . versions [ 0 ] ) ; } ) ; } ) ; } private populateVersion ( ) { const filteredContent = this . lexedContent . filter ( ( node ) => { return ( node . depth == 2 && node . type == "heading" ) ; } ) ; let nodeVersion ; const versionArray = [ ] ; for ( nodeVersion of filteredContent ) { if ( ! nodeVersion . text . startsWith ( '⚠' ) ) { versionArray . push ( nodeVersion . text ) ; } } this . versions = versionArray ; } private populateList ( ) { let version ; const versionArray = [ ] ; for ( version of this . versions ) { const splitedVersion = version . split ( " - " ) ; const versionNumber = splitedVersion [ 0 ] . replace ( /^[\[]+|[\]]+$/g , "" ) ; const versionDate = new Date ( splitedVersion [ 1 ] ) ; versionArray . push ( { number : versionNumber , date : versionDate } ) ; } this . versionList = versionArray ; } public selectVersion ( $event , version ) { this . changeVersion ( version ) ; const old = document . querySelector ( ".tab-bar-item.selected" ) ; if ( old !== undefined ) { old . classList . remove ( "selected" ) ; } const target = $event . target . classList . add ( "selected" ) ; } private changeVersion ( version ) { let node ; let record = false ; const result = [ ] ; for ( node of this . lexedContent ) { if ( node [ "depth" ] == 2 && node [ "type" ] == "heading" && node [ "text" ] . indexOf ( version ) >= 0 ) { const splitedVersion = node [ "text" ] . split ( " - " ) ; this . versionNumber = splitedVersion [ 0 ] . replace ( /^[\[]+|[\]]+$/g , "" ) ; this . versionDate = new Date ( splitedVersion [ 1 ] ) ; record = true ; } if ( node [ "depth" ] == 2 && node [ "type" ] == "heading" && node [ "text" ] . indexOf ( version ) == - 1 ) { record = false ; } if ( record == true ) { if ( node [ "depth" ] != 2 || node [ "type" ] != "heading" ) { result . push ( node ) ; } } } result [ "links" ] = { } ; this . versionContent = marked . parser ( result ) ; } }