From c2eb8c641909cdf2dccc7968cd09dcec7724d08c Mon Sep 17 00:00:00 2001 From: kwaroran Date: Wed, 19 Jun 2024 03:20:54 +0900 Subject: [PATCH] fix: add null checking --- src/ts/plugins/automark.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/ts/plugins/automark.ts b/src/ts/plugins/automark.ts index 7fb74c1a..cfee0bc4 100644 --- a/src/ts/plugins/automark.ts +++ b/src/ts/plugins/automark.ts @@ -27,8 +27,13 @@ export function risuFormater(dat:string){ let htmlType = 0 // 0: not inside tag, 1: closing tag, 2: opening tag for(let i=0;i { + return lines[lines.length-1] ?? [ + 'not-found', '' + ] + } //html tag handling - if(dat[i] === '<' && lines[lines.length-1][0] !== 'code-block'){ + if(dat[i] === '<' && getLastLine()[0] !== 'code-block'){ lines.push(['html-tag','']) if(dat[i+1] === '/'){ htmlType = 1 @@ -38,29 +43,29 @@ export function risuFormater(dat:string){ } } - if(dat[i] === '>' && lines[lines.length-1][0] === 'html-tag'){ + if(dat[i] === '>' && getLastLine()[0] === 'html-tag'){ const pop = lines.pop() const tagAttr = pop[1].substring(1).trim() if(htmlType === 1){ const pop2 = lines.pop() //probably html-inner const chunk = pop2[1] + pop[1] + '>' - if(lines[lines.length-1][0] === ''){ + if(getLastLine()[0] === ''){ lines.push(['html-chunk',chunk]) lines.push(['','']) } else{ - lines[lines.length-1][1] += chunk + getLastLine()[1] += chunk } continue } else if(checkSelfClosingTag(tagAttr)){ const chunk = pop[1] + '>' - if(lines[lines.length-1][0] === ''){ + if(getLastLine()[0] === ''){ lines.push(['html-chunk',chunk]) lines.push(['','']) } else{ - lines[lines.length-1][1] += chunk + getLastLine()[1] += chunk } continue } @@ -72,9 +77,9 @@ export function risuFormater(dat:string){ //code block handling - if(dat[i] === '`' && dat[i+1] === '`' && dat[i+2] === '`' && lines[lines.length-1][0] === ''){ - if(lines[lines.length-1][0] === 'code-block'){ - lines[lines.length-1][1] += '```' + if(dat[i] === '`' && dat[i+1] === '`' && dat[i+2] === '`' && getLastLine()[0] === ''){ + if(getLastLine()[0] === 'code-block'){ + getLastLine()[1] += '```' lines.push(['','']) } else{ @@ -85,11 +90,11 @@ export function risuFormater(dat:string){ } - if(dat[i] === '\n' && lines[lines.length-1][0] === ''){ + if(dat[i] === '\n' && getLastLine()[0] === ''){ lines.push(['newline','\n']) lines.push(['','']) } - else{ + else if(lines[lines.length-1]){ lines[lines.length-1][1] += dat[i] } } @@ -101,7 +106,7 @@ export function risuFormater(dat:string){ continue } - let line = lines[i][1] + let line = lines[i][1] ??'' let isNumbered = false let endMarked = false if(excludesDat.includes(line[0]) || (line[1] === '.' && ['1','2','3','4','5','6','7','8','9'].includes(line[0]))){