showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.27k stars 1.57k forks source link

Metadata (front-matter) not fully support YAML #640

Open lqmanh opened 5 years ago

lqmanh commented 5 years ago

I have some markdown files with the front-matter lilke this:

---
title: Sample Title
description: This is the description
published: true
publicationDate: 2019-01-18
tags: [tag1, tag2]
---

Currently Showdown Converter.getMetadata() doesn't parse Boolean, Date or Array values correctly but literal strings. Could you please make it fully support YAML front-matter format?

tivie commented 5 years ago

Metadata is similar to YAML but it's not YAML. It's supposed to be a way for defining HTML metadata.

lqmanh commented 5 years ago

I think it would be great if it fully support YAML though. That metadata is just a subset of YAML, right?

jcc10 commented 5 years ago

If you do Converter.getMetadata(true) it should return the raw metadata block, you could then pass that to a YAML parser.

jcc10 commented 5 years ago

This commit should work (I'm trying to do the same thing myself.): https://gitlab.com/random-reviews/random-reviews.gitlab.io/tree/bb1c92d1e40d765112e69ccd55917b4f4a209e84

lqmanh commented 5 years ago

@jcc10 Currently I use gray-matter. It's totally enough for my needs but I'm saying it would be great if this is implemented natively in showdown. Then I don't have to use a different tool to parse front matter just because the built-in one is worse.

tivie commented 5 years ago

While I understand the appeal of supporting full YAML, I don't think the pros outweigh the cons.

The metadata feature is meant for defining HTML metadata which is rather simple and does not support most YAML features (like arrays, lists, etc...). It's not

Also, it would slow down the parser just for those extra features, which most users don't really care about.

Then I don't have to use a different tool to parse front matter just because the built-in one is worse.

In order for fully support YAML I would need to add an extra dependency (a YAML parser) to showdown so you would be using a different tool anyways, you just wouldn't notice it.

As @jcc10 pointed out, using getMetadata(true) is more flexible. You get the raw stuff and can parse it however you want.