yvente/swift-remove-markdown
> ๐ **Swift port of the popular [remove-markdown](https://github.com/zuchka/remove-markdown) package**
What is it?
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.
When do I need it?
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.
Installation
Swift Package Manager
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
Usage
Basic Usage
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."With Options
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)Options Explained
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, replacestextwithurlinstead oftexthtmlTagsToSkip: Array of HTML tag names to preserve in the outputthrowError: Whentrue, throws errors; whenfalse, prints error and returns original text
Examples
Stripping Emphasis
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."Removing Headers
let text = "## This is a heading"
let result = removeMarkdown(text)
// Result: "This is a heading"Handling Lists
let text = """
* Item 1
* Item 2
* Item 3
"""
let result = removeMarkdown(text)
// Result: "Item 1\nItem 2\nItem 3"Preserving Specific HTML Tags
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"Platform Support
- macOS 10.15+
- iOS 13.0+
- tvOS 13.0+
- watchOS 6.0+
Testing
Run tests using Swift Package Manager:
cd swift-remove-markdown
swift testOr in Xcode:
- Open
Package.swift - Press
โUto run tests
Credits
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.
License
MIT License - see LICENSE file for details.
This Swift port maintains the same MIT License as the original remove-markdown package.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development
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
Future Enhancements
- Allow customization of regex patterns per rule
- Support for more edge cases
- Additional comprehensive tests
- Performance optimizations
Package Metadata
Repository: yvente/swift-remove-markdown
Default branch: main
README: README.md