markdown icon indicating copy to clipboard operation
markdown copied to clipboard

Support for access to front matter in markdown

Open CodeDoctorDE opened this issue 4 years ago • 4 comments
trafficstars

For example, we have this markdown file:

---
title: Overview
slug: /
id: introduction
---

![Alt](/img/banner.svg)

It would be cool if I can access to the properties title, slug and id

CodeDoctorDE avatar Jun 05 '21 06:06 CodeDoctorDE

Do you know if this is specified in a spec somewhere? AFAIK, the front matter like you show is not standard...

srawlins avatar Jun 07 '21 14:06 srawlins

I saw this methods in jekyll and docusaurus

CodeDoctorDE avatar Jun 07 '21 14:06 CodeDoctorDE

For further reference,

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.

telenieko avatar Mar 15 '22 17:03 telenieko

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
---

![Alt](/img/banner.svg)
''';

  final doc = FrontmatterDocument.fromString(input);
  print(loadYaml(doc.frontmatter));
  print(markdownToHtml(doc.content));
}

How would this logic benefit from living in package:markdown?

jonasfj avatar Apr 25 '23 11:04 jonasfj