From 3cc84a6668343c10d8193523fb2af86c026f57a9 Mon Sep 17 00:00:00 2001 From: Sam <30577766+Samasaur1@users.noreply.github.com> Date: Thu, 2 Feb 2023 12:43:17 -0800 Subject: [PATCH 1/5] Allow separate strong emphasis marker --- Sources/Markdown/Walker/Walkers/MarkupFormatter.swift | 10 +++++++--- .../MarkdownTests/Visitors/MarkupFormatterTests.swift | 4 ++-- Tools/markdown-tool/Commands/FormatCommand.swift | 8 ++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift index 310280a5..f7ae4cda 100644 --- a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift +++ b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift @@ -248,6 +248,7 @@ public struct MarkupFormatter: MarkupWalker { var thematicBreakCharacter: ThematicBreakCharacter var thematicBreakLength: UInt var emphasisMarker: EmphasisMarker + var strongEmphasisMarker: EmphasisMarker var condenseAutolinks: Bool var preferredHeadingStyle: PreferredHeadingStyle var preferredLineLimit: PreferredLineLimit? @@ -264,7 +265,8 @@ public struct MarkupFormatter: MarkupWalker { - defaultCodeBlockLanguage: The default language string to use when code blocks don't have a language and will be printed as fenced code blocks. - thematicBreakCharacter: The character to use for thematic breaks. - thematicBreakLength: The length of printed thematic breaks. - - emphasisMarker: The character to use for emphasis and strong emphasis markers. + - emphasisMarker: The character to use for emphasis markers. + - strongEmphasisMarker: The character to use for strong emphasis markers. - condenseAutolinks: Print links whose link text and destination match as autolinks, e.g. ``. - preferredHeadingStyle: The preferred heading style. - lineLimit: The preferred maximum line length and method for splitting ``Text`` elements in an attempt to maintain that line length. @@ -277,6 +279,7 @@ public struct MarkupFormatter: MarkupWalker { thematicBreakCharacter: ThematicBreakCharacter = .dash, thematicBreakLength: UInt = 5, emphasisMarker: EmphasisMarker = .star, + strongEmphasisMarker: EmphasisMarker = .star, condenseAutolinks: Bool = true, preferredHeadingStyle: PreferredHeadingStyle = .atx, preferredLineLimit: PreferredLineLimit? = nil, @@ -288,6 +291,7 @@ public struct MarkupFormatter: MarkupWalker { self.defaultCodeBlockLanguage = defaultCodeBlockLanguage self.thematicBreakCharacter = thematicBreakCharacter self.emphasisMarker = emphasisMarker + self.strongEmphasisMarker = strongEmphasisMarker self.condenseAutolinks = condenseAutolinks self.preferredHeadingStyle = preferredHeadingStyle self.preferredLineLimit = preferredLineLimit @@ -862,9 +866,9 @@ public struct MarkupFormatter: MarkupWalker { } public mutating func visitStrong(_ strong: Strong) { - print(String(repeating: formattingOptions.emphasisMarker.rawValue, count: 2), for: strong) + print(String(repeating: formattingOptions.strongEmphasisMarker.rawValue, count: 2), for: strong) descendInto(strong) - print(String(repeating: formattingOptions.emphasisMarker.rawValue, count: 2), for: strong) + print(String(repeating: formattingOptions.strongEmphasisMarker.rawValue, count: 2), for: strong) } public mutating func visitText(_ text: Text) { diff --git a/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift b/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift index 37750e62..d4810dd3 100644 --- a/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift +++ b/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift @@ -533,13 +533,13 @@ class MarkupFormatterOptionsTests: XCTestCase { do { let document = Document(parsing: underline) - let printed = document.format(options: .init(emphasisMarker: .star)) + let printed = document.format(options: .init(strongEmphasisMarker: .star)) XCTAssertEqual(star, printed) } do { let document = Document(parsing: star) - let printed = document.format(options: .init(emphasisMarker: .underline)) + let printed = document.format(options: .init(strongEmphasisMarker: .underline)) XCTAssertEqual(underline, printed) } diff --git a/Tools/markdown-tool/Commands/FormatCommand.swift b/Tools/markdown-tool/Commands/FormatCommand.swift index 4f81c791..f677fa9a 100644 --- a/Tools/markdown-tool/Commands/FormatCommand.swift +++ b/Tools/markdown-tool/Commands/FormatCommand.swift @@ -96,6 +96,9 @@ extension MarkdownCommand { @Option(help: "Emphasis marker; choices: \(MarkupFormatter.Options.EmphasisMarker.allCases.map { $0.rawValue }.joined(separator: ", "))") var emphasisMarker: String = "*" + @Option(help: "Strong emphasis marker; choices: \(MarkupFormatter.Options.EmphasisMarker.allCases.map { $0.rawValue }.joined(separator: ", "))") + var strongEmphasisMarker: String = "*" + @Flag(inversion: .prefixedNo, exclusivity: .chooseLast, help: "Condense links whose text matches their destination to 'autolinks' e.g. ") var condenseAutolinks: Bool = true @@ -206,6 +209,10 @@ extension MarkdownCommand { throw ArgumentParser.ValidationError("The value '\(self.emphasisMarker)' is invalid for '--emphasis-marker'") } + guard let strongEmphasisMarker = MarkupFormatter.Options.EmphasisMarker(argument: strongEmphasisMarker) else { + throw ArgumentParser.ValidationError("The value '\(self.strongEmphasisMarker)' is invalid for '--strong-emphasis-marker'") + } + guard let unorderedListMarker = MarkupFormatter.Options.UnorderedListMarker(argument: unorderedListMarker) else { throw ArgumentParser.ValidationError("The value '\(self.unorderedListMarker)' is invalid for '--unordered-list-marker'") } @@ -229,6 +236,7 @@ extension MarkdownCommand { thematicBreakCharacter: thematicBreakCharacter, thematicBreakLength: thematicBreakLength, emphasisMarker: emphasisMarker, + strongEmphasisMarker: strongEmphasisMarker, condenseAutolinks: condenseAutolinks, preferredHeadingStyle: preferredHeadingStyle, preferredLineLimit: preferredLineLimit, From 1db1755fe61ef1282ab6c99f0c73e10d49e703f5 Mon Sep 17 00:00:00 2001 From: Sam <30577766+Samasaur1@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:11:58 -0800 Subject: [PATCH 2/5] Make change backwards-compatible --- .../Walker/Walkers/MarkupFormatter.swift | 4 +-- .../Visitors/MarkupFormatterTests.swift | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift index f7ae4cda..6c3d71ba 100644 --- a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift +++ b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift @@ -279,7 +279,7 @@ public struct MarkupFormatter: MarkupWalker { thematicBreakCharacter: ThematicBreakCharacter = .dash, thematicBreakLength: UInt = 5, emphasisMarker: EmphasisMarker = .star, - strongEmphasisMarker: EmphasisMarker = .star, + strongEmphasisMarker: EmphasisMarker? = nil, condenseAutolinks: Bool = true, preferredHeadingStyle: PreferredHeadingStyle = .atx, preferredLineLimit: PreferredLineLimit? = nil, @@ -291,7 +291,7 @@ public struct MarkupFormatter: MarkupWalker { self.defaultCodeBlockLanguage = defaultCodeBlockLanguage self.thematicBreakCharacter = thematicBreakCharacter self.emphasisMarker = emphasisMarker - self.strongEmphasisMarker = strongEmphasisMarker + self.strongEmphasisMarker = strongEmphasisMarker ?? emphasisMarker self.condenseAutolinks = condenseAutolinks self.preferredHeadingStyle = preferredHeadingStyle self.preferredLineLimit = preferredLineLimit diff --git a/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift b/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift index d4810dd3..972398c5 100644 --- a/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift +++ b/Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift @@ -531,6 +531,23 @@ class MarkupFormatterOptionsTests: XCTestCase { let underline = "__strong__" let star = "**strong**" + do { + let document = Document(parsing: underline) + let printed = document.format(options: .init(emphasisMarker: .star)) + XCTAssertEqual(star, printed) + } + + do { + let document = Document(parsing: star) + let printed = document.format(options: .init(emphasisMarker: .underline)) + XCTAssertEqual(underline, printed) + } + } + + do { + let underline = "__strong__" + let star = "**strong**" + do { let document = Document(parsing: underline) let printed = document.format(options: .init(strongEmphasisMarker: .star)) @@ -542,7 +559,23 @@ class MarkupFormatterOptionsTests: XCTestCase { let printed = document.format(options: .init(strongEmphasisMarker: .underline)) XCTAssertEqual(underline, printed) } + } + + do { + let underline = "__strong__" + let star = "**strong**" + do { + let document = Document(parsing: underline) + let printed = document.format(options: .init(emphasisMarker: .underline, strongEmphasisMarker: .star)) + XCTAssertEqual(star, printed) + } + + do { + let document = Document(parsing: star) + let printed = document.format(options: .init(emphasisMarker: .star, strongEmphasisMarker: .underline)) + XCTAssertEqual(underline, printed) + } } } From da3b39ca688f14e61402d4846c5030a81cc694bf Mon Sep 17 00:00:00 2001 From: Sam <30577766+Samasaur1@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:26:00 -0800 Subject: [PATCH 3/5] Make markdown-tool changes backwards compatible as well --- Tools/markdown-tool/Commands/FormatCommand.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/markdown-tool/Commands/FormatCommand.swift b/Tools/markdown-tool/Commands/FormatCommand.swift index f677fa9a..410e1bc9 100644 --- a/Tools/markdown-tool/Commands/FormatCommand.swift +++ b/Tools/markdown-tool/Commands/FormatCommand.swift @@ -97,7 +97,7 @@ extension MarkdownCommand { var emphasisMarker: String = "*" @Option(help: "Strong emphasis marker; choices: \(MarkupFormatter.Options.EmphasisMarker.allCases.map { $0.rawValue }.joined(separator: ", "))") - var strongEmphasisMarker: String = "*" + var strongEmphasisMarker: String? @Flag(inversion: .prefixedNo, exclusivity: .chooseLast, help: "Condense links whose text matches their destination to 'autolinks' e.g. ") var condenseAutolinks: Bool = true @@ -209,7 +209,7 @@ extension MarkdownCommand { throw ArgumentParser.ValidationError("The value '\(self.emphasisMarker)' is invalid for '--emphasis-marker'") } - guard let strongEmphasisMarker = MarkupFormatter.Options.EmphasisMarker(argument: strongEmphasisMarker) else { + guard let strongEmphasisMarker = MarkupFormatter.Options.EmphasisMarker(argument: strongEmphasisMarker ?? self.emphasisMarker) else { throw ArgumentParser.ValidationError("The value '\(self.strongEmphasisMarker)' is invalid for '--strong-emphasis-marker'") } From 7be8670e76efa53890f0c2c40a123128272d7ae9 Mon Sep 17 00:00:00 2001 From: Sam <30577766+Samasaur1@users.noreply.github.com> Date: Thu, 2 Feb 2023 16:15:47 -0800 Subject: [PATCH 4/5] Add more documentation --- Sources/Markdown/Walker/Walkers/MarkupFormatter.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift index 6c3d71ba..6b472018 100644 --- a/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift +++ b/Sources/Markdown/Walker/Walkers/MarkupFormatter.swift @@ -266,7 +266,7 @@ public struct MarkupFormatter: MarkupWalker { - thematicBreakCharacter: The character to use for thematic breaks. - thematicBreakLength: The length of printed thematic breaks. - emphasisMarker: The character to use for emphasis markers. - - strongEmphasisMarker: The character to use for strong emphasis markers. + - strongEmphasisMarker: The character to use for strong emphasis markers. If no value is provided, uses `emphasisMarker`. - condenseAutolinks: Print links whose link text and destination match as autolinks, e.g. ``. - preferredHeadingStyle: The preferred heading style. - lineLimit: The preferred maximum line length and method for splitting ``Text`` elements in an attempt to maintain that line length. From 29e8e09b362a9e5ccb3a4ecf0ffb34f920e82290 Mon Sep 17 00:00:00 2001 From: Sam <30577766+Samasaur1@users.noreply.github.com> Date: Thu, 10 Oct 2024 00:00:57 -0700 Subject: [PATCH 5/5] Don't duplicate emphasis marker selection logic --- Tools/markdown-tool/Commands/FormatCommand.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Tools/markdown-tool/Commands/FormatCommand.swift b/Tools/markdown-tool/Commands/FormatCommand.swift index 410e1bc9..e9817eab 100644 --- a/Tools/markdown-tool/Commands/FormatCommand.swift +++ b/Tools/markdown-tool/Commands/FormatCommand.swift @@ -209,8 +209,14 @@ extension MarkdownCommand { throw ArgumentParser.ValidationError("The value '\(self.emphasisMarker)' is invalid for '--emphasis-marker'") } - guard let strongEmphasisMarker = MarkupFormatter.Options.EmphasisMarker(argument: strongEmphasisMarker ?? self.emphasisMarker) else { - throw ArgumentParser.ValidationError("The value '\(self.strongEmphasisMarker)' is invalid for '--strong-emphasis-marker'") + let strongEmphasisMarker: MarkupFormatter.Options.EmphasisMarker? + if let marker = self.strongEmphasisMarker { + guard let marker = MarkupFormatter.Options.EmphasisMarker(argument: marker) else { + throw ArgumentParser.ValidationError("The value '\(marker)' is invalid for '--strong-emphasis-marker'") + } + strongEmphasisMarker = marker + } else { + strongEmphasisMarker = nil } guard let unorderedListMarker = MarkupFormatter.Options.UnorderedListMarker(argument: unorderedListMarker) else {