Giter Site home page Giter Site logo

objecthub / swift-markdownkit Goto Github PK

View Code? Open in Web Editor NEW
126.0 126.0 19.0 165 KB

A framework for parsing and transforming text in Markdown format written in Swift 5 for macOS, iOS, and Linux. The syntax is based on the CommonMark specification. The framework defines an abstract syntax for Markdown, provides a parser for parsing strings into abstract syntax trees, and comes with generators for HTML and attributed strings.

License: Apache License 2.0

Swift 99.73% Objective-C 0.27%
commonmark ios linux macos markdown markdown-converter markdown-parser swift

swift-markdownkit's People

Contributors

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

swift-markdownkit's Issues

Display local images

Great package and really useful, but I was wondering is there a way to display local images?

Setting up an image with a web URL works perfectly, but using a local URL, even if I start it with file:// does not translate to an image block.

NSAttributedStringGenerator not found on Linux

Hi, I'm using this package to render markdown to HTML in a Vapor project. Running it on a Mac works fine, but depploying it to Heroku gives the following error message:

AttributedStringGenerator.swift

/tmp/build_ffeabf09/.build/checkouts/swift-markdownkit/Sources/MarkdownKit/CustomBlock.swift:32:33: error: cannot find type 'AttributedStringGenerator' in scope
                    and attGen: AttributedStringGenerator?,
                                ^~~~~~~~~~~~~~~~~~~~~~~~~

/tmp/build_ffeabf09/.build/checkouts/swift-markdownkit/Sources/MarkdownKit/CustomTextFragment.swift:31:62: error: cannot find type 'AttributedStringGenerator' in scope
  func generateHtml(via htmlGen: HtmlGenerator, and attrGen: AttributedStringGenerator?) -> String
                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~

Any possibility this will get fixed in the future?

How to get source ranges in AST?

This is an awesome project which is not a simple wrapper around CMark and done the Swift way.
I am wonering a question: Is there a way to get source text ranges when iterating over the AST?

Linux compatibility?

The core parser and HTML renderer seem to just rely on Foundation, so they should run on Linux as well. Yet because of the AttributedString renderer the supported platforms are limited to .macOS(.v10_12) and .iOS(.v12). Could this possibly be split up into a separate package? Or maybe there is another way to achieve the same result, namely that this package can be used in pure Swift libraries that run on CI.

Example of creating a simple inline parser?

Let's say I want to add support for ~~strikethrough~~ text, and I want to write a plugin for that. How would I go about doing this? I found https://github.com/objecthub/swift-markdownkit/blob/master/Sources/MarkdownKit/Parser/EmphasisTransformer.swift but this seems quite complex for something that feels should be rather simple ๐Ÿค” Is there no helper function available that takes a "delimiter" like ** or ~~ and transforms the text in between them?

Are you replacing newlines within paragraphs with spaces?

I was hoping to add my own support for hard breaks, something like so:

import MarkdownKit

let string = """
This is a line
This is another line
"""

func nl2br(doc: Block) -> Block {
  guard case .document(let topLevelBlocks) = doc else {
    preconditionFailure("markdown block does not represent a document")
  }

  var blocks = [Block]()

  for block in topLevelBlocks {
    switch block {
      case .paragraph(let text):
        let nl2brText = text.rawDescription.replacingOccurrences(of: "\n", with: "<br>")
        blocks.append(.paragraph(.init(Substring(nl2brText))))

      default:
        blocks.append(block)
    }
  }

  let newDoc = Block.document(.init(blocks))
  return newDoc
}

let document = MarkdownParser.standard.parse(string)
let modifiedDocument = nl2br(doc: document)
let html = HtmlGenerator.standard.generate(doc: modifiedDocument)
print(html)

But the newline within the Markdown text is already gone, seemingly replaced with a space. Is this intentional?

Inline highlighting without a split pane?

Is it possible to create only a single window where a user types in markdown and have it dynamically rendered? Sort of like Obsidian / Bear app / Spaces does it?

Eg writing **bold** to only show bold? (Notice that the asterisks have been "hidden") - all in one window, without having a "preview" and an "editor" mode so to say

Html inside of code blocks gets rendered as html

Problem

Using the HTMLGenerator, if there is html inside of code blocks, it will be rendered as html instead of as text. This causes undefined behaviour.

Example: here, a css stylesheet is being rendered, messing up the webpage. (see highlighted line)
Schermafbeelding 2022-02-04 om 02 12 28

Proposed solution

Replace all < with &lt; inside of code blocks and > with &gt;.

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.