Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/parser/chords_over_words/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ interface NoChord {
column: number,
}

type ChordsLineItem = Chord | RhythmSymbol | NoChord;

type DirectionLine = SerializedLine;
type InlineMetadata = SerializedLine;

interface ChordsLine {
type: 'chordsLine',
items: (Chord | RhythmSymbol | NoChord)[]
items: ChordsLineItem[]
}

interface LyricsLine {
Expand Down Expand Up @@ -70,8 +72,17 @@ function chordProperties(chord: Chord): ChordProperties {
return properties;
}

function getChordData(chord: Chord) {
return (chord.type === 'chord') ? { chord: chordProperties(chord) } : { chords: chord.value };
function getChordData(item: ChordsLineItem) {
switch (item.type) {
case 'chord':
return { chord: chordProperties(item) };
case 'symbol':
return { chords: item.value, isRhythmSymbol: true };
case 'noChord':
return { chords: item.value };
default:
throw new Error(`Unexpected chordsLine item ${item}`);
}
}

function buildSoftLineBreakResult(
Expand All @@ -93,8 +104,8 @@ function buildSoftLineBreakResult(
}

function buildChordLyricsPairForChord(
chord: Chord,
nextChord: Chord | undefined,
chord: ChordsLineItem,
nextChord: ChordsLineItem | undefined,
lyrics: string,
chopFirstWord: boolean,
): (SerializedChordLyricsPair | SerializedSoftLineBreak)[] | SerializedChordLyricsPair {
Expand All @@ -121,7 +132,7 @@ function buildChordLyricsPairForChord(
}

function constructChordLyricsPairs(
chords: Chord[],
chords: ChordsLineItem[],
lyrics: string,
chopFirstWord: boolean,
): (SerializedChordLyricsPair | SerializedSoftLineBreak)[] {
Expand All @@ -137,7 +148,7 @@ function pairChordsWithLyrics(
): SerializedLine {
const { content: lyrics } = lyricsLine;

const chords = chordsLine.items as Chord[];
const chords = chordsLine.items;
const chordLyricsPairs = constructChordLyricsPairs(chords, lyrics, chopFirstWord);
const firstChord = chords[0];

Expand Down
17 changes: 17 additions & 0 deletions test/parser/chords_over_words_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ describe('ChordsOverWordsParser', () => {
expect(line6Pairs[4]).toBeChordLyricsPair('/', '');
});

it('preserves rhythm symbols in chords over words lines', () => {
const chordOverWords = heredoc`
N.C. Am C -
Hey, Jude, don't make it bad`;

const parser = new ChordsOverWordsParser();
const song = parser.parse(chordOverWords, { chopFirstWord: false });
const { lines } = song;

const linePairs = lines[0].items;
expect(linePairs[0]).toBeChordLyricsPair('N.C.', 'Hey, ');
expect(linePairs[1]).toBeChordLyricsPair('Am', 'Jude, ');
expect(linePairs[2]).toBeChordLyricsPair('C', 'don\'t make it bad');
expect(linePairs[3]).toBeChordLyricsPair('-', '');
expect((linePairs[3] as ChordLyricsPair).isRhythmSymbol).toBe(true);
});

it('parses comment without a ":"', () => {
const chordOverWords = heredoc`
title: Let it be
Expand Down