Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace embedded cmark-gfm by swift-cmark #348

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 37 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,34 @@ 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)
}
}
103 changes: 0 additions & 103 deletions Sources/cmark-gfm/arena.c

This file was deleted.

Loading
Loading