dartboard_resume/lib/models/document_text.dart

46 lines
1.6 KiB
Dart
Raw Normal View History

2024-09-10 15:09:15 -06:00
enum DocumentTextType { normal, linkText }
typedef DocumentTextLinkData = ({String text, String? url, DocumentTextType type});
/// Automatically detects and parses strings with hypertext in a markdown format
class DocumentText {
DocumentText({required this.content});
final String content;
static final _markdownLinkRegex = RegExp(r'\[(.*?)\]\((.*?)\)');
bool get hasLinkMarkup => _markdownLinkRegex.hasMatch(content);
List<DocumentTextLinkData> toTextLinkList() {
final markdownLinkRegex = RegExp(r'\[(.*?)\]\((.*?)\)');
if (markdownLinkRegex.hasMatch(content)) {
final matches = markdownLinkRegex.allMatches(content).toList();
final List<DocumentTextLinkData> stringSections = [];
int prevStartIndex = 0;
while (matches.isNotEmpty) {
final match = matches.removeAt(0);
stringSections
.add((text: content.substring(prevStartIndex, match.start), url: null, type: DocumentTextType.normal));
stringSections.add((text: match.group(1)!, url: match.group(2), type: DocumentTextType.linkText));
prevStartIndex = match.end;
}
stringSections
.add((text: content.substring(prevStartIndex, content.length), url: null, type: DocumentTextType.normal));
return stringSections;
} else {
return [(text: content, url: null, type: DocumentTextType.normal)];
// return Text("${bulletString != null ? '$bulletString ' : ''}$content", style: style);
}
}
@override
int get hashCode {
return Object.hashAll([content]);
}
@override
bool operator ==(Object other) {
return super.hashCode == other.hashCode;
}
}