Code Monkey home page Code Monkey logo

cdmarkdownkit's People

Contributors

chrisdhaan avatar felixlisczyk avatar ismyhc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cdmarkdownkit's Issues

Mention pattern bug

ℹ I want to open a link

ℹ Is it possible?

ℹ Do you have a plan for handling click action?

CDMarkdownKit Environment

CDMarkdownKit version: 2.1.0
Xcode version: 11.6
Swift version: 5
Platform(s) running CDMarkdownKit: iOS
macOS version running Xcode: 10.15.3

Image scaling

What did you do?

Parsed an image link with a large image.

What did you expect to happen?

The image to fit in the textView.

What happened instead?

The image was too large and didn't scale.

CDMarkdownKit Environment

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.

Feature Request

Would it be possible to add Strikethrough and underline?

I use this with Swift 5

Paragraph Styling in CDMarkdownKit

What did you do?

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 Environment

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

Demo Project

Demo Example

Swift 4 Support?

ℹ 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 tag cdmarkdownkit.

Please remove this line and everything above it before submitting.

What did you do?

ℹ 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 Environment

CDMarkdownKit version:
Xcode version:
Swift version:
Platform(s) running CDMarkdownKit:
macOS version running Xcode:

Demo Project

ℹ Please link to or upload a project we can download that reproduces the issue.

Links not rendering

ℹ 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 tag cdmarkdownkit.

Please remove this line and everything above it before submitting.

What did you do?

Tried to parse links

What did you expect to happen?

I expect [Link](somelink.com) to render as Link with whatever styling i set

What happened instead?

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 Environment

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

CDMarkdownKit build failed on XCode 11.4

What did you do?

I've upgraded XCode from 11.3.1 to 11.4

What did you expect to happen?

CDMarkdownKit built with successful results.

What happened instead?

Build failed.
"Switch must be exhaustive on line 301 of CDMarkdownLabel.swift file."
Captura de pantalla 2020-03-25 a las 11 05 31

CDMarkdownKit Environment

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.

CDMarkdownTextView: Load local images

What did you do?

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)

What did you expect to happen?

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.

What happened instead?

No image loaded.

Instead, console told about a NSURLConnection finished with error - code -1002 error. IIUC this is a not supported protocol.

CDMarkdownKit Environment

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.

Demo Project

TBD, matter of time...

Add possibility to use multiple \n in a row

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

Asynchronous images

What did you do?

I have images that are between some paragraphs in my markdown code

What did you expect to happen?

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.

What happened instead?

The images are loading well but they are synchronized with the rest of the rendering. The view takes a while to load.

CDMarkdownKit Environment

CDMarkdownKit version: 1.0
Xcode version: 10.1
Swift version: 3
Platform(s) running CDMarkdownKit: IOS
**macOS version running Xcode:**10.14.3

Crash in CDFont+CDMarkdownKit.swift, line 57. Unexpectedly found nil when unwrapping optional value.

What did you do?

                        if let m = chat.message
                           , let font = UIFont(name: "Lato-Regular", size: 20.0)
                        {   
                            let markdownParser = CDMarkdownParser(font: font)
                        }

What did you expect to happen?

No crash

What happened instead?

Crash in CDFont+CDMarkdownKit.swift, line 57. Unexpectedly found nil when unwrapping optional value.

CDMarkdownKit Environment

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)
}

Swift 4.2 compatibillity

What did you do?

Used CDMarkdownKit in a Swift 4.2/Xcode 10 project

What did you expect to happen?

It should not yield any errors or warnings

What happened instead?

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:

  1. Drop pre-Swift 4.2 support completely
  2. Make Swift 4.2 support available through a new branch
  3. Use conditional compile directives to include different code based on used Swift version.

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 Environment

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

marking down a file doesn't format tables

What did you do?

I followed the documentation and marked down a text file

What did you expect to happen?

I expected the entire file to be marked down including tables

What happened instead?

The entire file was marked down except for the tables in the file

CDMarkdownKit Environment

**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?

Add custom attribute

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

Link not clickable

What did you do?

I've tried the sample project, then I create and render a simple markdown, like "This framework helps with parsing markdown with Link."

What did you expect to happen?

the Link is clickable

What happened instead?

it is not clickable because displayActionController is not triggered <- selectedURLRange = nil <- boundingReact is not contains the correctLocation

image

image

CDMarkdownKit Environment

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

Remove language hint of syntax blocks

What did you do?

Consider the following block

```js
var t = 5;
```

What did you expect to happen?

I would expect it to render as

var t = 5;

What happened instead?

It renders as

js
var t = 5;

CDMarkdownKit Environment

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})(?!`)"

but this is now rendered as
image

Would be nice if the behaviour could be influenced.

Crash with automatic link

What did you do?

ℹ Please replace this with what you did.
This is unclear as I have CDMarkdownKit in a deployed application.

What did you expect to happen?

For it not to crash?

What happened instead?

It crashes with CDMarkdownAutomaticLink.regularExpression()

CDMarkdownKit Environment

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

UITextViewDelegate not called textView(_ textView: UITextView, shouldInteractWith....

What did you do?

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
   }
}


What did you expect to happen?

call delegate method
func textView(_ textView: UITextView, shouldInteractWith markdownURL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

What happened instead?

nothing

CDMarkdownKit Environment

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)

Document WatchKit support

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 :-)

[Privacy Manifest] Implementation

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.