Skip to content

Commit 87c9e60

Browse files
jangelsbJosh Angelsberg
andauthored
Add a new Parse Option to disable Smart Opts during parsing (#62)
* * Add a new Parse Option to disable Smart Opts during parsing: i.e., * disable converting straight quotes to curly, --- to em dashes, -- to en dashes during parsing * Update parser initialization to use the defaults when the options contain `.disableSmartOpts` * * Add a unit test to verify `.disableSmartOpts` works as expected Co-authored-by: Josh Angelsberg <[email protected]>
1 parent e7a570d commit 87c9e60

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Sources/Markdown/Parser/CommonMarkConverter.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,11 @@ struct MarkupParser {
577577

578578
static func parseString(_ string: String, source: URL?, options: ParseOptions) -> Document {
579579
cmark_gfm_core_extensions_ensure_registered()
580-
let parser = cmark_parser_new(CMARK_OPT_SMART)
580+
581+
let parser = cmark_parser_new(options.contains(.disableSmartOpts)
582+
? CMARK_OPT_DEFAULT
583+
: CMARK_OPT_SMART)
584+
581585
cmark_parser_attach_syntax_extension(parser, cmark_find_syntax_extension("table"))
582586
cmark_parser_attach_syntax_extension(parser, cmark_find_syntax_extension("strikethrough"))
583587
cmark_parser_attach_syntax_extension(parser, cmark_find_syntax_extension("tasklist"))

Sources/Markdown/Parser/ParseOptions.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ public struct ParseOptions: OptionSet {
2121

2222
/// Enable interpretation of symbol links from inline code spans surrounded by two backticks.
2323
public static let parseSymbolLinks = ParseOptions(rawValue: 1 << 1)
24+
25+
/// Disable converting straight quotes to curly, --- to em dashes, -- to en dashes during parsing
26+
public static let disableSmartOpts = ParseOptions(rawValue: 1 << 2)
2427
}
2528

Tests/MarkdownTests/Parsing/SourceURLTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,26 @@ class SourceURLTests: XCTestCase {
7575
"""
7676
XCTAssertEqual(expectedDump, document.debugDescription(options: .printSourceLocations))
7777
}
78+
79+
func testParseString_SmartOpts() {
80+
let text = "The iPod (2001--2022) changed the way people listened and interacted with music---it'll forever be in our hearts!"
81+
82+
// With Smart Opts
83+
let smartOptsEnabledDocument = Document(parsing: text, options: [])
84+
let smartOptsEnabledExpectedDump = """
85+
Document
86+
└─ Paragraph
87+
└─ Text \"The iPod (2001–2022) changed the way people listened and interacted with music—it’ll forever be in our hearts!\"
88+
"""
89+
XCTAssertEqual(smartOptsEnabledExpectedDump, smartOptsEnabledDocument.debugDescription())
90+
91+
// Without Smart Opts
92+
let smartOptsDisabledDocument = Document(parsing: text, options: [.disableSmartOpts])
93+
let smartOptsDisabledExpectedDump = """
94+
Document
95+
└─ Paragraph
96+
└─ Text \"The iPod (2001--2022) changed the way people listened and interacted with music---it'll forever be in our hearts!\"
97+
"""
98+
XCTAssertEqual(smartOptsDisabledExpectedDump, smartOptsDisabledDocument.debugDescription())
99+
}
78100
}

0 commit comments

Comments
 (0)