rianfuro/commonmark-metadata

Parse MultiMarkdown-like metadata at the beginning of a markdown document

Installs: 20

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 0

Type:commonmark-extension

v0.1.2 2021-10-02 19:24 UTC

This package is auto-updated.

Last update: 2025-01-07 11:54:00 UTC


README

This is an extension for thephpleague/commonmark to parse MultiMarkdown-like metadata from the top of a Markdown file.

Installation

  1. Require the package like any other:
composer require rianfuro/commonmark-metadata
  1. Install the extension.
use CommonMark\Extension\Metadata\MetadataExtension;

$environment = new Environment([]);
// ... other extensions
$environment->addExtension(new MetadataExtension);

new MarkdownConverter($environment);

Usage

The extension is used automatically by the CommonMark Parser when included. The extension converts the metadata section into a MetadataBlock, which holds the fields in it's data property. You can, for example, use CommonMark's query utility to fetch the Block and get it's data:

$metadata = (new Query())
  ->where(Query::type(MetadataBlock::class))
  ->findOne($document);
  
$metadata->data->export(); // note that this always includes the default `attributes` field

Current limitations

The project is in it's super early stages so the parsing is very limited. Currently the metadata needs to be fenced with yaml-style delimiters (should be optional according to the spec):

---
Author: Me
Title: Super Awesome Document
---

My Amazing Markdown Document
============================

...

Additionally, the metadata fields cannot be referenced inside the document.