dart-lang / markdown

A Dart markdown library
https://pub.dev/packages/markdown
BSD 3-Clause "New" or "Revised" License
444 stars 201 forks source link

Support for access to front matter in markdown #371

Open CodeDoctorDE opened 3 years ago

CodeDoctorDE commented 3 years ago

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

srawlins commented 3 years ago

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

CodeDoctorDE commented 3 years ago

I saw this methods in jekyll and docusaurus

telenieko commented 2 years ago

For further reference,

Note that, not being standarised, the syntax differs from program to program but mostly it can be briefed as:

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.

jonasfj commented 1 year ago

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?