]> piware.de Git - chordpro-web.git/commitdiff
Recognize directives without argument
authorMartin Pitt <martin@piware.de>
Wed, 23 Sep 2026 20:45:53 +0000 (22:45 +0200)
committerMartin Pitt <martin@piware.de>
Wed, 23 Sep 2026 20:46:11 +0000 (22:46 +0200)
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

index 0d54e55c3f2b53edd2e661d72e7e46369a75c5f9..e818d174202e67064430096ec46d5ccd58efd6af 100644 (file)
@@ -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 === '') {