mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 04:07:53 +08:00
Note: Add an API to get the images from a note
This will be used in the future to display the images in the grid/card view.
This commit is contained in:
@ -7,6 +7,7 @@ import 'package:gitjournal/core/links_loader.dart';
|
|||||||
import 'package:gitjournal/core/md_yaml_doc_loader.dart';
|
import 'package:gitjournal/core/md_yaml_doc_loader.dart';
|
||||||
import 'package:gitjournal/core/note_notifier.dart';
|
import 'package:gitjournal/core/note_notifier.dart';
|
||||||
import 'package:gitjournal/core/notes_folder_fs.dart';
|
import 'package:gitjournal/core/notes_folder_fs.dart';
|
||||||
|
import 'package:gitjournal/core/processors/image_extractor.dart';
|
||||||
import 'package:gitjournal/core/processors/inline_tags.dart';
|
import 'package:gitjournal/core/processors/inline_tags.dart';
|
||||||
import 'package:gitjournal/error_reporting.dart';
|
import 'package:gitjournal/error_reporting.dart';
|
||||||
import 'package:gitjournal/settings.dart';
|
import 'package:gitjournal/settings.dart';
|
||||||
@ -64,6 +65,7 @@ class Note with NotesNotifier {
|
|||||||
String _summary;
|
String _summary;
|
||||||
List<Link> _links;
|
List<Link> _links;
|
||||||
Set<String> _inlineTags;
|
Set<String> _inlineTags;
|
||||||
|
Set<NoteImage> _images;
|
||||||
|
|
||||||
static final _mdYamlDocLoader = MdYamlDocLoader();
|
static final _mdYamlDocLoader = MdYamlDocLoader();
|
||||||
static final _linksLoader = LinksLoader();
|
static final _linksLoader = LinksLoader();
|
||||||
@ -153,6 +155,7 @@ class Note with NotesNotifier {
|
|||||||
_summary = null;
|
_summary = null;
|
||||||
_links = null;
|
_links = null;
|
||||||
_inlineTags = null;
|
_inlineTags = null;
|
||||||
|
_images = null;
|
||||||
|
|
||||||
_notifyModified();
|
_notifyModified();
|
||||||
}
|
}
|
||||||
@ -200,6 +203,15 @@ class Note with NotesNotifier {
|
|||||||
return _inlineTags;
|
return _inlineTags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Set<NoteImage> get images {
|
||||||
|
if (_loadState != NoteLoadState.Loaded) return {};
|
||||||
|
|
||||||
|
var p = ImageExtractor();
|
||||||
|
_images ??= p.extract(body);
|
||||||
|
|
||||||
|
return _images;
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, dynamic> get extraProps {
|
Map<String, dynamic> get extraProps {
|
||||||
return _extraProps;
|
return _extraProps;
|
||||||
}
|
}
|
||||||
|
32
lib/core/processors/image_extractor.dart
Normal file
32
lib/core/processors/image_extractor.dart
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
class NoteImage extends Equatable {
|
||||||
|
final String url;
|
||||||
|
final String alt;
|
||||||
|
|
||||||
|
NoteImage({@required this.url, @required this.alt});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [url, alt];
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get stringify => true;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ImageExtractor {
|
||||||
|
static final _regexp = RegExp(r"!\[(.*)\]\((.*)\)");
|
||||||
|
|
||||||
|
Set<NoteImage> extract(String body) {
|
||||||
|
var images = <NoteImage>{};
|
||||||
|
var matches = _regexp.allMatches(body);
|
||||||
|
for (var match in matches) {
|
||||||
|
var alt = match.group(1);
|
||||||
|
var url = match.group(2);
|
||||||
|
|
||||||
|
images.add(NoteImage(alt: alt, url: url));
|
||||||
|
}
|
||||||
|
|
||||||
|
return images;
|
||||||
|
}
|
||||||
|
}
|
20
test/processors/image_extractor_test.dart
Normal file
20
test/processors/image_extractor_test.dart
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import 'package:gitjournal/core/processors/image_extractor.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('Should parse simple tags', () {
|
||||||
|
var body = """#hello Hi
|
||||||
|

|
||||||
|

|
||||||
|
""";
|
||||||
|
|
||||||
|
var p = ImageExtractor();
|
||||||
|
var images = p.extract(body);
|
||||||
|
|
||||||
|
expect(images, {
|
||||||
|
NoteImage(alt: 'alt', url: '../final.img'),
|
||||||
|
NoteImage(alt: 'alt2', url: '../final2.img'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Reference in New Issue
Block a user