Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit b07b7c6

Browse files
authored
Fix GitHub emoji short code escaping (#167)
* Revert "Add zero width space to escape emoji shortcodes (#149)" This reverts commit aeed63c. * Extract StringProtocol helper methods into separate file * Add and apply escapingEmojiShortcodes property where necessary * Add Changelog entry for #167 * Fix Swift 5.2 error 'closure containing control flow statement cannot be used with function builder'
1 parent cc2098c commit b07b7c6

File tree

8 files changed

+39
-41
lines changed

8 files changed

+39
-41
lines changed

Changelog.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
#159 by @mattt.
2323
- Fixed relationship diagram to prevent linking to unknown symbols.
2424
#178 by @MattKiazyk.
25+
- Fixed problems in CommonMark output related to escaping emoji shortcode.
26+
#167 by @mattt.
2527

2628
### Changed
2729

@@ -56,9 +58,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5658
#130 by @mattt.
5759
- Fixed file and directory unexpected permissions.
5860
#146 by @niw.
59-
- Fixed rendering of colon sequences in function signatures
60-
as emoji shortcodes (e.g. `:on:` → 🔛).
61-
#149 by @mattt.
6261
- Fixed declarations for properties without explicit type annotations.
6362
#150 by @mattt.
6463
- Fixed visual regression for adjacent linked tokens in code block.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extension StringProtocol {
2+
func indented(by spaces: Int = 2) -> String {
3+
return String(repeating: " ", count: spaces) + self
4+
}
5+
6+
func leftPadded(to length: Int) -> String {
7+
guard count < length else { return String(self) }
8+
return String(repeating: " ", count: length - count) + self
9+
}
10+
11+
func rightPadded(to length: Int) -> String {
12+
guard count < length else { return String(self) }
13+
return self + String(repeating: " ", count: length - count)
14+
}
15+
16+
var escapingEmojiShortcodes: String {
17+
// Insert U+200B ZERO WIDTH SPACE
18+
// to prevent colon sequences from being interpreted as
19+
// emoji shortcodes (without wrapping with code element).
20+
// See: https://docs.github.com/en/github/writing-on-github/basic-writing-and-formatting-syntax#using-emoji
21+
return self.replacingOccurrences(of: ":", with: ":\u{200B}")
22+
}
23+
}

Sources/swift-doc/Subcommands/Coverage.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ extension SwiftDoc {
2828
let data = try encoder.encode(report)
2929
try data.write(to: URL(fileURLWithPath: output))
3030
} else {
31-
print(["Total".rightPadded(to: 60), format(report.totals.percentage)].joined(separator: "\t"))
31+
print(["Total".rightPadded(to: 60), format(percentage: report.totals.percentage)].joined(separator: "\t"))
3232
for (file, ratio) in report.coverageBySourceFile.sorted(by: { $0.0 < $1.0 }) {
33-
print([" - \(file)".rightPadded(to: 60), format(ratio.percentage)].joined(separator: "\t"))
33+
print([" - \(file)".rightPadded(to: 60), format(percentage: ratio.percentage)].joined(separator: "\t"))
3434
}
3535

3636
print("")
@@ -46,18 +46,6 @@ extension SwiftDoc {
4646

4747
// MARK: -
4848

49-
fileprivate extension String {
50-
func leftPadded(to length: Int) -> String {
51-
guard count < length else { return self }
52-
return String(repeating: " ", count: length - count) + self
53-
}
54-
55-
func rightPadded(to length: Int) -> String {
56-
guard count < length else { return self }
57-
return self + String(repeating: " ", count: length - count)
58-
}
59-
}
60-
61-
fileprivate func format(_ percentage: Double) -> String {
49+
fileprivate func format(percentage: Double) -> String {
6250
return String(format: "%0.2f %%", percentage).leftPadded(to: 8)
6351
}

Sources/swift-doc/Subcommands/Diagram.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,3 @@ fileprivate func diagram(of module: Module) -> String {
7979

8080
return dot
8181
}
82-
83-
// MARK: -
84-
85-
fileprivate extension String {
86-
func indented(by spaces: Int = 2) -> String {
87-
return String(repeating: " ", count: spaces) + self
88-
}
89-
}

Sources/swift-doc/Supporting Types/Components/Abstract.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Abstract: Component {
2121
List.Item {
2222
Fragment {
2323
#"""
24-
[\#(symbol.id)](\#(path(for: symbol, with: baseURL))):
24+
[\#(symbol.id.description.escapingEmojiShortcodes)](\#(path(for: symbol, with: baseURL))):
2525
\#(summary)
2626
"""#
2727
}
@@ -31,7 +31,7 @@ struct Abstract: Component {
3131
return Fragment {
3232
List.Item {
3333
Paragraph {
34-
Link(urlString: path(for: symbol, with: baseURL), text: symbol.id.description)
34+
Link(urlString: path(for: symbol, with: baseURL), text: symbol.id.description.escapingEmojiShortcodes)
3535
}
3636
}
3737
}

Sources/swift-doc/Supporting Types/Components/Documentation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct Documentation: Component {
3636
}
3737

3838
if documentation.summary != nil {
39-
Fragment { "\(documentation.summary!)" }
39+
Fragment { "\(documentation.summary!.escapingEmojiShortcodes)" }
4040
}
4141

4242
Declaration(of: symbol, in: module, baseURL: baseURL)
@@ -57,14 +57,14 @@ struct Documentation: Component {
5757
if documentation.throws != nil {
5858
Section {
5959
Heading { "Throws" }
60-
Fragment { documentation.throws! }
60+
Fragment { documentation.throws!.escapingEmojiShortcodes }
6161
}
6262
}
6363

6464
if documentation.returns != nil {
6565
Section {
6666
Heading { "Returns" }
67-
Fragment { documentation.returns! }
67+
Fragment { documentation.returns!.escapingEmojiShortcodes }
6868
}
6969
}
7070

Sources/swift-doc/Supporting Types/Components/Requirements.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ struct Requirements: Component {
3131
ForEach(in: sections) { section -> BlockConvertible in
3232
Section {
3333
Heading { section.title }
34-
ForEach(in: section.requirements) { requirement in
35-
Heading { requirement.name }
36-
Documentation(for: requirement, in: module, baseURL: baseURL)
34+
Section {
35+
ForEach(in: section.requirements) { requirement in
36+
Heading { requirement.name.escapingEmojiShortcodes }
37+
Documentation(for: requirement, in: module, baseURL: baseURL)
38+
}
3739
}
3840
}
3941
}

Sources/swift-doc/Supporting Types/Page.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ extension Page {
2424
let data: Data?
2525
switch format {
2626
case .commonmark:
27-
var text = document.render(format: .commonmark)
28-
// Insert U+200B ZERO WIDTH SPACE
29-
// to prevent colon sequences from being interpreted as
30-
// emoji shortcodes (without wrapping with code element).
31-
// See: https://docs.github.com/en/github/writing-on-github/basic-writing-and-formatting-syntax#using-emoji
32-
text = text.replacingOccurrences(of: ":", with: ":\u{200B}")
33-
data = text.data(using: .utf8)
27+
data = document.render(format: .commonmark).data(using: .utf8)
3428
case .html:
3529
data = layout(self).description.data(using: .utf8)
3630
}

0 commit comments

Comments
 (0)