|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import cmark_gfm |
| 12 | + |
| 13 | +/// Options to use when converting Markdown. |
| 14 | +public struct ConvertOptions { |
| 15 | + public let parseOptions: ParseOptions |
| 16 | + public let commonmarkOptions: CommonmarkOptions |
| 17 | + public let commonmarkExtensions: [String] |
| 18 | + |
| 19 | + public init(parseOptions: ParseOptions, commonmarkOptions: CommonmarkOptions, extensions: [String]) { |
| 20 | + self.parseOptions = parseOptions |
| 21 | + self.commonmarkOptions = commonmarkOptions |
| 22 | + self.commonmarkExtensions = extensions |
| 23 | + } |
| 24 | + |
| 25 | + public init(fromParseOptions options: ParseOptions) { |
| 26 | + var commonmarkOptions = ConvertOptions.defaultCommonmarkOptions |
| 27 | + if options.contains(.disableSmartOpts) { |
| 28 | + commonmarkOptions.remove(.smart) |
| 29 | + } |
| 30 | + if options.contains(.disableSourcePosOpts) { |
| 31 | + commonmarkOptions.remove(.sourcepos) |
| 32 | + } |
| 33 | + self.init( |
| 34 | + parseOptions: options, |
| 35 | + commonmarkOptions: commonmarkOptions, |
| 36 | + extensions: ConvertOptions.defaultCommonmarkExtensions |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + public init() { |
| 41 | + self.init(fromParseOptions: ConvertOptions.defaultParseOptions) |
| 42 | + } |
| 43 | + |
| 44 | + public static let defaultParseOptions: ParseOptions = [] |
| 45 | + public static let defaultCommonmarkOptions: CommonmarkOptions = [ |
| 46 | + .smart, |
| 47 | + .tableSpans, |
| 48 | + .sourcepos |
| 49 | + ] |
| 50 | + public static let defaultCommonmarkExtensions: [String] = [ |
| 51 | + "table", |
| 52 | + "strikethrough", |
| 53 | + "tasklist", |
| 54 | + ] |
| 55 | +} |
| 56 | + |
| 57 | +/// Options given to the Commonmark converter. |
| 58 | +public struct CommonmarkOptions: OptionSet { |
| 59 | + public var rawValue: Int32 |
| 60 | + |
| 61 | + public init(rawValue: Int32) { |
| 62 | + self.rawValue = rawValue |
| 63 | + } |
| 64 | + |
| 65 | + /// The default Commonmark behavior, no special options. |
| 66 | + public static let `default` = CommonmarkOptions(rawValue: CMARK_OPT_DEFAULT) |
| 67 | + |
| 68 | + /// Include a `data-sourcepos` element on all block elements. |
| 69 | + public static let sourcepos = CommonmarkOptions(rawValue: CMARK_OPT_SOURCEPOS) |
| 70 | + |
| 71 | + /// Render `softbreak` elements as hard line breaks. |
| 72 | + public static let hardBreaks = CommonmarkOptions(rawValue: CMARK_OPT_HARDBREAKS) |
| 73 | + |
| 74 | + /// Render raw HTML and unsafe links. |
| 75 | + /// |
| 76 | + /// Unsafe links are `javascript:`, `vbscript:`, `file:`, and |
| 77 | + /// `data:`, except for `image/png`, `image/gif`, `image/jpeg` |
| 78 | + /// or `image/webp` MIME types. Without this option, raw HTML |
| 79 | + /// is replaced by a placeholder HTML comment. Unsafe links |
| 80 | + /// are replaced by empty strings. |
| 81 | + public static let unsafe = CommonmarkOptions(rawValue: CMARK_OPT_UNSAFE) |
| 82 | + |
| 83 | + /// Render `softbreak` elements as spaces. |
| 84 | + public static let noBreaks = CommonmarkOptions(rawValue: CMARK_OPT_NOBREAKS) |
| 85 | + |
| 86 | + /// Validate UTF-8 in the input before parsing, replacing illegal |
| 87 | + /// sequences with the replacement character `U+FFFD`. |
| 88 | + public static let validateUtf8 = CommonmarkOptions(rawValue: CMARK_OPT_VALIDATE_UTF8) |
| 89 | + |
| 90 | + /// Convert straight quotes to curly, `---` to em dashes, `--` to en dashes. |
| 91 | + public static let smart = CommonmarkOptions(rawValue: CMARK_OPT_SMART) |
| 92 | + |
| 93 | + /// Use GitHub-style `<pre lang="x">` tags for code blocks instead of |
| 94 | + /// `<pre><code class="language-x">`. |
| 95 | + public static let githubPreLang = CommonmarkOptions(rawValue: CMARK_OPT_GITHUB_PRE_LANG) |
| 96 | + |
| 97 | + /// Be liberal in interpreting inline HTML tags. |
| 98 | + public static let liberalHtmlTag = CommonmarkOptions(rawValue: CMARK_OPT_LIBERAL_HTML_TAG) |
| 99 | + |
| 100 | + /// Parse footnotes. |
| 101 | + public static let footnotes = CommonmarkOptions(rawValue: CMARK_OPT_FOOTNOTES) |
| 102 | + |
| 103 | + /// Only parse strikethroughs if surrounded by exactly 2 tildes. |
| 104 | + /// |
| 105 | + /// Strikethroughs are still only parsed when the `"strikethrough"` |
| 106 | + /// extension is enabled. |
| 107 | + public static let strikethroughDoubleTilde = CommonmarkOptions(rawValue: CMARK_OPT_STRIKETHROUGH_DOUBLE_TILDE) |
| 108 | + |
| 109 | + /// Use style attributes to align table cells instead of align attributes. |
| 110 | + public static let tablePreferStyleAttributes = CommonmarkOptions(rawValue: CMARK_OPT_TABLE_PREFER_STYLE_ATTRIBUTES) |
| 111 | + |
| 112 | + /// Include the remainder of the info string in code blocks in |
| 113 | + /// a separate attribute. |
| 114 | + public static let fullInfoString = CommonmarkOptions(rawValue: CMARK_OPT_FULL_INFO_STRING) |
| 115 | + |
| 116 | + /// Parse only inline markdown directives. Block directives will not be |
| 117 | + /// parsed (their literal representations will remain in the output). |
| 118 | + public static let inlineOnly = CommonmarkOptions(rawValue: CMARK_OPT_INLINE_ONLY) |
| 119 | + |
| 120 | + /// Parse the markdown input without removing preceding/trailing whitespace and |
| 121 | + /// without converting newline characters to breaks. |
| 122 | + /// |
| 123 | + /// Using this option also enables the `CMARK_OPT_INLINE_ONLY` option. |
| 124 | + // FIXME: the original `CMARK_OPT_PRESERVE_WHITESPACE` isn't available to the swift compiler? |
| 125 | + public static let preserveWhitespace = CommonmarkOptions(rawValue: (1 << 19) | CMARK_OPT_INLINE_ONLY) |
| 126 | + |
| 127 | + /// Enable the row- and column-span syntax in the tables extension. |
| 128 | + public static let tableSpans = CommonmarkOptions(rawValue: CMARK_OPT_TABLE_SPANS) |
| 129 | + |
| 130 | + /// Use a "ditto mark" (`"`) instead of a caret (`^`) to indicate row-spans in the tables extension. |
| 131 | + public static let tableRowspanDitto = CommonmarkOptions(rawValue: CMARK_OPT_TABLE_ROWSPAN_DITTO) |
| 132 | +} |
0 commit comments