Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save/get metadata? #260

Closed
IonicaBizau opened this issue Jun 6, 2016 · 5 comments
Closed

How to save/get metadata? #260

IonicaBizau opened this issue Jun 6, 2016 · 5 comments
Assignees

Comments

@IonicaBizau
Copy link

I'm looking how to save/read metadata like below:

---
Title:   My awesome markdown file
Author:  Me
Scripts:
    - js/doStuff.js
    - js/doMoreStuff.js
...

##Header
Regular text and stuff goes here.
@timaschew
Copy link

This is not part of a markdown parser usually.
You need a parse step before you run your markdown parser.
Here is module to fetch these data and split them into properties and the markdown body

https://github.com/jxson/front-matter

@tivie tivie added the question label Jun 6, 2016
@tivie
Copy link
Member

tivie commented Jun 6, 2016

the bit at the top is YAML so you need a yaml parser

@tivie tivie closed this as completed Jun 6, 2016
@IonicaBizau
Copy link
Author

@timaschew @tivie Thanks! I built mdify which uses js-yaml to parse the YAML stuff and showdown to htmlify the markdown code. 😁

@tivie
Copy link
Member

tivie commented Dec 10, 2017

I've decided that a simple metadata parser would be useful in markdown documents.

The syntax is:

  1. --- or ««« at the beginning of the markdown document
  2. optionally followed by a alphanumeric designation of the format (anything you like as long as it does not have any white spaces or newlines and stuff)
  3. followed by a newline
  4. followed by key value pairs separated by a colon and a space : and a newline at the end
  5. Closed by --- or »»» and a newline

like this:

---
title: document title
some meta name: some meta content
---
# my markdown document

rest of the document

or

«««
title: document title
some meta name: some meta content
»»»
# my markdown document

rest of the document

or even this:

---FORMAT-IDENTIFIER
title: document title
some meta name:
 - a list
 - of values
---
# my markdown document

rest of the document

To enable metadata parsing, one must enable the option metadata.

var conv = new showdown.Converter({metadata: true});

Showdown will expose a method for retrieving the document's metadata after parsing:

var conv = new showdown.Converter({metadata: true});
var html = conv.makeHtml(someMd);
var metadata = conv.getMetadata(); // returns an object with the document metadata

Note that showdown won't support YAML (or any other data format) out of the box, but you can retrieve the raw metadata and parse it yourself.

var conv = new showdown.Converter({metadata: true});
var html = conv.makeHtml(someMd);
var metadata = conv.getMetadata(true); // returns a string with the raw metadata
var metadataFormat = conv.getMetadataFormat(); // returns the format of the metadata

// do stuff with the metadata var

@tivie
Copy link
Member

tivie commented Dec 10, 2017

This feature should be implemented now

Donate Click here to lend your support to: ShowdownJS website and testing platform and make a donation at pledgie.com

As you know, ShowdownJS is a free library and it will remain free forever. However, maintaining and improving the library costs time and money.
If you like our work and find our library useful, please donate through Pledgie or directly through paypal!! Your contribution will be greatly appreciated and help us continue to develop this awesome library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants