Skip to content

Commit

Permalink
Replace embedded cmark-gfm by swift-cmark
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Oct 10, 2024
1 parent 5544181 commit cd44c34
Show file tree
Hide file tree
Showing 73 changed files with 58 additions and 30,794 deletions.
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "6.0.0"
}
},
{
"identity" : "swift-cmark",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-cmark",
"state" : {
"revision" : "3ccff77b2dc5b96b77db3da0d68d28068593fa53",
"version" : "0.5.0"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
Expand Down
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/gonzalezreal/NetworkImage", from: "6.0.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.10.0"),
.package(url: "https://github.com/swiftlang/swift-cmark", from: "0.4.0"),
],
targets: [
.target(name: "cmark-gfm"),
.target(
name: "MarkdownUI",
dependencies: [
"cmark-gfm",
.product(name: "cmark-gfm", package: "swift-cmark"),
.product(name: "cmark-gfm-extensions", package: "swift-cmark"),
.product(name: "NetworkImage", package: "NetworkImage"),
]
),
Expand Down
47 changes: 43 additions & 4 deletions Sources/MarkdownUI/Parser/MarkdownParser.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
@_implementationOnly import cmark_gfm
@_implementationOnly import cmark_gfm_extensions

extension Array where Element == BlockNode {
init(markdown: String) {
Expand Down Expand Up @@ -317,7 +318,7 @@ extension UnsafeNode {
return node
case .table(let columnAlignments, let rows):
guard let table = cmark_find_syntax_extension("table"),
let node = cmark_node_new_with_ext(CMARK_NODE_TABLE, table)
let node = cmark_node_new_with_ext(ExtensionNodeTypes.shared.CMARK_NODE_TABLE, table)
else {
return nil
}
Expand Down Expand Up @@ -354,7 +355,7 @@ extension UnsafeNode {

fileprivate static func make(_ tableRow: RawTableRow) -> UnsafeNode? {
guard let table = cmark_find_syntax_extension("table"),
let node = cmark_node_new_with_ext(CMARK_NODE_TABLE_ROW, table)
let node = cmark_node_new_with_ext(ExtensionNodeTypes.shared.CMARK_NODE_TABLE_ROW, table)
else {
return nil
}
Expand All @@ -364,7 +365,7 @@ extension UnsafeNode {

fileprivate static func make(_ tableCell: RawTableCell) -> UnsafeNode? {
guard let table = cmark_find_syntax_extension("table"),
let node = cmark_node_new_with_ext(CMARK_NODE_TABLE_CELL, table)
let node = cmark_node_new_with_ext(ExtensionNodeTypes.shared.CMARK_NODE_TABLE_CELL, table)
else {
return nil
}
Expand Down Expand Up @@ -400,7 +401,8 @@ extension UnsafeNode {
return node
case .strikethrough(let children):
guard let strikethrough = cmark_find_syntax_extension("strikethrough"),
let node = cmark_node_new_with_ext(CMARK_NODE_STRIKETHROUGH, strikethrough)
let node = cmark_node_new_with_ext(
ExtensionNodeTypes.shared.CMARK_NODE_STRIKETHROUGH, strikethrough)
else {
return nil
}
Expand Down Expand Up @@ -480,3 +482,40 @@ private struct UnsafeNodeSequence: Sequence {
.init(self.node)
}
}

// Extension node types are not exported in `cmark_gfm_extensions`,
// so we need to look for them in the symbol table
private struct ExtensionNodeTypes {
let CMARK_NODE_TABLE: cmark_node_type
let CMARK_NODE_TABLE_ROW: cmark_node_type
let CMARK_NODE_TABLE_CELL: cmark_node_type
let CMARK_NODE_STRIKETHROUGH: cmark_node_type

static let shared = ExtensionNodeTypes()

private init() {
func findNodeType(_ name: String, in handle: UnsafeMutableRawPointer!) -> cmark_node_type? {
guard let symbol = dlsym(handle, name) else {
return nil
}
return symbol.assumingMemoryBound(to: cmark_node_type.self).pointee
}

let handle = dlopen(nil, RTLD_LAZY)

self.CMARK_NODE_TABLE = findNodeType("CMARK_NODE_TABLE", in: handle) ?? CMARK_NODE_NONE
self.CMARK_NODE_TABLE_ROW = findNodeType("CMARK_NODE_TABLE_ROW", in: handle) ?? CMARK_NODE_NONE
self.CMARK_NODE_TABLE_CELL =
findNodeType("CMARK_NODE_TABLE_CELL", in: handle) ?? CMARK_NODE_NONE
self.CMARK_NODE_STRIKETHROUGH =
findNodeType("CMARK_NODE_STRIKETHROUGH", in: handle) ?? CMARK_NODE_NONE

dlclose(handle)

// TODO: delete this
print("CMARK_NODE_TABLE:\(self.CMARK_NODE_TABLE)")
print("CMARK_NODE_TABLE_ROW:\(self.CMARK_NODE_TABLE_ROW)")
print("CMARK_NODE_TABLE_CELL:\(self.CMARK_NODE_TABLE_CELL)")
print("CMARK_NODE_STRIKETHROUGH:\(self.CMARK_NODE_STRIKETHROUGH)")
}
}
103 changes: 0 additions & 103 deletions Sources/cmark-gfm/arena.c

This file was deleted.

Loading

0 comments on commit cd44c34

Please sign in to comment.