From 9e0a0a71796282b40d24c068b9d73f9ab43d112c Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Wed, 23 Sep 2026 22:44:59 +0200 Subject: [PATCH] Flush open verse on directives and at end of file Verses were only emitted on a blank line. A directive directly after a lyrics line, such as {comment: Chorus}, thus appeared before that verse, and a file without a trailing newline lost its last verse. --- chordpro-web.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/chordpro-web.js b/chordpro-web.js index abbf404..0d54e55 100644 --- a/chordpro-web.js +++ b/chordpro-web.js @@ -19,10 +19,18 @@ const directive_handlers = { }; function renderChordpro(text) { - let verseLines = null; + let verseLines = []; let verseHasChords = false; song.length = 0; + const flushVerse = () => { + if (verseLines.length === 0) + return; + song.push(html`

${verseLines}

`); + verseLines = []; + verseHasChords = false; + }; + for (let line of text.split('\n')) { line = line.trim(); if (line.startsWith('#')) @@ -30,6 +38,7 @@ function renderChordpro(text) { let m; if ((m = re_directive.exec(line)) !== null) { + flushVerse(); const [_, directive, value] = m; const handler = directive_handlers[directive]; if (handler) @@ -37,15 +46,8 @@ function renderChordpro(text) { else console.warn('unknown directive:', line); } else if (line === '') { - if (verseLines == null) - continue; - song.push(html`

${verseLines}

`); - verseLines = null; - verseHasChords = false; + flushVerse(); } else { - if (verseLines == null) - verseLines = []; - // split line into segments of one chord plus the lyrics up to the // next chord; the first element is the lyrics before the first chord const [lead, ...rest] = line.split(/\[([^\]]+)\]/); @@ -65,6 +67,7 @@ function renderChordpro(text) { verseLines.push(segments); } } + flushVerse(); render(song, el_song); } -- 2.47.3