You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在模板字符串中缩进代码行而不缩进最终输出内容?

如何在保持代码缩进规范的同时,不让模板字符串输出带缩进?

问题描述

我正在构建一个用于邮件内容的带换行符的字符串,目前的代码是这样的:

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}
`;
}

可这么做的话,输出的字符串也会带上这些缩进,有没有办法既能让代码保持整洁的缩进结构,又不让最终输出的内容有多余缩进呢?


解决方案

这个场景我之前也遇到过,有几种实用的方法可以解决这个冲突:

方法1:正则替换配合trim()

最简单的方式是在模板字符串末尾调用trim()去掉首尾空白,再用正则去掉每一行开头的缩进:

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}
  `.trim().replace(/^\s+/gm, '');
}

这里的/^\s+/gm正则会全局匹配每一行开头的所有空白字符(空格、制表符都算),gm标志确保多行模式生效,每一行都会被处理。

方法2:自定义标签模板函数

如果需要更灵活的处理,可以写一个标签模板函数,专门用来清理模板字符串的缩进:

// 自定义标签函数:移除每一行的缩进
function cleanIndent(strings, ...values) {
  let output = '';
  strings.forEach((str, index) => {
    // 去掉当前字符串片段里每一行的开头缩进
    const cleanedStr = str.replace(/^\s+/gm, '');
    output += cleanedStr + (values[index] || '');
  });
  // 去掉首尾的空行和空白
  return output.trim();
}

if (action) {
  description = cleanIndent`
    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}
  `;
}

这个函数会自动处理模板里的每一部分,清理掉缩进,之后不管你代码里用多少缩进,输出的字符串都是整齐无缩进的,还能保留内容里的换行。

方法3:精准匹配缩进量(适合固定缩进场景)

如果你的代码缩进是固定的(比如4个空格),也可以直接精准替换掉每一行开头的固定空格数:

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}
  `.trimStart().replace(/^ {4}/gm, '');
}

这里的/^ {4}/gm会匹配每一行开头的4个空格,把它们替换成空字符串。如果你的代码用2个空格缩进,就把{4}改成{2}就行。


内容的提问来源于stack exchange,提问作者Sandra Schlichting

火山引擎 最新活动