Skip to content

MarkupFormatter is inserting extra newlines #197

Open

Description

@shawnhyam

I've noticed that the formatting has been putting in extra newlines when wrapping text that contains inline code blocks. I've included a test below that illustrates the problem; both tests fail, but in particular the first fails because there is an extra newline preceding contributesBlockIndent in the output:

fileprivate func markdownFormat(_ text: String, _ maxLength: Int) -> String {
  let document = Document(parsing: text, options: .disableSmartOpts)
  let lineLimit = MarkupFormatter.Options.PreferredLineLimit(
    maxLength: maxLength,
    breakWith: .softBreak
  )
  let formatterOptions = MarkupFormatter.Options(
    orderedListNumerals: .incrementing(start: 1),
    useCodeFence: .always,
    condenseAutolinks: false,
    preferredLineLimit: lineLimit
  )

  let output = document.format(options: formatterOptions)
  return output
}

@Test func testInlineCodeWrapping() {
  let str = """
    This indent is applied independently of `contributesBlockIndent`, which means a given break \
    may apply both a continuation indent and a block indent, either indent, or neither indent.
    """

  let expected = """
    This indent is applied independently of 
    `contributesBlockIndent`, which means a given break
    may apply both a continuation indent and a block
    indent, either indent, or neither indent.
    """

  // format it twice; we want (a) the first version to be correct, and (b) the second version to be the same
  let trip1 = markdownFormat(str, 55)
  let trip2 = markdownFormat(trip1, 55)

  #expect(trip1 == expected)
  #expect(trip1 == trip2)
}

Activity

  1. mynona commented on Sep 15, 2024

    @mynona

    I can confirm this problem. It also effects the HTMLFormatter whenever you have content inside a <li> element it renders it as

    <li><p>content</p></li>
    

    but it should be just

    <li>content</li>
    
  2. mynona commented on Nov 3, 2024

    @mynona

    Any update?

  3. mynona commented on Feb 5, 2025

    @mynona

    Any update?

  4. zaneenders commented on Feb 21, 2026

    @zaneenders

    I get a similar issue even with the default settings. I end up running the formatter in a loop tell the output stabilize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions