From 587cad100cb6397f21fac9a7c728e4359b4458c1 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Wed, 23 Sep 2026 22:45:53 +0200 Subject: [PATCH] Recognize directives without argument Directives like `{start_of_chorus}` or `{eoc}` did not match the directive pattern, so they were rendered literally as lyrics. They now get the same "unknown directive" warning as other unsupported ones. --- chordpro-web.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/chordpro-web.js b/chordpro-web.js index 0d54e55..e818d17 100644 --- a/chordpro-web.js +++ b/chordpro-web.js @@ -1,6 +1,7 @@ import { html, render } from 'https://cdn.jsdelivr.net/gh/lit/dist@3.3.3/core/lit-core.min.js'; -const re_directive = /^{(\w+):\s*([^}]+)}$/; +// {name} or {name: argument} +const re_directive = /^\{(\w+)(?::\s*(.*?))?\s*\}$/; const el_song = document.getElementById('song'); const song = []; @@ -36,13 +37,13 @@ function renderChordpro(text) { if (line.startsWith('#')) continue; - let m; - if ((m = re_directive.exec(line)) !== null) { + const m = re_directive.exec(line); + if (m) { flushVerse(); - const [_, directive, value] = m; + const [, directive, value] = m; const handler = directive_handlers[directive]; if (handler) - handler(value); + handler(value ?? ''); else console.warn('unknown directive:', line); } else if (line === '') { -- 2.47.3