-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (36 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const mainPattern = new RegExp(
'^' +
'(?:([\\d.]*\\d) *\\| *)?' + // Optional number, followed by a pipe,
'(.+) *' + // followed by a title,
'\\n=+\\n' + // followed by a line of `=` signs.
'\\n+' + // At least one blank line.
'([^]*?)' + // The song’s body,
'\\n*$' // trimmed of trailing newlines.
);
module.exports = (input) => {
const match = input.match(mainPattern);
const body = match[3];
const rawParts = body.split(/\n{3,}/);
const blocks = rawParts.map(partString => {
const lines = partString.split(/\n{2,}/);
const rawLyricsLines = lines.map((line) => (
`${
line.replace(/[^]*\n/, '')
}\n`
));
const lyricsLines = rawLyricsLines.map(line => line.replace(/^ */, ''));
const lyrics = lyricsLines.join('');
const firstLineIndent = rawLyricsLines[0].match(/^ */)[0];
const type = (firstLineIndent.length >= 4 ?
'refrain' :
'stanza'
);
return { type, lyrics };
});
return {
number: match[1],
title: match[2],
blocks,
parts: blocks, // deprecated!
};
};