swiftlang / swift-markdown

A Swift package for parsing, building, editing, and analyzing Markdown documents.
https://swiftpackageindex.com/swiftlang/swift-markdown/documentation/markdown
Apache License 2.0
2.79k stars 191 forks source link

add indentationStyle to MarkupFormatter.Options #204

Open dpowers opened 1 month ago

dpowers commented 1 month ago

Summary

Add indentationStyle (.tabs/.spaces) to MarkupFormatter.Options to allow format to use tabs when outputting Markdown.

QuietMisdreavus commented 1 month ago

Could you add a CLI flag to the format tool in Tools/markdown-tool/Commands/FormatCommand.swift? That way this can be set in the command-line formatter and not just the API.

dpowers commented 1 month ago

Your comment about block directives has sent me down a bit of a rabbit hole.

I see a few possible solutions:

  1. Replace these 4 spaces with a single tab if indentationStyle is set to tab and reduce it to two spaces if indentationStyle is set to spaces.  This would be more consistent with other indentation choices made in the code, but would cause rendering differences with older versions of the code.
  2. Replace the 4 spaces with a single tab but hard code 4 spaces if spaces is set.  This feels clunky and like it's going to confuse someone a lot later.  But it's backwards compatible.
  3. Change case spaces to be case spaces(int) and use that many spaces in all places.  This doesn't really solve the backwards compatibility issue, but it does let the user dictate the space count in all places.  This is kind of 1 with a bit of extra space choice.

If it were my code alone, I would switch to 4 spaces as a hard coded indent in all places as a start.  It might be the case that the list indenting code needs more poking to be fully compliant, but in reality it probably works 99% of the time and any changes should be a different PR.

Since it's not my code alone, I'll let you tell me what you'd prefer.  :)

David (he/him)

On October 10, 2024, GitHub @.***> wrote:

@QuietMisdreavus requested changes on this pull request.

Good PR, but a couple more things:

  1. The indentation for content nested underneath a block directive (line 488) uses a four-space indent; i don't think we handle tabs for nested block-directive content, but if that is handled that should also be updated.
  2. Can you add some tests in MarkupFormatterTests?

In Sources/Markdown/Walker/Walkers/MarkupFormatter.swift https://github.com/swiftlang/swift- markdown/pull/204#discussion_r1795696090:

  • prefix += formattingOptions.indentationStyle.indentation() } unorderedListCount += 1 } else if element is OrderedList { if orderedListCount > 0 { - prefix += " " + prefix += formattingOptions.indentationStyle.indentation() These indentation markers use different numbers of spaces between ordered and unordered lists to align trailing text with the previous line. While it may still parse correctly, this is a regression in behavior.

— Reply to this email directly, view it on GitHub https://github.com/swiftlang/swift-markdown/pull/204#pullrequestreview- 2360764538, or unsubscribe https://github.com/notifications/unsubscribe- auth/AAGINYGEH7IPSE72CIUW4B3Z22N6HAVCNFSM6AAAAABPW2ZLMSVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDGNRQG43DINJTHA. You are receiving this because you authored the thread.Message ID: @.***>