parse method

Future<Content> parse(
  1. FileSystemEntity entity
)

Recursively parse file tree starting from entity.

Implementation

Future<Content> parse(FileSystemEntity entity) async {
  return entity.when(
    file: (file) async {
      final content = await file.readAsString();
      final parsed = await _parseFile(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 (await GitUtil.isGitInstalled()) {
          final date = await GitUtil.getModified(file);
          if (date != null) {
            metadata['date'] = date.toIso8601String();
          }
        }
      }

      return Page(
        path: path,
        content: parsed.content,
        metadata: metadata,
      );
    },
    directory: (directory) async {
      final children = await directory.list().toList();

      final content = (await children.asyncMap(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.');
    },
  )!;
}