plaintext-song-parser
v1.0.2
Published
Allows you to parse plaintext lyrics file contents into a Javascript object
Downloads
8
Readme
Plaintext Song Parser for JavaScript
Allows you to parse plaintext lyrics file contents into a Javascript object
Installation
- From npmjs.com
npm install plaintext-song-parser
- From Github:
npm install https://github.com/Miniontoby/plaintext-song-parser.js
- From an CDN (only for browser):
<script src="https://cdn.jsdelivr.net/npm/plaintext-song-parser/dist/plaintext-song-parser.min.js"></script>
Usage
This package ships in three types. Select your way for importing it:
- CJS (when using
require
):const Song = require('plaintext-song-parser');
- ESM (when using
"type": "module"
):import Song from 'plaintext-song-parser';
- UMD (Browser):
If you added the script tag from earlier, you are good to go, since it loads it automatically to
window.Song
After you imported the Song
class, you can start using it:
const content = `<GET YOUR CONTENT FROM SOMEWHERE>`;
const song = new Song(content);
console.log(song.couplets);
API
new Song(content: string[]|string, identifier: number|string|null = null, title: string|null = null)
Makes a new Song object and processes the supplied content. You can supply anidentifier
in order to keep track of which Song is which And if you really need to, you can also supply antitle
, but this will also be parsed from the first line of thecontent
if it starts with#
Song.content: string
Returns the content you suppliedSong.identifier: number|string|null
Returns the identifier you suppliedSong.title: string|null
Returns the title you supplied or the title that was parsedSong.coupletsWithReferences: string[][]
Returns an array containing an array of strings which is the parsed lyrics. This includes the references and can be seen as plain parsingSong.couplets: string[][]
Returns the same asSong.coupletsWithReferences
, but the references are replaced by the lyrics connected to that reference. This is the recommended property to use.Song.modifiers: {any}
Returns the modifiers that had been detected, such as titlestatic Song.getTitleFromText(content: string[]|string)
Returns the title from the text This will just runstatic Song.getModifiersFromText(content)
and then return the.title
field ornull
If no title can be found, it will returnnull
, so if you always need an string, just add?? ''
after the method...static Song.getModifiersFromText(content: string[]|string)
Returns the modifiers from the text This will be parsed from the first lines of thecontent
if it starts with#
If no comments can be found, it will return{}
. This is the place where you would find the title.
Song format
For parsing the songs, you need to supply the songtext into the content
parameter when creating the Song
object
The format for the content
parameter/the songfile, is the following:
# Put your title on the first line after an '#'
Here is the first line of your song
And here is the second line
This is a new paragraph
and this line belongs to it
If you have paragraphs that
need to be split up, just add a
[split]
and this paragraph will
belong to the same couplet
Refrain:
^^ this will create a reference
that this paragraph is named 'Refrain'
It may not include spaces!
[split]
You can also split these up
into multiple parts
[split]
And you would name them if you want
to reuse these lines of the song later.
If you want to sing this again, just add
(Refrain)
and if you need to repeat
that paragraph more than once
just add the amount and an x
after the name of the reference
(Refrain 2x)
Why
Well,
Since the beginning of 2023 I started working on SongTextProjector, which is a songtext presentation software/website/app. For that I wanted it to be as easy as it could be to make new song files and to edit them during usage of the app, so I wanted to use plain .txt files for the songs, so you could edit them using your favorite editor.
In the begining, I made a working, but bad first Song class, but it lacked a lot of parsing extra stuff. Then when I was at school and I had to do a little research project, I decided to do it about parsing songs. In the project I did three steps: Finding the best place where to get songs from, finding the most used keywords/format in songs, and making a song parser.
The document (in Dutch) is inside of this repository for you to read it if you want. (see the extra_stuff
folder)
I might translate it into English if people want to read it.
And for the record, I got an 7.2 out of 10 as grade for the research project.
This package/class is the result of that last step.