mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-27 17:29:50 +08:00
Port LinksLoader to null safety
With this I'm not accepting any of the Link's field to be an empty string. In that case it's better for it to be null, as the compiler will force me to deal with an empty value.
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import 'package:markdown/markdown.dart' as md;
|
||||
|
||||
// FIXME: This should be split into 2 classes, that way it would be easier
|
||||
// to access to members with null safety
|
||||
class Link {
|
||||
String? publicTerm;
|
||||
String? filePath;
|
||||
@ -13,7 +15,17 @@ class Link {
|
||||
required this.filePath,
|
||||
this.headingID,
|
||||
this.alt,
|
||||
});
|
||||
}) {
|
||||
if (publicTerm?.isEmpty == true) {
|
||||
publicTerm = null;
|
||||
}
|
||||
if (headingID?.isEmpty == true) {
|
||||
headingID = null;
|
||||
}
|
||||
if (alt?.isEmpty == true) {
|
||||
alt = null;
|
||||
}
|
||||
}
|
||||
Link.wiki(this.wikiTerm);
|
||||
|
||||
bool get isWikiLink => wikiTerm != null;
|
||||
@ -68,7 +80,7 @@ class LinkExtractor implements md.NodeVisitor {
|
||||
return;
|
||||
}
|
||||
|
||||
var alt = el.attributes['title'] ?? "";
|
||||
var alt = el.attributes['title'];
|
||||
var title = _getText(el.children);
|
||||
|
||||
var url = el.attributes['href'];
|
||||
|
@ -1,5 +1,3 @@
|
||||
// @dart=2.9
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
|
||||
@ -12,9 +10,9 @@ import 'package:synchronized/synchronized.dart';
|
||||
import 'package:gitjournal/core/link.dart';
|
||||
|
||||
class LinksLoader {
|
||||
Isolate _isolate;
|
||||
Isolate? _isolate;
|
||||
ReceivePort _receivePort = ReceivePort();
|
||||
SendPort _sendPort;
|
||||
SendPort? _sendPort;
|
||||
|
||||
var _loadingLock = Lock();
|
||||
|
||||
@ -24,7 +22,7 @@ class LinksLoader {
|
||||
return await _loadingLock.synchronized(() async {
|
||||
if (_isolate != null && _sendPort != null) return;
|
||||
if (_isolate != null) {
|
||||
_isolate.kill(priority: Isolate.immediate);
|
||||
_isolate!.kill(priority: Isolate.immediate);
|
||||
_isolate = null;
|
||||
}
|
||||
_isolate = await Isolate.spawn(_isolateMain, _receivePort.sendPort);
|
||||
@ -35,11 +33,12 @@ class LinksLoader {
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<Link>> parseLinks({String body, String filePath}) async {
|
||||
Future<List<Link>> parseLinks(
|
||||
{required String body, required String filePath}) async {
|
||||
await _initIsolate();
|
||||
|
||||
var rec = ReceivePort();
|
||||
_sendPort.send(_LoadingMessage(body, filePath, rec.sendPort));
|
||||
_sendPort!.send(_LoadingMessage(body, filePath, rec.sendPort));
|
||||
|
||||
var data = await rec.first;
|
||||
assert(data is List<Link>);
|
||||
@ -90,7 +89,7 @@ List<Link> parseLinks(String body, String filePath) {
|
||||
continue;
|
||||
}
|
||||
|
||||
l.filePath = p.join(parentFolderPath, p.normalize(l.filePath));
|
||||
l.filePath = p.join(parentFolderPath, p.normalize(l.filePath!));
|
||||
links.add(l);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
// @dart=2.9
|
||||
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:gitjournal/core/links_loader.dart';
|
||||
@ -26,27 +24,27 @@ void main() {
|
||||
filePath: "/tmp/foo/file.md",
|
||||
);
|
||||
|
||||
expect(links[0].filePath.isEmpty, true);
|
||||
expect(links[0].headingID.isEmpty, true);
|
||||
expect(links[0].alt.isEmpty, true);
|
||||
expect(links[0].publicTerm.isEmpty, true);
|
||||
expect(links[0].filePath, isNull);
|
||||
expect(links[0].headingID, isNull);
|
||||
expect(links[0].alt, isNull);
|
||||
expect(links[0].publicTerm, isNull);
|
||||
expect(links[0].wikiTerm, "GitJournal");
|
||||
expect(links[0].isWikiLink, true);
|
||||
|
||||
expect(links[1].filePath, "/tmp/foo/gitjournal.md");
|
||||
expect(links[1].publicTerm, "GitJournal");
|
||||
expect(links[1].alt.isEmpty, true);
|
||||
expect(links[1].wikiTerm.isEmpty, true);
|
||||
expect(links[1].alt, isNull);
|
||||
expect(links[1].wikiTerm, isNull);
|
||||
|
||||
expect(links[2].filePath, "/tmp/foo/gitjournal.md");
|
||||
expect(links[2].publicTerm, "GitJournal");
|
||||
expect(links[2].alt.isEmpty, true);
|
||||
expect(links[2].wikiTerm.isEmpty, true);
|
||||
expect(links[2].alt, isNull);
|
||||
expect(links[2].wikiTerm, isNull);
|
||||
|
||||
expect(links[3].filePath, "/tmp/foo/gitjournal");
|
||||
expect(links[3].publicTerm, "GitJournal");
|
||||
expect(links[3].alt, "alt-text");
|
||||
expect(links[3].wikiTerm.isEmpty, true);
|
||||
expect(links[3].wikiTerm, isNull);
|
||||
|
||||
/*
|
||||
expect(links[4].filePath, "/tmp/foam.md");
|
||||
@ -78,13 +76,13 @@ Foam is licensed under the [MIT license](license).
|
||||
expect(links.length, 5);
|
||||
|
||||
expect(links[0].filePath, "/tmp/foo.md");
|
||||
expect(links[0].alt.isEmpty, true);
|
||||
expect(links[0].alt, isNull);
|
||||
expect(links[0].headingID, "#contributors-");
|
||||
expect(links[0].publicTerm.isEmpty, true);
|
||||
expect(links[0].publicTerm, isNull);
|
||||
|
||||
expect(links[1].filePath, "/tmp/license");
|
||||
expect(links[1].alt.isEmpty, true);
|
||||
expect(links[1].headingID.isEmpty, true);
|
||||
expect(links[1].alt, isNull);
|
||||
expect(links[1].headingID, isNull);
|
||||
expect(links[1].publicTerm, "MIT license");
|
||||
|
||||
expect(links[2].filePath, "/tmp/foo.md");
|
||||
@ -96,7 +94,7 @@ Foam is licensed under the [MIT license](license).
|
||||
// FIXME: link-references for wiki Links
|
||||
// expect(links[3].filePath.isEmpty, true);
|
||||
// expect(links[3].isWikiLink, true);
|
||||
expect(links[3].headingID.isEmpty, true);
|
||||
expect(links[3].headingID, isNull);
|
||||
expect(links[3].alt, "Wiki Links");
|
||||
|
||||
expect(links[4].filePath, "/tmp/foo.md");
|
||||
|
Reference in New Issue
Block a user