@grafctory/core - v0.15.0
    Preparing search index...

    Function dedent

    Removes the common indentation from a block of text or a template literal.

    • Removes the common indentation from a block of text.

      Strips the longest leading whitespace sequence shared by all non-blank lines, so text laid out to match the indentation of the surrounding code reads as if written flush left:

      • Line terminators (\r\n, \r, \n) are normalised to \n
      • Blank lines are emitted empty and take no part in the shared sequence
      • Leading and trailing blank lines are dropped
      • Everything else is preserved, trailing whitespace on content lines included

      Text whose lines share no leading whitespace keeps its indentation, aside from these normalisations.

      Note

      Margins are compared as character sequences rather than as visual widths, so a tab never matches a run of spaces, even where the two render to the same width.

      Parameters

      • string: string

        The indented string to realign

      Returns string

      A copy of string with the line terminators normalised, the outer blank lines dropped and the shared leading whitespace removed from every line

    • Removes the common indentation from a template literal.

      Realigns the literal sections under the same rules as the plain text form, then inserts the interpolated values, converted to strings as the template literal itself would. A value sourced at runtime thus never alters the layout of the literal around it, and a template can match the indentation of the surrounding code whatever it renders:

      • The shared margin is computed from the literal sections alone, so a multi-line value never suppresses the dedent
      • A line carrying a placeholder counts as content, even if its value is empty or blank
      • Values are inserted verbatim at the column their placeholder occupies after realignment; the continuation lines of a multi-line value keep their own indentation and line terminators, and are never realigned to the placeholder
      • Leading and trailing blank lines are dropped from the assembled result, including those a value contributes
      Note

      A tagged call and a plain call on the same literal differ whenever a value spans several lines or leaves its line blank. A plain call receives only the assembled string, so it computes the shared margin over the lines of the value as well, and treats a line left blank by an empty or blank value as blank rather than as content.

      Parameters

      • template: TemplateStringsArray

        The literal sections of the template

      • ...values: readonly unknown[]

        The values interpolated between the literal sections

      Returns string

      The assembled template with the line terminators of the literal normalised, the shared leading whitespace removed from every literal line, the values inserted verbatim and the outer blank lines dropped

      text and markdown for tags that dedent while declaring the content type