Skip to content

Commit fc88b7f

Browse files
committed
➕️ Add plain text content utilityfor songs
1 parent fabd480 commit fc88b7f

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎frontend/src/utils.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,38 @@ function parsedContent(content: string, keyOffset: number, showChords: boolean,
316316
}
317317
};
318318

319+
// build plain text content for a song's songsheet: header (title [key]), lyrics with chord lines optionally
320+
// stripped and verse markers numbered/stripped, and an authors/copyright footer
321+
const songPlainTextContent = (song: SongEntity, tuning: string, showChords: boolean): string => {
322+
// add header
323+
var content = song.title + ' [' + tuning + ']' + '\n\n';
324+
var lines = song.content.split('\n');
325+
// process lines
326+
for (var i = 0; i < lines.length; i++) {
327+
var line = lines[i];
328+
// handle chord line
329+
if (!showChords && isChordLine(line)) continue;
330+
// handle verse marker indentation
331+
if (line.trim().toLowerCase().indexOf('--v') >= 0 && !isNaN(parseInt(line.trim().charAt(3)))) {
332+
// if next line is chord line, prepend number to the line after
333+
if (isChordLine(lines[i+1])) {
334+
lines[i+2] = line.trim().charAt(3) + '. ' + lines[i+2];
335+
// add 3 spaces to next line to sync chords with text again
336+
lines[i+1] = ' ' + lines[i+1];
337+
} else {
338+
lines[i+1] = line.trim().charAt(3) + '. ' + lines[i+1];
339+
}
340+
}
341+
// handle marker
342+
if (line.trim().indexOf('--') >= 0) continue;
343+
// keep line for export
344+
content += line + '\n';
345+
}
346+
content += '\n' + song.authors?.join(', ') + '\n\n'
347+
+ '© ' + (song.year ? song.year + ' ' : '') + song.publisher.replace(/(?:\r\n|\r|\n)/g, '; ');
348+
return content;
349+
};
350+
319351
// file download
320352
const download = (data: string | Blob, filename: string, isBlob = false): void => {
321353
var a = document.createElement('a');
@@ -524,6 +556,7 @@ export {
524556
bufferAndDefer,
525557
maximizePresentFontsize,
526558
parsedContent,
559+
songPlainTextContent,
527560
download,
528561
humanDate,
529562
humanFileSize,

0 commit comments

Comments
 (0)