front_matter 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

Front Matter #

A front matter parser that extracts YAML metadata from the start of a file or string.

Build Status

Front matter is a concept heavily borrowed from Jekyll, and other static site generators, referring to a block of YAML in the header of a file representing the file's metadata.

Usage #

Suppose you have the following Markdown file:

---
title: "Hello, world!"
author: "izolate"
---

This is an example.

Use parse to parse a string, or parseFile to read a file and parse its contents.

import 'dart:io';
import 'package:front_matter/front_matter.dart' as fm;

// Example 1 - parse a string.
void example1() async {
  var file = File('/path/to/file.md');
  var fileContents = await file.readAsString();
  var doc = fm.parse(fileContents);
  
  print(doc.data['title']); // "Hello, world!"
  print(doc.content);       // "This is an example."
}

// Example 2 - read file and parse contents.
void example2() async {
  var doc = await fm.parseFile('path/to/file.md');

  print(doc.data['title']); // "Hello, world!"
  print(doc.content);       // "This is an example."
}

The returned document is an instance of FrontMatterDocument with properties:

  • YamlMap data - The front matter.
  • String content - The content body.

To convert the document back to the initial string value, call toString().

var text = '---\nfoo: bar\n---\nHello, world!';
var doc = fm.parse(text);

assert(doc.toString(), equals(text)); // true

API #

parse(String text, {String delimiter = "---"}) #

Parses a string, returns a FrontMatterDocument with front matter and body content.

parseFile(String path, {String delimiter = "---"}) #

Reads a file and parses its contents. Returns a Future<FrontMatterDocument> with front matter and body content.

Changelog #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[1.1.0] - 2019-06-16

Added #

  • toString() method to FrontMatterDocument.

[1.0.2] - 2019-06-09

Changed #

  • Exported FrontMatterDocument and FrontMatterException.

[1.0.1] - 2019-06-09

Added #

  • Changelog.

Changed #

  • Package description.
  • Combined examples into one file.

[1.0.0] - 2019-06-09

Added #

  • Initial commit of project files.

example/front_matter.dart

import 'dart:io';
import 'package:front_matter/front_matter.dart' as fm;

// Example 1 - Parse a string.
void example1() async {
  var file = File('example/hello-world.md');
  var fileContents = await file.readAsString();

  var doc = fm.parse(fileContents);

  print("The author is ${doc.data['author']}");
  print("The publish date is ${doc.data['date']}");
  print("The content is ${doc.content}");
}

// Example 2 - Read a file and parse its contents.
void example2() async {
  var doc = await fm.parseFile('example/hello-world.md');

  print("The author is ${doc.data['author']}");
  print("The publish date is ${doc.data['date']}");
  print("The content is ${doc.content}");
}

void main() async {
  await example1();
  await example2();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  front_matter: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:front_matter/front_matter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
37
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
68
Learn more about scoring.

We analyzed this package on Oct 23, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:front_matter/front_matter.dart with components: io.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
yaml ^2.1.15 2.2.0
Transitive dependencies
charcode 1.1.2
collection 1.14.12
meta 1.1.7
path 1.6.4
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
Dev dependencies
test ^1.6.2