Skip to content

Commit

Permalink
Support pipes in markdown tables (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
eneko authored Jan 17, 2021
1 parent de25330 commit 5575590
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

![Release](https://img.shields.io/github/release/eneko/markdowngenerator.svg)
![Swift 4.0+](https://img.shields.io/badge/Swift-4.0+-orange.svg)
[![Build Status](https://travis-ci.org/eneko/MarkdownGenerator.svg?branch=master)](https://travis-ci.org/eneko/MarkdownGenerator)
[![Build Status](https://travis-ci.com/eneko/MarkdownGenerator.svg?branch=main)](https://travis-ci.com/eneko/MarkdownGenerator)
![CI](https://github.com/eneko/MarkdownGenerator/workflows/CI/badge.svg)
[![codecov](https://codecov.io/gh/eneko/MarkdownGenerator/branch/master/graph/badge.svg)](https://codecov.io/gh/eneko/MarkdownGenerator)
[![Swift Package Manager Compatible](https://img.shields.io/badge/spm-compatible-brightgreen.svg)](https://swift.org/package-manager)
![Linux Compatible](https://img.shields.io/badge/linux-compatible%20🐧-brightgreen.svg)
Expand All @@ -13,7 +14,7 @@ A small Swift library to generate Markdown documents.
**Features**
- ✅ Easily generate Markdown from structured data
- ✅ Extendible: make your classes and structs conform to `MarkdownConvertible`
- ✅ Swift Package Manager compatible
- ✅ Swift Package Manager compatible (Swift 4.0+)
- ✅ Linux compatible 🐧


Expand Down
6 changes: 5 additions & 1 deletion Sources/MarkdownGenerator/MarkdownTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public struct MarkdownTable: MarkdownConvertible {
/// - Parameter values: array of values
/// - Returns: Markdown formatted row
func makeRow(values: [String]) -> String {
let values = values.map { $0.replacingOccurrences(of: String.newLine, with: " ") }
let values = values.map { value in
value
.replacingOccurrences(of: String.newLine, with: " ")
.replacingOccurrences(of: "|", with: "\\|")
}
return "| " + values.joined(separator: " | ") + " |"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,24 @@ class MarkdownTableTests: XCTestCase {
XCTAssertEqual(table.markdown, output)
}

func testPipeEscaping() {
let table = MarkdownTable(headers: ["Foo"], data: [["Foo|Bar|Baz"]])

let output = """
| Foo |
| ----------- |
| Foo\\|Bar\\|Baz |
"""

XCTAssertEqual(table.markdown, output)
}

static var allTests = [
("test1x1Table", test1x1Table),
("test3x3Table", test3x3Table),
("testMultilineValues", testMultilineValues)
("testMultilineValues", testMultilineValues),
("testMixedTable", testMixedTable),
("testPipeEscaping", testPipeEscaping)
]

}

0 comments on commit 5575590

Please sign in to comment.