GithubHelp home page GithubHelp logo

bmoliveira / markdownkit Goto Github PK

View Code? Open in Web Editor NEW
763.0 763.0 136.0 595 KB

A simple and customizable Markdown Parser for Swift

License: MIT License

Swift 95.01% Objective-C 2.15% Ruby 2.84%

markdownkit's People

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

markdownkit's Issues

Example does not compile

Hello,

I'm just trying to use your code. I download Example project but I have a compilation Error

ld: warning: directory not found for option '-F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.3.sdk/Developer/Library/Frameworks'

Can you please help

Incorrect font size for bold text inside header

Hi, I've noticed that the following markdown text is not rendering as expected:

# Heading with **bold text**

The bold part of the text is being rescaled down, instead of maintaining the header font size.

Any way to change lineHeight?

Hi,

I would like to use that lib to format my label.
The idea is that I have a structure like this:
title1
Description1

title2
Description2

etc...

I would like to have some space between the title and the description. one "\n" is not enough and "\n\n" is too much.
Do you have any idea about how I could do that?

Errors occur when installing via Swift Package Manager

If you go to File > Swift Packages > Add Package Dependency, paste in https://github.com/bmoliveira/MarkdownKit.git, click Next, leave it at the default up to 1.4.1 rule, click Next, you'll get an error stating:

https://github.com/bmoliveira/MarkdownKit.git has no Package.swift manifest for version 1.4.1 in https://github.com/bmoliveira/MarkdownKit.git

If you instead put in 1.7.0 as is implied in the Readme, you'll get an error stating:

Dependencies could not be resolved because no versions of 'MarkdownKit' match the requirement 1.7.0..<2.0.0 and root depends on 'MarkdownKit' 1.7.0..<2.0.0.

Screen Shot 2021-03-22 at 1 33 50 PM

Markdown Not Working In Sentence

Hi,
I have a problem with markdown. when i use single word like ' Hello ' word style correctly will be changed to bold. but when i use a word in a sentences like ' Test Hello Test ' bold style not working and sentence will be shown 'Test Hello Test '

Customizing default elements

I have the need for the header element to have a specific line height (added as an NSAttributedString attribute during parsing). I'm not seeing a straightforward way to do this. Have thought about:

  1. Subclass MarkdownHeader and replace MarkdownParser.header with subclass instance. However, MarkdownParser.header is defined with let and can't be updated.

  2. I could add a custom element with the same regex to replace the header-defined text before the default element could parse it. However, custom elements are currently parsed after default elements.

Are there other solutions I'm not seeing. If I were to fork, is there a suggested option that would be more acceptable to a pull request back into core project?

Thanks for all the work so far. Like having a lightweight and Swift option for Markdown.

  • josh jacob

wrong regex

MarkdownBold does not work for "(**BOLD**)" string, but works for "( **BOLD**)"

fixed by using custom element:

class MarkdownWorkingBold: MarkdownBold {
    
    private static let regex = "(\\s+|^|\\B)(\\*\\*|__)(.+?)(\\2)"
    
    override var regex: String {
        return MarkdownWorkingBold.regex
    }
}

How to set the custom font for regular text in Swift 3.0

I am unable to set the custom font for the regular text. My font file only contains a single font style, so it crashes at initialisation of MarkdownParser where it is unable to find its corresponding MarkdownBold font. Previously in Swift 2.3, it used to use the font set to UILable, but with the update to Swift 3.0, it no longer supports that. Any idea how to support that? Here is how I initialised MarkdownParser

let markdownParser = MarkdownParser(font: UIFont(name: "Some Custom Font Name", size: 16.0))

Public init functions for Escaping classes are missing

The init functions in MarkdownCodeEscaping, MarkdownEscaping and MarkdownUnescaping are not public (default constructors are in 'internal' protection level). Hence the code outside of the pod cannot create instances of these classes, and other open functions in those classes are not really customizable. Thanks.

() is URL

What if an URL has brackets, such as a lot of Wikipedia URLs? Currently, those don't get parsed correctly.

Enable autoHyperlink ?

Is it possible to enable auto hyperlink?

The text used for MarkdownKit is get dynamically. I would like that the links (ex: https://youtbe.com) are transformed automatically to links clickable, is it possible?

App crashes when running MarkdownKit version 1.6

After upgrading from version 1.5 to 1.6 via cocoapods, my app crashed when trying to parse markdown text. The error was thrown when this method in the MarkdownParser was called:

  open func removeCustomElement(_ element: MarkdownElement) {
    guard let index = customElements.index(where: { someElement -> Bool in
      return element === someElement
    }) else {
      return
    }
    customElements.remove(at: index)
  }

The error was because element was not retained. Downgrading to 1.5 fixed the issue for me. Please let me know if I can provide any more helpful information.

App Crash on MarkdownLink.formatText()

Our app relies on MarkdownKit for parsing and formatting articles, some of which containing links. Through crash reports, we learned that some of our users are experiencing crashes in MarkdownLink's formatText() method. We have been unable to reproduce this error.

Details of the stack trace of our crash are shown below:

image

Any insights as to why this is occurring, or how we should go about troubleshooting this crash?

Thank you for your help.

@bmoliveira

App crashes when launching

Any app to which I add MarkdownKit began to crash during lunchtime on an iOS device (works fine on simulator). Xcode Version 11.3.1 (11C504), iOS 13.3.1, MarkdownKit 1.6. The error messages are below:

dyld: Library not loaded: @rpath/MarkdownKit.framework/MarkdownKit
  Referenced from: /private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/fdslakfjdslkfj
  Reason: no suitable image found.  Did find:
	/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit: code signature invalid for '/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit'

	/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit: stat() failed with errno=25
	/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit: code signature invalid for '/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit'

	/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit: stat() failed with errno=1
	/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit: code signature invalid for '/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit'

	/private/var/containers/Bundle/Application/8B2E7228-0DAD-45E6-B85E-3E5473770964/fdslakfjdslkfj.app/Frameworks/MarkdownKit.framework/MarkdownKit: stat() failed with errno=1

Link color does not work?

Hi! I was glad to find this project, I was trying to use Down, but it's attributed string rendering leaves much to be desired. I thought this seemed like a great way to get a dark mode friendly Markdown parser into my project... and it is! So, thank you.

Unfortunately there appears to be a problem with link colors. Here's some example code...

var textColor: UIColor = .black
if #available(iOS 13.0, *) {
    textColor = .label
}
let markdownParser = MarkdownParser(font: UIFont.preferredFont(forTextStyle: .body), color: textColor)
markdownParser.link.color = textColor
return markdownParser.parse("A string [with a link](http://foo.com), and so on.")

While the main content of the string is colored properly no matter what, the link color appears to be stuck as a black color, even without the link.color call. I've tried link.color = .red as well, and no dice.

Markdown element link will lead to NSInvalidArgumentException

Error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The specified URL has an unsupported scheme. Only HTTP and HTTPS URLs are supported.

I tried entering apple.com, http://apple.com, or https://apple.com inside my app's text view. They all still lead to the same exception.

I put several print statements inside your formatText(_:range:link:) function within MarkdownLink.swift file:

open func formatText(_ attributedString: NSMutableAttributedString, range: NSRange,
                         link: String) {
    print("link:\(link)")
    guard let encodedLink = link.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)
      else {
      return
    }
    print("encoded link: \(encodedLink)")
    guard let url = URL(string: link) ?? URL(string: encodedLink) else { return }
    print("url: \(url)")
    attributedString.addAttribute(NSLinkAttributeName, value: url, range: range)
  }

and get the following output in the console, which shows no problem:

link:https://apple.com
encoded link: https:%2F%2Fapple.com
url: https://apple.com

Abandoned????

Since the maintainer appears to have abandoned this, I have cloned and renamed this project, and merged most of the outstanding PRs manually. I also support Swift 4. New PRs are welcomed as well. https://github.com/davidlari/Haring

Problem with Carthage

When I try to install MarkdownKit via Carthage I get the following error:

Dependency "MarkdownKit" has no shared framework schemes

Font size

Hello,

Tank you for your cool Pod.

I wanna know if there is a way to define font size

regards
seb

List Identation

I was trying this library and faced the issue that lists doesn't have the correct indentation, like when the content of a line from a list breaks line, the second line starts below the "โ—" indicator.

What I would expect is to start below of the first character fo the first line

Combined bold and italic formatting doesn't work as expected.

First of all: Awesome framework, thank you for open-sourcing it! ๐Ÿ‘


When nesting emphasis and strong delimiters (i.e. bold and italic delimiters) as follows:

_**word**_

I would expect word to be formatted both bold and italic:

word

This is specified in the CommonMark Spec and as you can see above, it's also the default behavior on GitHub.

The MarkdownKit parser currently formats the Markdown input above as only italic:

word

I would suggest to change the parser's behavior according to the specification.

Color of "regular" text

In my case I have this string that is displayed as a button label.
"Already registered? **Login**"

       let parser = MarkdownParser(font: UIFont.systemFont(ofSize: 17))
        parser.automaticLinkDetectionEnabled = false
        parser.bold.color = UIColor.white
        parser.header.color = UIColor.white

My problem is that "Already registered?" is displayed in blue and "Login" is white as set by bold-attribute. How can the color for regular text be set? Why is it set to blue?

H1, H2... sizes the wrong way around

I believe it's the case that markdown standard means number of hashes equates to Hn index, which is inversely proportional to font size.

So

Largest header

Smaller header

Smallest header

However, MarkdownKit by default has this the wrong way around, ### is the largest font.

Links and parentheses don't work well together

([linkme](schema://domain/whees)) results in (linkme -> schema://domain/whees) where one would expect (linkme) -> schema://domain/whees.

This can be fixed by making the link regex not greedy. The downside is that embedded parentheses in links will not be parsed correctly.

Another approach is to try to balance the bracket: any closing bracket at the end of the link that's is not matched by an opening bracket will be assumed to outside of the actual link. This heuristic seems to work well in practice. I've made a PR for this (#79).

Before (notice how the first linkme is broken):
Screenshot 2021-02-02 at 22 49 34

After (notice how all links now work correctly):
Screenshot 2021-02-02 at 22 50 22

list bullet color in dark mode

It's seems that the list bullet color is always black? When I use a color in a list for dark mode, the text color in the list changed, but the bullets didn't.

Code Element have some corner cases not handled.

I've found at least 2 bugs with the Code element:

  • When the language that the code block is specified is added it's not being interpreted as a Markdown character and its interpreted as content.

Example:

func bug() {}

shows as:

swift
func bug() {}
  • When there are other ` surrounding other expressions the surrounding expression disappear

Example:

/// lets test `this` out

show as

/// lets test out

App crash on link markdown

The app is crashing on this format of link:
[(William) Davenport Griffen](/artists/(william)-davenport-griffen)

The issue is with the name William in brackets. It seems that the framework treats (William) as a separate link.. instead of just a plain text.
The app is crashing in the MarkdownLink.swift file on formatText function.

@bmoliveira

Links without explicit schemes formatted but not working

Steps to reproduce

  1. Parse a markdown text containing links without explicit schemes:
let markdownParser = MarkdownParser()
let markdown = """
    Link with implicit scheme: old.reddit.com
    Link with explicit scheme: https://old.reddit.com/
    """
textView.attributedText = markdownParser.parse(markdown)
  1. Observe that the link with the implicit scheme is formatted as a link
  2. Select the link

Expected behaviour
Either the link is opened in Safari,
or the text is not formatted as a link at all

Actual behaviour

  • The link is formatted as a link
  • Selecting the link does nothing
  • Printed to console: Could not find any actions for URL old.reddit.com without any result.

My suggestion would be to have an option for a default scheme for links that is added if the scheme is nil.

Manual Installation

CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

Crash in `String+UTF16.swift` - `unescapeUTF16()`

I have experienced a crash in the unescapeUTF16 method in String+UTF16.swift. The markdown that led to the crash can be found here: https://github.com/apple/swift-evolution/blob/master/proposals/0240-ordered-collection-diffing.md

The crash was found on the line let endIndex = index(self.startIndex, offsetBy: $0 + 4) in the following code snippet:

stride(from: 0, to: count, by: 4).forEach {
    let startIndex = index(self.startIndex, offsetBy: $0)
    let endIndex = index(self.startIndex, offsetBy: $0 + 4)
    let hex4 = String(self[startIndex..<endIndex])
    if let utf16 = UInt16(hex4, radix: 16) {
        utf16Array.append(utf16)
    }
}

Carthage Install Failing

I'm trying to install via Carthage on Xcode 12.2 and keep getting this error:

*** Building scheme "MarkdownKit-AppKit" in MarkdownKit.xcworkspace
*** Building scheme "MarkdownKit" in MarkdownKit.xcworkspace
Build Failed
	Task failed with exit code 1:
	/usr/bin/xcrun lipo -create /Users/tj/Library/Caches/org.carthage.CarthageKit/DerivedData/12.2_12B45b/MarkdownKit/1.7.0/Build/Intermediates.noindex/ArchiveIntermediates/MarkdownKit/IntermediateBuildFilesPath/UninstalledProducts/iphoneos/MarkdownKit.framework/MarkdownKit /Users/tj/Library/Caches/org.carthage.CarthageKit/DerivedData/12.2_12B45b/MarkdownKit/1.7.0/Build/Products/Release-iphonesimulator/MarkdownKit.framework/MarkdownKit -output /Volumes/VNStorage/Repos/iosSwitchbacks/switchbacks-ios/Carthage/Build/iOS/MarkdownKit.framework/MarkdownKit

This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/yt/zk5_9dxd08b_4gpddjpl6qx00000gn/T/carthage-xcodebuild.RtHQE3.log

Is this because Xcode 12.2 (Swift 5.3.1) is not supported? Or maybe arm64 architecture needs to be excluded?

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.