下面我将创建一个带有换行符的字符串,稍后将出现在电子邮件中。
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 (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}
`
};
它还会缩进输出。
问题 有没有办法缩进行而不缩进输出?
最佳答案
目前还无法做到这一点。
但是有一个TC39 proposal to change that (仍处于草案
阶段)。
所以也许将来是可能的。
同时,您可以使用零依赖 dedent library这正是这样做的。
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}
`
};
它负责处理不同类型的换行符、空行、转义反引号,并保持所有其他缩进一致。
关于javascript - 可以在模板中缩进行而不缩进内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66548165/