martijnversluis / ChordSheetJS

A JavaScript library for parsing and formatting ChordPro chord sheets
GNU General Public License v2.0
311 stars 48 forks source link

Chord cannot be parsed when using ChordSheetParser #494

Closed Gonzalo2683 closed 2 years ago

Gonzalo2683 commented 2 years ago

When using new ChordSheetJS.ChordSheetParser() and traversing its lines to process chords, some chord values cannot be parsed.

You could also transform from ChordSheetParser to ChordProParser similar to what you do with the convertChordSheetToChordPro function in chordFiddle and the problem would be avoided, but that missing hole would remain.

I don't know if this is by design, or if it has a problem, based on the chordFiddle example, the following can be shown:

With: "chordsheetjs": "^6.3.0"

const chordSheet = `
       Am         C/G        F          C
Let it be, let it be, let it be, let it be
C                G              F  C/E Dm C
Whisper words of wisdom, let it be`.substring(1);

Starting from:

import ChordSheetJS, { Chord } from "chordsheetjs";
const song = new ChordSheetJS.ChordSheetParser().parse(chordSheet);
......
.....
..... 
CODE ABREVIATED
......
const processChord = (item, processor) => {
  if (item instanceof ChordSheetJS.ChordLyricsPair && item.chords) {
    const parsedChord = Chord.parse(item.chords); // HERE, PARSING

    if (parsedChord) {
      const processedChordLyricsPair = item.clone();
      processedChordLyricsPair.chords = processor(parsedChord).toString();
      return processedChordLyricsPair;
    }
  }
  return item;
};

The parsed chord value is null, because of apparently having trailing whitespace. chord_error_parsing

Here's another iteration where the chord has no whitespace chord_parse_ok

martijnversluis commented 2 years ago

Hey @Gonzalo2683. Thanks for following up.

I might think about whether to loosen up the chord parsing by discarding leading or trailing whitespace.

For the time being you can work around it in two ways:

Please let me know if this is helpful.

Gonzalo2683 commented 2 years ago

Thanks, I think what you mention would be enough, I'm still learning how the API works, I only mentioned it out of doubt that those blank spaces influence the final alignment of the chords on the lyrics, but apparently it doesn't.