-
Notifications
You must be signed in to change notification settings - Fork 7
Migrate MarkdownSyntax to swift-cmark #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Sources/MarkdownSyntax/CMark/CMNode+PositionAdjustment.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // | ||
| // CMNode+PositionAdjustment.swift | ||
| // MarkdownSyntax | ||
| // | ||
| // Created for swift-cmark migration compatibility | ||
| // | ||
| // This file compensates for position calculation differences between swift-cmark-gfm | ||
| // and swift-cmark. The new library changed how it reports positions for certain elements: | ||
| // | ||
| // 1. GFM autolinks (autolink.c:275): Changed from `start - rewind` to `max_rewind - rewind` | ||
| // Result: Off-by-one error in start column | ||
| // | ||
| // 2. Footnote definitions: Positions now exclude the [^label]: prefix | ||
| // | ||
|
|
||
| extension CMNode { | ||
|
|
||
| /// Adjusts position to restore syntax delimiters that swift-cmark now excludes. | ||
| /// | ||
| /// - Parameters: | ||
| /// - text: The source markdown text | ||
| /// - lineOffsets: Line offset indices | ||
| /// - Returns: Position adjusted to include syntax delimiters | ||
| func adjustedPosition(in text: String, using lineOffsets: [String.Index]) -> Position { | ||
| let pos = position(in: text, using: lineOffsets) | ||
|
|
||
| guard let startOffset = pos.start.offset, | ||
| let endOffset = pos.end.offset, | ||
| startOffset >= text.startIndex, | ||
| endOffset < text.endIndex else { | ||
| return pos | ||
| } | ||
|
|
||
| switch type { | ||
| case .link where isAutolink(): | ||
| return adjustAutolinkPosition(pos, in: text, startOffset: startOffset, endOffset: endOffset) | ||
|
|
||
| case .footnoteDefinition: | ||
| return adjustFootnotePosition(pos, in: text, startOffset: startOffset) | ||
|
|
||
| default: | ||
| return pos | ||
| } | ||
| } | ||
|
|
||
| /// Adjusts autolink position for GFM bare URLs only. | ||
| /// Angle bracket autolinks like <http://example.com> don't need adjustment. | ||
| /// Fix off-by-one: GFM bare URL position includes character before URL | ||
| private func adjustAutolinkPosition( | ||
| _ pos: Position, | ||
| in text: String, | ||
| startOffset: String.Index, | ||
| endOffset: String.Index | ||
| ) -> Position { | ||
| guard startOffset > text.startIndex else { | ||
| return pos | ||
| } | ||
|
|
||
| // Fix off-by-one for GFM bare URLs: position includes character before URL | ||
| let adjustedStart = text.utf8.index(startOffset, offsetBy: 1, limitedBy: endOffset) ?? startOffset | ||
| return Position( | ||
| start: Point(line: pos.start.line, column: pos.start.column + 1, offset: adjustedStart), | ||
| end: pos.end, | ||
| indent: pos.indent | ||
| ) | ||
| } | ||
|
|
||
| /// Checks if this is a bare URL autolink (not an explicit Markdown link). | ||
| /// GFM autolinks have their URL as the child text content. | ||
| private func isAutolink() -> Bool { | ||
| guard let childText = firstChild?.literal, let linkURLString = linkDestination else { | ||
| return false | ||
| } | ||
|
|
||
| // GFM expands www.example.com to http://www.example.com | ||
| return childText == linkURLString || linkURLString.hasSuffix(childText) | ||
| } | ||
|
|
||
| /// Adjusts footnote definition position to include [^label]: prefix. | ||
| /// Searches backwards up to 100 chars to find the footnote marker. | ||
| private func adjustFootnotePosition( | ||
| _ pos: Position, | ||
| in text: String, | ||
| startOffset: String.Index | ||
| ) -> Position { | ||
| guard startOffset > text.startIndex else { return pos } | ||
|
|
||
| let searchLimit = text.utf8.index(startOffset, offsetBy: -100, limitedBy: text.startIndex) ?? text.startIndex | ||
| let searchRange = searchLimit..<startOffset | ||
|
|
||
| // Use native backward search for the [^ pattern | ||
| if let range = text.range(of: "[^", options: .backwards, range: searchRange) { | ||
| let distance = text.utf8.distance(from: range.lowerBound, to: startOffset) | ||
| return Position( | ||
| start: Point(line: pos.start.line, column: pos.start.column - distance, offset: range.lowerBound), | ||
| end: pos.end, | ||
| indent: pos.indent | ||
| ) | ||
| } | ||
|
|
||
| return pos | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If swiftlang/swift-cmark#84 gets merged, add this test back into the suite.