📝 Swift port of the popular remove-markdown package Maintains 100% feature parity with the original Node.js implementation
A Swift package that removes (strips) Markdown formatting from text.
RemoveMarkdown is a Swift package that will remove Markdown formatting from text. Markdown formatting means pretty much anything that doesn't look like regular text, like square brackets, asterisks, etc.
The typical use case is to display an excerpt from some Markdown text, without any of the actual Markdown syntax - for example in a list of posts.
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/yvente/swift-remove-markdown.git", from: "1.0.0")
]Or in Xcode:
- File > Add Package Dependencies...
- Enter the repository URL
- Select your version requirements
import RemoveMarkdown
let markdown = """
# This is a heading
This is a paragraph with [a link](http://www.disney.com/) in it.
"""
let plainText = removeMarkdown(markdown)
// Result: "This is a heading\n\nThis is a paragraph with a link in it."You can customize the behavior by passing an RemoveMarkdownOptions struct:
import RemoveMarkdown
let options = RemoveMarkdownOptions(
stripListLeaders: true, // strip list leaders (default: true)
listUnicodeChar: nil, // char to insert instead of stripped list leaders (default: nil)
gfm: true, // support GitHub-Flavored Markdown (default: true)
useImgAltText: true, // replace images with alt-text, if present (default: true)
abbr: false, // remove abbreviations (default: false)
replaceLinksWithURL: false, // replace links with URL instead of text (default: false)
htmlTagsToSkip: [], // HTML tags to preserve (default: [])
throwError: false // throw errors instead of returning original text (default: false)
)
let plainText = removeMarkdown(markdown, options: options)stripListLeaders: Whentrue, removes list markers like*,-,+, and numbered lists like1.listUnicodeChar: If provided, replaces list leaders with this character instead of removing them entirelygfm: Enables GitHub-Flavored Markdown support (strikethrough, fenced code blocks, etc.)useImgAltText: Whentrue, replaces image syntax with the alt text; whenfalse, removes images entirelyabbr: Removes abbreviation definitions whentruereplaceLinksWithURL: Whentrue, replaces[text](url)withurlinstead oftexthtmlTagsToSkip: Array of HTML tag names to preserve in the outputthrowError: Whentrue, throws errors; whenfalse, prints error and returns original text
let text = "I italicized an *I* and it _made_ me *sad*."
let result = removeMarkdown(text)
// Result: "I italicized an I and it made me sad."let text = "## This is a heading"
let result = removeMarkdown(text)
// Result: "This is a heading"let text = """
* Item 1
* Item 2
* Item 3
"""
let result = removeMarkdown(text)
// Result: "Item 1\nItem 2\nItem 3"let text = "<div>Content <sub>subscript</sub> <span>text</span></div>"
let options = RemoveMarkdownOptions(htmlTagsToSkip: ["sub"])
let result = removeMarkdown(text, options: options)
// Result: "Content <sub>subscript</sub> text"- macOS 10.15+
- iOS 13.0+
- tvOS 13.0+
- watchOS 6.0+
Run tests using Swift Package Manager:
cd swift-remove-markdown
swift testOr in Xcode:
- Open
Package.swift - Press
⌘Uto run tests
This is a Swift port of the remove-markdown package:
- Original JavaScript implementation: Stian Grytøyr (creator)
- Current maintainer: zuchka
- Original inspiration: Markdown Service Tools by Brett Terpstra
All regex patterns and test cases are ported from the original package to ensure behavioral consistency.
MIT License - see LICENSE file for details.
This Swift port maintains the same MIT License as the original remove-markdown package.
Contributions are welcome! Please feel free to submit a Pull Request.
To work on this package:
- Clone the repository
- Open
Package.swiftin Xcode - Make your changes
- Run tests to ensure everything works
- Submit a pull request
- Allow customization of regex patterns per rule
- Support for more edge cases
- Additional comprehensive tests
- Performance optimizations