parse method
Recursively parse file tree starting from entity
.
Implementation
Content parse(FileSystemEntity entity) {
return entity.when(
file: (file) {
final extension = file.path.substring(file.path.lastIndexOf('.') + 1);
final contentType = (extension == 'html')
? ContentFileType.html : ContentFileType.markdown;
final content = file.readAsStringSync();
final parsed = _parseFile(contentType, content);
// Remove leading 'content/' part of the directory.
final path = Path.normalize(file.path).replaceFirst(
'${config.build.contentDir}/',
'',
);
final metadata = Map<String, dynamic>.from(parsed.metadata);
if (!metadata.containsKey('date')) {
if (GitUtil.isGitInstalled()) {
final date = GitUtil.getModified(file);
if (date != null) {
metadata['date'] = date.toIso8601String();
}
}
}
return Page(
path: path,
contentType: parsed.type,
content: parsed.content,
metadata: metadata,
);
},
directory: (directory) {
final children = directory.listSync().toList();
final content = (children.map(parse)).toList();
final index = content.where((e) => e is Page && e.isIndex);
if (index.length > 1) {
throw BuildError(
'Only one index file can be provided: '
'${index.map((e) => e.path).toList()}',
"Use either 'index.md' or '_index.md' file, not both.",
);
}
// Remove leading 'content/' part of the directory.
final path = Path.normalize(directory.path).replaceFirst(
'${config.build.contentDir}/',
'',
);
final indexes = content
.where((element) => element is Page && element.isIndex)
.whereType<Page>();
return Section(
path: path,
index: indexes.isEmpty ? null : indexes.first,
children: content
.where((element) => !(element is Page && element.isIndex))
.toList(),
);
},
link: (link) {
throw UnimplementedError('Link file is not yet supported.');
},
)!;
}