markdown
markdown copied to clipboard
Support for access to front matter in markdown
For example, we have this markdown file:
---
title: Overview
slug: /
id: introduction
---

It would be cool if I can access to the properties title, slug and id
Do you know if this is specified in a spec somewhere? AFAIK, the front matter like you show is not standard...
I saw this methods in jekyll and docusaurus
For further reference,
- Frontmatter was discussed in Common Mark but never added to the spec
- Several tools, specially CMS, understand and support it including Hugo, Gatsby, Jekyll, Nikola, VuePress.
- VSCode also understands front-matter
Note that, not being standarised, the syntax differs from program to program but mostly it can be briefed as:
- If the first line of the file is
---what follows may be a front-matter (note that on YAML it could be...but haven't seen any product use that in the front-matter) - The front-matter is ended with a yaml document delimiter (
---or...) - The front-matter must be a valid yaml document
I say may be because somebody might (?) start the document with a thematic break and what follows would NOT be a yaml document but the Markdown document itself.
If front-matter processing is opt-in then the assumption could be made that nobody would a) enable front-matter and b) start a document with a thematic break.
To make things more entertaining, some products (like VuePress or Hugo) allow other formats for the front-matter like JSON, TOML and even Org-Mode.
It's also possible that this should live in a separate package maintained by the community.
It's not super hard to do, and it's not really a markdown thing.
If we wanted to do it in this package we'd have to introduce a type that represents yaml-frontmatter and markdown document.
It's probably better if someone creates a generic frontmatter package, like:
import 'package:frontmatter/frontmatter.yaml';
import 'package:markdown/markdown.yaml';
void main() {
final input = '''
title: Overview
slug: /
id: introduction
---

''';
final doc = FrontmatterDocument.fromString(input);
print(loadYaml(doc.frontmatter));
print(markdownToHtml(doc.content));
}
How would this logic benefit from living in package:markdown?