chrisdhaan / cdmarkdownkit Goto Github PK
View Code? Open in Web Editor NEWAn extensive Swift framework providing simple and customizable markdown parsing.
License: MIT License
An extensive Swift framework providing simple and customizable markdown parsing.
License: MIT License
I have images that are between some paragraphs in my markdown code
I want to load asynchronously my images. I've tried to change the match function with a DispatchQueue.main.async in the CDMarkdownImage.swift but it doesn't display the images at all.
The images are loading well but they are synchronized with the rest of the rendering. The view takes a while to load.
CDMarkdownKit version: 1.0
Xcode version: 10.1
Swift version: 3
Platform(s) running CDMarkdownKit: IOS
**macOS version running Xcode:**10.14.3
Used CDMarkdownKit in a Swift 4.2/Xcode 10 project
It should not yield any errors or warnings
It yields numerous compile errors, primarily related to NSAttributedStringKey
& UIFontDescriptorSymbolicTraits
.
CDMarkdownKit/Source/CDFont.swift:33:55: 'UIFontDescriptorSymbolicTraits' has been renamed to 'UIFontDescriptor.SymbolicTraits'
CDMarkdownKit/Source/CDMarkdownStyle.swift:50:24: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownStyle.swift:53:24: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownStyle.swift:56:24: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownStyle.swift:59:24: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownSyntax.swift:88:50: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownSyntax.swift:112:47: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownLink.swift:71:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownList.swift:108:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownParser.swift:193:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownParser.swift:196:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownParser.swift:199:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownParser.swift:202:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownHeader.swift:118:24: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownImage.swift:76:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownLabel.swift:333:39: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
CDMarkdownKit/Source/CDMarkdownLabel.swift:390:60: 'NSAttributedStringKey' has been renamed to 'NSAttributedString.Key'
/Foundation.NSAttributedStringKey:2:18: 'NSAttributedStringKey' was obsoleted in Swift 4.2
I can submit a PR for this, but I'd like to hear from @chrisdhaan what approach he wants to follow.
Possible approaches:
The last option is probably the right one judging on past use of this approach. But I'd like to know for certain before I submit a PR to avoid unnecessary work.
CDMarkdownKit version: 1.2.0
Xcode version: 10.0
Swift version: 4.2
Platform(s) running CDMarkdownKit: iOS 12
macOS version running Xcode: 10.13.6
I followed the documentation and marked down a text file
I expected the entire file to be marked down including tables
The entire file was marked down except for the tables in the file
**CDMarkdownKit version:2.1.0
**Xcode version:12.5
**Swift version:5
**Platform(s) running CDMarkdownKit:14.5
**macOS version running Xcode:11.3.1
Is support for tables going to be included in future?
Hello,
I need a lib for Swift markdown parser/display where when interpreting a link where the href is "ubook-app://product/123", a custom view is placed inside that displays a loading, then loads the product by the link id (ex: 123), and then show the product cover, title and two buttons (read/listen) within the view.
Can you tell me how to do this with this component?
Thanks.
final class MarkDownItemView: StackItemView<MarkDownItemView.Style> {
override var arrangedViews: [UIView] {
[markdownView]
}
private lazy var markdownView: CDMarkdownTextView = { [unowned self] in
let textContainer = NSTextContainer(size: .zero)
let layoutManager = CDMarkdownLayoutManager()
layoutManager.addTextContainer(textContainer)
let codeTextView = CDMarkdownTextView(frame: .zero,
textContainer: textContainer,
layoutManager: layoutManager)
codeTextView.backgroundColor = .clear
codeTextView.isScrollEnabled = false
codeTextView.isEditable = false
codeTextView.isSelectable = true
codeTextView.delegate = self
return codeTextView
}()
private var model: MarkdownTitleCellModelType?
override func applyStyle(_ style: Style) {
setContentInsets(style.contentInsets)
}
}
extension MarkDownItemView {
struct Style {
let headerFont: UIFont
let font: UIFont
let boldFont: UIFont
let italicFont: UIFont
let linkColor: UIColor
let fontColor: UIColor
var contentInsets: UIEdgeInsets = .single(inset: 16)
static let `default`: Style = .init(
headerFont: Asset.Font.withStyle(.medium, size: .caption),
font: Asset.Font.withStyle(.regular, size: .caption),
boldFont: Asset.Font.withStyle(.medium, size: .caption),
italicFont: Asset.Font.withStyle(.light, size: .body),
linkColor: ColorPalette.Purple.darkPrimary.color,
fontColor: ColorPalette.Black.primary.color
)
}
func setup(model: MarkdownTitleCellModelType) {
guard let style = styles[model.itemViewState] else {
return
}
self.model = model
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 3
paragraphStyle.paragraphSpacingBefore = 12
paragraphStyle.lineSpacing = 1.38
let markdownParser = CDMarkdownParser(
font: style.font,
boldFont: style.boldFont,
italicFont: style.italicFont,
fontColor: style.fontColor,
paragraphStyle: paragraphStyle
)
markdownParser.header.font = style.headerFont
markdownParser.header.fontIncrease = 1
let paragraphStyleList = NSMutableParagraphStyle()
paragraphStyleList.paragraphSpacing = 3
paragraphStyleList.paragraphSpacingBefore = 0
paragraphStyleList.lineSpacing = 1.38
markdownParser.list.paragraphStyle = paragraphStyleList
markdownView.linkTextAttributes = [.foregroundColor: style.linkColor]
markdownView.attributedText = markdownParser.parse(model.title)
restyle(state: model.itemViewState)
}
}
extension MarkDownItemView: UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith markdownURL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if ValidationRule(ruleType: .email).validate(against: markdownURL.absoluteString) {
model?.emailHandler(markdownURL.absoluteString)
} else if UIApplication.shared.canOpenURL(markdownURL) {
UIApplication.shared.open(markdownURL, options: [:])
}
return false
}
}
call delegate method
func textView(_ textView: UITextView, shouldInteractWith markdownURL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
nothing
CDMarkdownKit version: 2.2.0 or 2.3.0 same behaviour
Xcode version: 14.1 (14B47b)
Swift version: 5.0
Platform(s) running CDMarkdownKit: ios 16
macOS version running Xcode: 13.0 (22A380)
ℹ I want to open a link
ℹ Is it possible?
ℹ Do you have a plan for handling click action?
CDMarkdownKit version: 2.1.0
Xcode version: 11.6
Swift version: 5
Platform(s) running CDMarkdownKit: iOS
macOS version running Xcode: 10.15.3
Hello,
As we approach the upcoming spring and the anticipated implementation of the Privacy Manifest by Apple, it's crucial to ensure that your SDK fully complies with Apple's requirements.
Does your SDK need to include it?
If so, do you have a release date planned?
Thank you in advance for your attention to this matter.
Best regards
ℹ Please replace this with what you did.
This is unclear as I have CDMarkdownKit in a deployed application.
For it not to crash?
It crashes with CDMarkdownAutomaticLink.regularExpression()
CDMarkdownKit version:
Xcode version: 9.3
Swift version: 4.1
Platform(s) running CDMarkdownKit: watchOS
macOS version running Xcode: Latest
Here is the report: https://pastebin.com/umuVpw8c
Parsed an image link with a large image.
The image to fit in the textView.
The image was too large and didn't scale.
CDMarkdownKit version: 0.9.7
Xcode version: 9.2 (9C40b)
Swift version: 3.2
Platform(s) running CDMarkdownKit: iOS
macOS version running Xcode: MacOS Sierra 10.12.6
Possible solution to this is to pass a desired frame to CDMarkdownParser
and initialize CDMarkdownImage
with it. Then, scale the attachment size to fit the passed frame.
private func adjustAttachmentSize(_ attachment: NSTextAttachment, image: UIImage) {
guard let frame = frame else {
return
}
// about 10 point padding so the image will fit
let preferredWidth = frame.width - 10
let widthScalingFactor = image.size.width / preferredWidth
attachment.bounds = CGRect(x: 0.0,
y: 0.0,
width: image.size.width / widthScalingFactor,
height: image.size.height / widthScalingFactor)
}
Another possible solution is to parse customElements
before defaultElements
in CDMarkdownParser
. Then developers have an option to create a custom class for scaled images and pass that as a custom element.
Tried to use a local image (added to main bundle) referencing it via:
![my image](my-image.png)
![my image](./my-image.png)
![my image](file://my-image.png)
![my image](file://./my-image.png)
The image should at least once be loaded like an external image e.g.
![my external image](http://www.christopherdehaan.me/public/img/circle-hover.png)
which works fine.
No image loaded.
Instead, console told about a NSURLConnection finished with error - code -1002 error. IIUC this is a not supported protocol.
CDMarkdownKit version: 0.9.7
Xcode version: 9.2 (9C40b)
Swift version: 4.0.3
Platform(s) running CDMarkdownKit: iOS Simulator w/ iOS 11.2 (15C107)
macOS version running Xcode:: 10.13.2 (17C205)
I used CDMarkdownTextView and could successfully load a local image with:
let myImage = UIImage(named: "my-image.png")
let attachment: NSTextAttachment = NSTextAttachment()
attachment.image = thumbnailImage
let attributedImageString: NSAttributedString = NSAttributedString(attachment: attachment)
cdMarkdownTextView.textStorage.append(attributedImageString)
Rough guess it's related to CDMarkdownImage's url handling.
TBD, matter of time...
Would it be possible to add Strikethrough and underline?
I use this with Swift 5
Thank you a lot for this library, i want to suggest small improvement that will be very helpful
Right now i can't use multiple \n
in a row, because library replacing all consecutive \n
into one \n
It can be solved adding bool flag in constructor which says if parser should replace \n
or not
for example
let detailLocation = gesture.location(in: contentLabel)
let storage = NSTextStorage(attributedString: contentLabel.attributedText)
let manager = NSLayoutManager()
storage.addLayoutManager(manager)
let container = NSTextContainer(size: CGSize(width: contentLabel.bounds.size.width, height: CGFloat.greatestFiniteMagnitude))
container.lineFragmentPadding = 0
container.maximumNumberOfLines = contentLabel.numberOfLines
container.lineBreakMode = contentLabel.lineBreakMode
manager.addTextContainer(container)
let index = manager.characterIndex(for: detailLocation, in: container, fractionOfDistanceBetweenInsertionPoints: nil)
let attributes = attributedText.attributes(at: index, effectiveRange: nil)
if let attrbuteValue = attributes[CustomAttributedKey] as? String {
print("url is ", attrbuteValue)
}
and other type, at(@xxx) image preview etc.
custome attrbutes interface is needed
ℹ Please fill out this template when filing an issue.
All lines beginning with an ℹ symbol instruct you with what info we expect. You can delete those lines once you've filled in the info.Per our *CONTRIBUTING guidelines, we use GitHub for
bugs and feature requests, not general support. Other issues should be opened on Stack Overflow with the tagcdmarkdownkit
.Please remove this line and everything above it before submitting.
ℹ Please replace this with what you did.
I expected Swift 4 to work
ℹ Please replace this with what you expected to happen.
It crashed with numerous errors, some fixable, others not.
ℹ Please replace this with of what happened instead.
CDMarkdownKit version:
Xcode version:
Swift version:
Platform(s) running CDMarkdownKit:
macOS version running Xcode:
ℹ Please link to or upload a project we can download that reproduces the issue.
I've tried the sample project, then I create and render a simple markdown, like "This framework helps with parsing markdown
with Link."
the Link
is clickable
it is not clickable because displayActionController
is not triggered <- selectedURLRange
= nil <- boundingReact
is not contains the correctLocation
CDMarkdownKit version: 2.3.0
Xcode version: 14.0.1
Swift version: swift-driver version: 1.62.8 Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50)
Platform(s) running CDMarkdownKit: iOS
macOS version running Xcode: 12.6
if let m = chat.message
, let font = UIFont(name: "Lato-Regular", size: 20.0)
{
let markdownParser = CDMarkdownParser(font: font)
}
No crash
Crash in CDFont+CDMarkdownKit.swift, line 57. Unexpectedly found nil when unwrapping optional value.
CDMarkdownKit v2.1.1
xcode 12.5.1
swift v5.0
iOS Simulator
macOS 11.4
This is the CDMarkdownKit code that crashed, it crashed on the force unwrap of "descriptor" variable.
private func withTraits(_ traits: CDFontDescriptorSymbolicTraits...) -> CDFont {
let descriptor = fontDescriptor.withSymbolicTraits(CDFontDescriptorSymbolicTraits(traits))
return CDFont(descriptor: descriptor!,
size: self.pointSize)
}
Proposal for controlling the paragraph styling in CDMarkdownKit
I looked into paragraph styling that was in the old pull request and really liked his solution for paragraph styling. The only changes I made were the math to do number of lines before and after and exposing several variable for configuration. I think there might need to be some work done to pass the paragraph style along to certain classes like CDMarkdownQuote.swift
as the default styling may look weird after you add the Quote style.
open var linesBeforeParagraph: CGFloat = 0
open var linesAfterParagraph: CGFloat = 1
open var lineSpacing: CGFloat = 1.38
let paraStyle = NSMutableParagraphStyle()
paraStyle.paragraphSpacingBefore = font.pointSize * linesBeforeParagraph
paraStyle.paragraphSpacing = font.pointSize * linesAfterParagraph
paraStyle.lineSpacing = lineSpacing
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paraStyle, range: range)
CDMarkdownKit version: 1.0.0
Xcode version: Current
Swift version: 4.0
Platform(s) running CDMarkdownKit: Tested with iOS
macOS version running Xcode: Have not tested for macOS
Consider the following block
```js
var t = 5;
```
I would expect it to render as
var t = 5;
It renders as
js
var t = 5;
CDMarkdownKit version: 2.5.1
Xcode version: 14.3.1
Swift version: 5.8.1
Platform(s) running CDMarkdownKit: iOS 15/16
macOS version running Xcode: Ventura 13.4.1
I tried to solve this myself and changed the regex inside of CDMarkdownSyntax
from
fileprivate static let regex = "(\\s+|^)(`{3})(\\s*[^`]*?\\s*)(\\2)(?!`)"
to
fileprivate static let regex = "(\\s+|^)(`{3}[^\\n]*?\\n)(\\s*[^`]*?\\s*)(`{3})(?!`)"
Would be nice if the behaviour could be influenced.
I've upgraded XCode from 11.3.1 to 11.4
CDMarkdownKit built with successful results.
Build failed.
"Switch must be exhaustive on line 301 of CDMarkdownLabel.swift file."
CDMarkdownKit version: 1.2.1
Xcode version: 11.4
Swift version: 5
Platform(s) running CDMarkdownKit: iOS 13.3.1
macOS version running Xcode: Catalina 10.15.3 (19D76)
Best regards.
Thanks for creating this project
I experimented with this on WatchKit, and I found that the only API that works is setting attributed text on a WKInterfaceLabel
, and that there seems to be no support for links, images, quote or code. Can you confirm this?
For this subset though, it works very well :-)
ℹ Please fill out this template when filing an issue.
All lines beginning with an ℹ symbol instruct you with what info we expect. You can delete those lines once you've filled in the info.Per our *CONTRIBUTING guidelines, we use GitHub for
bugs and feature requests, not general support. Other issues should be opened on Stack Overflow with the tagcdmarkdownkit
.Please remove this line and everything above it before submitting.
Tried to parse links
I expect [Link](somelink.com)
to render as Link with whatever styling i set
The styling is not applied. Also if the link is the only thing being parsed it is not recognised as a link as the regex expects it to be prefixed with some character.
CDMarkdownKit version: 1.2.1
Xcode version: 10.2
Swift version: 4.2
Platform(s) running CDMarkdownKit: iOS 12.1 iPhone XS
macOS version running Xcode: 10.14.3, macOS Mojave
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.