0

I'm working with an API with song lyrics and print the fetched lyrics on the web page but I want them to print with line breaks like this:

Hello, it's me
I was wondering if after all these years you'd like to meet
To go over everything
They say that time's supposed to heal ya, but I ain't done much
healing

Hello, can you hear me?
I'm in California dreaming about who we used to be
When we were younger and free
I've forgotten how it felt before the world fell at our feet

There's such a difference between us
And a million miles

Hello from the other side

How can I accomplish this when I get lyrics as a string. Say the lyrics are stored in a variable for example let lyrics

I tried creating line breaks before each capital letter but then I get line breaks where I don't want them. For example, in front of each "I".

Here is my attempt to solve the task but it turns out the wrong way, not the way the original lyrics are written.

searchForm.addEventListener('submit', function (event) {
  event.preventDefault();

  const artistValue = artistInput.value;
  const titleValue = titleInput.value;

  fetch(
    `https://api.musixmatch.com/ws/1.1/matcher.lyrics.get?q_track=${titleValue}&q_artist=${artistValue}&apikey=${apiKey}`
  )
    .then((response) => response.json())
    .then((data) => {
      console.log(data);
      console.log(data.message.body.lyrics.lyrics_body);

      let lyrics = data.message.body.lyrics.lyrics_body;

      if (data) {
        let characters = lyrics.split('');

        for (var i = 0; i < characters.length; i++) {
          // Check if the character is uppercase
          if (
            characters[i] === characters[i].toUpperCase() &&
            characters[i] !== characters[i].toLowerCase()
          ) {
            // Add a line break before the uppercase character
            characters[i] = '<br>' + characters[i];
          }
        }

        let modifiedLyrics = characters.join('');

        lyricsContainer.innerHTML = modifiedLyrics;
      } else {
        lyricsContainer.textContent = `No lyrics were  found for Artist: '${artistValue}', Title: '${titleValue}'.`;
      }
    })
<form id="lyrics-search-form">
        <input id="artist" type="text" placeholder="Artist..." />
        <input id="title" type="text" placeholder="Title..." />
        <button id="search-btn" disabled>Search</button>
      </form>
      <div class="lyrics-div">
        <p id="lyrics"></p>
      </div>
Phil
  • 157,677
  • 23
  • 242
  • 245
Igor Gryp
  • 67
  • 5
  • I don't know why @David marked the other post as duplicate. As there are no '\n' inside his text? – Wimanicesir Jun 29 '23 at 13:28
  • 1
    @Wimanicesir: Copying and pasting the exact text from the OP and applying the solution in the linked duplicate results in [this](https://jsfiddle.net/0pwm263y/). If that's not what the OP is looking for then the question may need some clarity, perhaps including a [mcve] demonstrating the specific attempt and what isn't working as expected. – David Jun 29 '23 at 13:30
  • Well i'm just tripping then. Cause your solution and his desired result are not the same at all to me. – Wimanicesir Jun 29 '23 at 13:35
  • 1
    @Wimanicesir. `\n` is the escape sequence for newline, as such the pasted text the OP posted has about 7 of them. – Keith Jun 29 '23 at 14:39
  • But what text? He doesn't supply his input, only his desired result. The desired result is not the same as @David. I don't know what I'm missing.. He wants to split up a string that has no newlines.. – Wimanicesir Jun 29 '23 at 15:09
  • 1
    @Wimanicesir: Do you have edit access to questions? If so, compare what the OP entered with what is rendered. My understanding is that they want the rendered result to match the raw format of the string, which of course HTML doesn't do by default. (If my understanding is incorrect, the OP is of course highly encouraged to clarify. But this appears to have been a drive-by question.) – David Jun 29 '23 at 15:18
  • @David, You are completely right. I'm sorry, I totally missed the correct formatting. However, OP has to clarify what his input actually is. – Wimanicesir Jun 29 '23 at 15:22
  • According to the [API documentation](https://developer.musixmatch.com/documentation/api-reference/matcher-lyrics-get), the `lyrics_body` response property already contains line-breaks. You just need to format them via CSS as per the duplicate link – Phil Jul 12 '23 at 23:28

0 Answers0