All Projects → mattt → Commonmarkattributedstring

mattt / Commonmarkattributedstring

Licence: mit
Create NSAttributedStrings from Markdown Text

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Commonmarkattributedstring

Commonmark
Create, parse, and render Markdown text according to the CommonMark specification
Stars: ✭ 147 (-61.52%)
Mutual labels:  markdown, commonmark
Vditor
♏ 一款浏览器端的 Markdown 编辑器,支持所见即所得(富文本)、即时渲染(类似 Typora)和分屏预览模式。An In-browser Markdown editor, support WYSIWYG (Rich Text), Instant Rendering (Typora-like) and Split View modes.
Stars: ✭ 3,773 (+887.7%)
Mutual labels:  markdown, commonmark
Commonmark
Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.
Stars: ✭ 2,128 (+457.07%)
Mutual labels:  markdown, commonmark
Down
Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.
Stars: ✭ 1,895 (+396.07%)
Mutual labels:  markdown, commonmark
Markdownlint
A Node.js style checker and lint tool for Markdown/CommonMark files.
Stars: ✭ 2,828 (+640.31%)
Mutual labels:  markdown, commonmark
Vditor
♏ 一款浏览器端的 Markdown 编辑器。
Stars: ✭ 1,742 (+356.02%)
Mutual labels:  markdown, commonmark
Tui.editor
🍞📝 Markdown WYSIWYG Editor. GFM Standard + Chart & UML Extensible.
Stars: ✭ 14,016 (+3569.11%)
Mutual labels:  markdown, commonmark
Commonmark Java
Java library for parsing and rendering CommonMark (Markdown)
Stars: ✭ 1,675 (+338.48%)
Mutual labels:  markdown, commonmark
Markdig
A fast, powerful, CommonMark compliant, extensible Markdown processor for .NET
Stars: ✭ 2,730 (+614.66%)
Mutual labels:  markdown, commonmark
Lute
🎼 一款结构化的 Markdown 引擎,支持 Go 和 JavaScript。
Stars: ✭ 211 (-44.76%)
Mutual labels:  markdown, commonmark
Texme
Self-rendering Markdown + LaTeX documents
Stars: ✭ 1,970 (+415.71%)
Mutual labels:  markdown, commonmark
Md4c
C Markdown parser. Fast. SAX-like interface. Compliant to CommonMark specification.
Stars: ✭ 322 (-15.71%)
Mutual labels:  markdown, commonmark
Mdme
Self-rendering Markdown content
Stars: ✭ 140 (-63.35%)
Mutual labels:  markdown, commonmark
Markdown It
Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
Stars: ✭ 12,638 (+3208.38%)
Mutual labels:  markdown, commonmark
Editor.md
The open source embeddable online markdown editor (component).
Stars: ✭ 11,741 (+2973.56%)
Mutual labels:  markdown, commonmark
Markdown
CommonMark-compliant markdown parser and renderer in Go. This repository has moved to https://gitlab.com/golang-commonmark/markdown
Stars: ✭ 180 (-52.88%)
Mutual labels:  markdown, commonmark
Goldmark
🏆 A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.
Stars: ✭ 1,813 (+374.61%)
Mutual labels:  markdown, commonmark
Flexmark Java
CommonMark/Markdown Java parser with source level AST. CommonMark 0.28, emulation of: pegdown, kramdown, markdown.pl, MultiMarkdown. With HTML to MD, MD to PDF, MD to DOCX conversion modules.
Stars: ✭ 1,673 (+337.96%)
Mutual labels:  markdown, commonmark
Markdownui
Render Markdown text in SwiftUI
Stars: ✭ 207 (-45.81%)
Mutual labels:  markdown, commonmark
Lute
🎼 一款对中文语境优化的 Markdown 引擎,支持 Go 和 JavaScript。A structured Markdown engine that supports Go and JavaScript.
Stars: ✭ 222 (-41.88%)
Mutual labels:  markdown, commonmark

CommonMarkAttributedString

CI Documentation

CommonMarkAttributedString is a Swift package that lets you create attributed strings using familiar CommonMark (Markdown) syntax. It's built on top of CommonMark, which is fully compliant with the CommonMark Spec.

Supported Platforms

  • macOS 10.10+
  • Mac Catalyst 13.0+
  • iOS 9.0+
  • tvOS 9.0+

Usage

import CommonMarkAttributedString

let commonmark = "A *bold* way to add __emphasis__ to your `code`"

let attributes: [NSAttributedString.Key: Any] = [
    .font: NSFont.systemFont(ofSize: 24.0),
    .foregroundColor: NSColor.systemBlue,
]

let attributedString = try NSAttributedString(commonmark: commonmark, attributes: attributes)

Result

You can also use CommonMarkAttributedString to create attributed strings that have multiple paragraphs, with links, headings, lists, and images.

let commonmark = #"""
# [Universal Declaration of Human Rights][uhdr]

## Article 1.

All human beings are born free and equal in dignity and rights.
They are endowed with reason and conscience
and should act towards one another in a spirit of brotherhood.

[uhdr]: https://www.un.org/en/universal-declaration-human-rights/ "View full version"
"""#

let attributes: [NSAttributedString.Key: Any] = [
    .font: NSFont.systemFont(ofSize: NSFont.systemFontSize),
    .foregroundColor: NSColor.textColor,
    .backgroundColor: NSColor.textBackgroundColor,
]

let attributedString = try NSAttributedString(commonmark: commonmark, attributes: attributes)

Result

Supported CommonMark Elements

  • [x] Code
  • [x] Emphasis
  • [x] Link (inline links, link references, and autolinks)
  • [x] Strong
  • [x] > Block Quotes
  • [x] Headings
  • [x] Raw <html> *
  • [x] • Bulleted Lists *
  • [x] 1. Ordered Lists *
  • [x] 🖼 Images *

Raw Inline HTML

According to the CommonMark specification, each inline HTML tag is considered its own element. That is to say, CommonMark doesn't have a concept of opening or closing tags. So, for example, the CommonMark string <span style="color: red;">hello</span> corresponds to a paragraph block containing three inline elements:

  • Code (<span style="color: red;">)
  • Text (hello)
  • Code (</span>)

Parsing and rendering HTML is out of scope for this library, so whenever CommonMarkAttributedString receives text containing any HTML, it falls back on NSAttributedString's built-in HTML initializer.

Bulleted and Ordered Lists

CommonMarkAttributedString renders bulleted and ordered lists using conventional indentation and markers --- disc (•), circle(◦), and square (■) for unordered lists and decimal numerals (1.), lowercase roman numerals (i.), and lowercase letters (a.) for ordered lists.

  • Level 1
    • Level 2
      • Level 3

  1. Level 1
    1. Level 2
      1. Level 3

Images

Attributed strings can embed images using the NSTextAttachment class. However, there's no built-in way to load images asynchronously. Rather than load images synchronously as they're encountered in CommonMark text, CommonMarkAttributedString provides an optional attachments parameter that you can use to associate existing text attachments with image URL strings.

let commonmark = "![](https://example.com/image.png)"

let attachments: [String: NSTextAttachment] = [
    "https://example.com/image.png": NSTextAttachment(data: <#...#>, ofType: "public.png")
]

let attributedString = try NSAttributedString(commonmark: commonmark, attributes: attributes, attachments: attachments)

Requirements

  • Swift 5.1+

Installation

Swift Package Manager

Add the CommonMarkAttributedString package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/mattt/CommonMarkAttributedString",
        from: "0.2.0"
    ),
  ]
)

Then run the swift build command to build your project.

License

MIT

Contact

Mattt (@mattt)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].