UrlSpliter
A Dart/Flutter library for splitting text and identifying URLs within it. This library processes text input and returns structured data that can be used for text rendering or further processing.
Features
- ✅ Null Safety: Fully supports Dart's null safety
- ✅ Flutter Compatible: Works with Flutter 3.0+
- ✅ URL Detection: Identifies HTTP and HTTPS URLs in text
- ✅ Structured Output: Returns categorized text spans
- ✅ Modern Dart: Uses latest Dart language features
Installation
Add this to your package's pubspec.yaml file:
dependencies:
urlspliter: ^0.2.0
Usage
Basic Usage
import 'package:urlspliter/urlspliter.dart';
void main() {
final spliter = UrlSpliter('Check out this website: http://example.com');
final result = spliter.process();
for (final span in result) {
if (span.contentType == TextSpanData.txt) {
print('Text: ${span.content}');
} else {
print('URL: ${span.content}');
}
}
}
Advanced Usage
import 'package:urlspliter/urlspliter.dart';
void main() {
// Process text with multiple URLs
final text = 'First: http://example1.com and second: https://example2.com';
final spliter = UrlSpliter(text);
final spans = spliter.process();
// Use the spans for rendering or processing
for (final span in spans) {
switch (span.contentType) {
case TextSpanData.txt:
// Handle plain text
print('Plain text: ${span.content}');
break;
case TextSpanData.url:
// Handle URL
print('Found URL: ${span.content}');
break;
}
}
}
API Reference
UrlSpliter
The main class for processing text and identifying URLs.
Constructor
UrlSpliter(String text)
Creates a new UrlSpliter instance with the given text.
Methods
List<TextSpanData> process()
Processes the text and returns a list of TextSpanData objects representing text spans and URLs found in the input.
TextSpanData
Represents a span of text with its content type.
Properties
content(String): The content of this text spancontentType(int): The type of content (txt or url)
Constants
TextSpanData.txt(0): Text content typeTextSpanData.url(1): URL content type
Examples
Plain Text
final spliter = UrlSpliter('This is plain text without any URLs');
final result = spliter.process();
// Returns: [TextSpanData('This is plain text without any URLs', TextSpanData.txt)]
Text with HTTP URL
final spliter = UrlSpliter('Check this link: http://example.com');
final result = spliter.process();
// Returns: [
// TextSpanData('Check this link: ', TextSpanData.txt),
// TextSpanData('http://example.com', TextSpanData.url)
// ]
Text with HTTPS URL
final spliter = UrlSpliter('Secure link: https://secure.example.com/path?param=value');
final result = spliter.process();
// Returns: [
// TextSpanData('Secure link: ', TextSpanData.txt),
// TextSpanData('https://secure.example.com/path?param=value', TextSpanData.url)
// ]
Multiple URLs
final spliter = UrlSpliter('First: http://example1.com and second: https://example2.com');
final result = spliter.process();
// Returns: [
// TextSpanData('First: ', TextSpanData.txt),
// TextSpanData('http://example1.com', TextSpanData.url),
// TextSpanData(' and second: ', TextSpanData.txt),
// TextSpanData('https://example2.com', TextSpanData.url)
// ]
Requirements
- Dart SDK: >=3.0.0 <4.0.0
- Flutter: >=3.0.0
License
This project is licensed under the same license as the original project.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Features and bugs
Please file feature requests and bugs at the issue tracker.
Libraries
- urlspliter
- A URL text splitter library that separates URLs from plain text.