Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
proggeramlug committed Feb 15, 2023
0 parents commit f44db30
Show file tree
Hide file tree
Showing 9 changed files with 622 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SwiftMarkdownHtml.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>MarkdownHtml</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>MarkdownHtmlTests</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2023 caching.guru / Ralph Kuepper

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

25 changes: 25 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "cmark-gfm",
"repositoryURL": "https://github.com/apple/swift-cmark.git",
"state": {
"branch": "gfm",
"revision": "86aeb491675de6f077a3a6df6cbcac1a25dcbee1",
"version": null
}
},
{
"package": "swift-markdown",
"repositoryURL": "https://github.com/apple/swift-markdown.git",
"state": {
"branch": "main",
"revision": "2c88ee595f84aa81aee6eaccf6705bba4f40c947",
"version": null
}
}
]
},
"version": 1
}
18 changes: 18 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version:5.2
import PackageDescription

let package = Package(
name: "SwiftMarkdownHtml",
products: [
.library(name: "MarkdownHtml", targets: ["MarkdownHtml"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-markdown.git", .branch("main")),
],
targets: [
.target(name: "MarkdownHtml", dependencies: [
.product(name: "Markdown", package: "swift-markdown"),
], path: "Sources"),
.testTarget(name: "MarkdownHtmlTests", dependencies: ["MarkdownHtml"])
]
)
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

# MarkdownHtml for Swift
This library is an attempt to allow for easy markdown to HTML conversion.

## Concepts & Usage

It's built as a `string` extension to allow for easy conversions:
```swift
let html = "# my markdown text".renderMarkdownToXML()
```

## Config
The following config options are available:
### HTML Tags
The only config we have thus far is that you can pick how to render HTML tags from the Markdown.

You can pick between:
* `hide`: Does not render the HTML at all. (safest)
* `renderAsIs`: Renders the tag as is.
* `renderWrapped`: Renders the tag wrapped in an `htmltag`-Tag (this is to allow XML conversion)

## License
MIT

## Development
This package is not fully tested or developed yet. Partially because the underlying library from Apple is not fully released yet either. But this may be a great starting point for your own implementation.

## Usage
This package is used by [caching.guru](https://caching.guru) in production.
213 changes: 213 additions & 0 deletions Sources/String+Markdown.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
//
// File.swift
//
//
// Created by Ralph Küpper on 2/15/23.
//

import Foundation
import Markdown
import SwiftSoup

public extension String {
struct MarkdownHtmlConfig {
enum HTMLTagConfig {
case hide
case renderAsIs
case renderWrapped
}
var renderHtmlTags: HTMLTagConfig

public static var defaultConfig = MarkdownHtmlConfig(renderHtmlTags: .renderWrapped)
}
func renderMarkdownToXML(_ config:MarkdownHtmlConfig = .defaultConfig) -> String {
let document = Document(parsing: self)
return self.renderChildXML(document, config: config)
}
private func renderChildXML(_ p: Markup, config: MarkdownHtmlConfig) -> String {
var ret = ""
if let para = p as? Paragraph {
ret = "<p>"
}
else if let para = p as? Markdown.Document {
// nothing to do
}
else if let text = p as? Text {
ret = ret + text.plainText
}
else if let break1 = p as? SoftBreak {
ret = ret + "<br />"
}
else if let ul = p as? UnorderedList {
ret = ret + "<ul>"
}
else if let ol = p as? OrderedList {
ret = ret + "<ol>"
}
else if let li = p as? ListItem {
ret = ret + "<li>"
}
else if let strong = p as? Strong {
ret = ret + "<strong>"
}
else if let strikethrough = p as? Strikethrough {
ret = ret + "<s>"
}
else if let blockQuote = p as? BlockQuote {
ret = ret + "<blockquote>"
}
else if let thematicBreak = p as? ThematicBreak {
ret = ret + "<hr />"
}
else if let table = p as? Table {
ret = ret + "<table>"
}
else if let tableHead = p as? Table.Head {
ret = ret + "<thead>"
}
else if let tableBody = p as? Table.Body {
ret = ret + "<tbody>"
}
else if let tableRow = p as? Table.Row {
ret = ret + "<tr>"
}
else if let tableCell = p as? Table.Cell {
ret = ret + "<td>"
}
else if let html = p as? InlineHTML {
switch config.renderHtmlTags {
case .hide:
return ret
case .renderAsIs:
ret = ret + "\(html.rawHTML)"
case .renderWrapped:
ret = ret + "<htmltag>\(html.rawHTML)"
}

}
else if let ul = p as? Emphasis {
ret = ret + "<i>"
} else if let inlineCode = p as? InlineCode {
ret = ret + "<code class=\"language-javascript\">"
} else if let link = p as? Link, let d = link.destination {
ret = ret + "<a href=\"\(d)\">"
} else if let image = p as? Image, let source = image.source {
if let title = image.title {
ret = ret + "<img src=\"\(source)\" title=\"\(title)\" />"
} else {
ret = ret + "<img src=\"\(source)\" />"
}
return ret
} else if let codeBlock = p as? CodeBlock {
if codeBlock.language == "box" {
ret = ret + "<div class=\"box\">"
} else if codeBlock.language == "info" {
ret = ret + "<div class=\"alert show alert-info fade\">"
} else if codeBlock.language == "url" {
ret = ret + "<div class=\"url\">"
} else if let language = codeBlock.language {
ret = ret + "<pre><code class=\"language-\(language)\">"
}
let c = Document(parsing: codeBlock.code)
ret = ret + self.renderChildXML(c, config: config)
} else if let h = p as? Heading {
if h.level == 1 {
ret = ret + "<h1>"
} else if h.level == 2 {
ret = ret + "<h2>"
} else if h.level == 3 {
ret = ret + "<h3>"
} else if h.level == 4 {
ret = ret + "<h4>"
} else if h.level == 5 {
ret = ret + "<h5>"
} else if h.level == 6 {
ret = ret + "<h6>"
}
}
else {
// In a final product we would not need this.
// As the markdown library has not released any official releases yet
// I'll leave it in until we know thre won't be any more.
// PR's welcome
#if DEBUG
print("unkown element1: ", type(of: p))
#endif
}

for c in p.children {
ret = ret + self.renderChildXML(c, config: config)
}

if let h = p as? Heading {
if h.level == 1 {
ret = ret + "</h1>"
} else if h.level == 2 {
ret = ret + "</h2>"
} else if h.level == 3 {
ret = ret + "</h3>"
} else if h.level == 4 {
ret = ret + "</h4>"
} else if h.level == 5 {
ret = ret + "</h5>"
} else if h.level == 6 {
ret = ret + "</h6>"
}
}
else if let link = p as? Link {
ret = ret + "</a>"
}
else if let codeBlock = p as? CodeBlock {
if ["box", "info", "url"].contains(codeBlock.language) {
ret = ret + "</div>"
} else {
ret = ret + "</code></pre>"
}
}
else if let li = p as? ListItem {
ret = ret + "</li>"
}
else if let inlindeCode = p as? InlineCode {
ret = ret + "</code>"
}
else if let strong = p as? Strong {
ret = ret + "</strong>"
}
else if let i = p as? Emphasis {
ret = ret + "</i>"
}
else if let blockQuote = p as? BlockQuote {
ret = ret + "</blockquote>"
}
else if let strikethrough = p as? Strikethrough {
ret = ret + "</s>"
}
else if let ul = p as? UnorderedList {
ret = ret + "</ul>"
}
else if let ol = p as? OrderedList {
ret = ret + "</ol>"
}
else if let table = p as? Table {
ret = ret + "</table>"
}
else if let tableHead = p as? Table.Head {
ret = ret + "</thead>"
}
else if let tableBody = p as? Table.Body {
ret = ret + "</tbody>"
}
else if let tableRow = p as? Table.Row {
ret = ret + "</tr>"
}
else if let tableCell = p as? Table.Cell {
ret = ret + "</td>"
}
else if let para = p as? Paragraph {
ret = ret + "</p>"
}
return ret
}

}

Loading

0 comments on commit f44db30

Please sign in to comment.