Is it feasible to indent lines in a template without affecting the content alignment?

Creating a string with newlines that will be included in an email later.

    if (action) {
      description = `
Git pull request action:        ${action}
Git pull request for repo:      ${req.body.repository.full_name}
Git pull request for repo URL:  ${req.body.repository.html_url}

Git pull request title:         ${req.body.pull_request.title}
Git pull request description:   ${req.body.pull_request.body}
Git pull request by user:       ${req.body.pull_request.user.login}
Git pull request URL:           ${req.body.pull_request.html_url}
`
    };

If I indent the lines like this:

    if (action) {
      description = `
        Git pull request action:        ${action}
        Git pull request for repo:      ${req.body.repository.full_name}
        Git pull request for repo URL:  ${req.body.repository.html_url}

        Git pull request title:         ${req.body.pull_request.title}
        Git pull request description:   ${req.body.pull_request.body}
        Git pull request by user:       ${req.body.pull_request.user.login}
        Git pull request URL:           ${req.body.pull_request.html_url}
     `
    };

The output also gets indented accordingly.

Inquiry: Is there a method to indent the lines without affecting the output indentation?

Answer №1

At this moment, the task cannot be accomplished.

Yet, there exists a TC39 proposal to alter this situation (Still in draft phase).
Thus, it is possible that it might become achievable in the coming days.

Meanwhile, you can make use of the zero-dependency dedent library specifically designed for this purpose.

let dedent = require("dedent");

// ...

if (action) {
  description = dedent`
    Git pull request action:        ${action}
    Git pull request for repo:      ${req.body.repository.full_name}
    Git pull request for repo URL:  ${req.body.repository.html_url}

    Git pull request title:         ${req.body.pull_request.title}
    Git pull request description:   ${req.body.pull_request.body}
    Git pull request by user:       ${req.body.pull_request.user.login}
    Git pull request URL:           ${req.body.pull_request.html_url}
  `
};

This library handles various types of line-breaks, empty lines, escaped back-ticks, and ensures consistency in all other aspects of indentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: When trying to initialize, an unexpected { token was encountered

Issue: While updating my Node.js version, I inadvertently broke my Node.js program. The problem is that I can't recall the previous Node.js version that was working fine before this happened. Error: When I try to run my program using node app.js, ...

extract keys and values from an array of objects

I would like assistance with removing any objects where the inspectionScheduleQuestionId is null using JS. How can we achieve this? Thank you. #data const data = [ { "id": 0, "inspectionScheduleQuestionId": 1, ...

Access a webpage whose URL has been dynamically assigned using JavaScript

I have a website that consists of a single page and features four tabs. Whenever a tab is clicked, it displays the corresponding content in a div while hiding the other three divs along with their respective content. To ensure a smooth user experience, I u ...

Retrieve items from the parent row of selected checkboxes

Within my table, I have the following row. <tr class="data_rows" ng-repeat='d in t2'> <td class="tds"> <input class='checkBoxInput' type='checkbox' onchange='keepCount(this)'></td> &l ...

Navigating Large Datasets in Windows Azure: A Comprehensive Solution

Looking for an efficient solution to handle millions of entities stored in Windows Azure Table Storage, I am seeking a method that satisfies the following criteria: 1) When searching for an entity, retrieve that entity along with a specified number of ent ...

Benefits of Utilizing Object.create

Unlike the referenced question, this code snippet is sourced from the book "JavaScript: The Definitive Guide". It introduces an inherit method that applies Object.create if available, but falls back to traditional JavaScript inheritance when it's not. ...

Is there a Joomla extension available that can display or conceal content upon clicking?

Looking to enhance my Joomla site by installing a plugin that allows me to toggle the visibility of content with a click, similar to how FAQ sections work on many websites. Question 1 (click here for the answer) { Details for question 1 go here } Questi ...

Utilize JavaScript to reference any numerical value

I am attempting to create a button that refreshes the page, but only functions on the root / page and any page starting with /page/* (where * can be any number). Below is the code I have written: $('.refresh a').click(function() { var pathNa ...

Pass data between JavaScript and PHP using the Phaser framework

I am trying to pass a JavaScript variable to PHP and then store it in a database. Despite searching for solutions on Google, I have not been successful. Most suggestions involve using AJAX, but the code doesn't seem to work when I try it. I attempted ...

Unable to establish a session on the Node server

I'm currently in the process of setting up a node server that includes login, logout, and authentication functionalities. However, I've encountered an issue with my code where after debugging, some debug information logs are being generated that ...

How can I switch out an item within FabricJS?

In order to incorporate undo and redo functionality, I have created an array named "history" that stores the most recent changes triggered by canvas.on() events. Displaying console.log: History: (3) […] ​ 0: Object { target: {…} } //initial object ...

Struggling with implementing nested for loops

I am struggling to find a solution for this issue. I need to compare the content of two arrays with each other. If they are exactly the same, I want to execute the if statement; otherwise, I want the else statement to be executed. The current setup is ca ...

Determine if a specific date occurred at least one day ago using momentjs

Is there a way to determine if a specific date is at least one day (24 hours) in the past using momentjs? For example: const currentDate = moment() const isAtLeastOneDayAgo = currentDate.subtract(dateToVerify) > 1 // How can this be done? ...

Is there a way to seamlessly integrate typeahead.js with jquery.validate?

Currently, I have a website built on ASP.NET MVC 5 which utilizes jQuery validation (specifically 'jquery.validate.js' in the MVC project template). My goal is to implement type-ahead functionality using 'typeahead.js' on an input field ...

Tips on confirming the completion of my form when the next button is pressed

I am working on a multi-step form with a jQuery script to split the forms. The first form has only one button - the next button, which takes the client to the next form. However, when I click on the next button in the first form, it moves to the next form ...

Echarts: scatter plots linked with a line to the axis (resembling the PACF diagram)

I am currently working with echarts (js). Is there a method to link the dot of the scatter plot with the 0 value on the y-axis? I want it to resemble a pacf plot, similar to this example: The desired outcome should look something like this: https://i.sta ...

Tips for automatically incorporating animation upon page initialization

I'm looking to add an automatic image effect when the page is loaded. I currently have this code in my js file: $(window).ready(function(){ $(pin).click(function(){ $("#pin01").show().animate({left: '650px'}); }) }); Here is the HTML wit ...

"Disabling bufferCommands feature in MongooseJs does not seem to be functioning as intended

I am currently utilizing MongooseJs to handle database operations such as creating and reading data. However, I have encountered an issue where I need Mongoose to immediately display any database connection errors upon request, but it seems to be buffering ...

React-Image-Annotate encountered an issue: SyntaxError - The import statement cannot be used outside a module

Encountering an issue while trying to set up react-image-annotate. Here is the problem I am facing initially: https://i.stack.imgur.com/XgYPd.png This is how I have implemented it: import React from 'react' import ReactImageAnnotate from ' ...

Tips on transforming JSON output into an array with JavaScript

Looking for a solution to convert a Json response into an array using JavaScript. I currently have the following json response: ["simmakkal madurai","goripalayam madurai"]. I need to transform these results into an array format. Any suggestions on how I ...