Skip to content

Feature request: support YAML front matter #65

@lvh

Description

@lvh

Summary

YAML front matter (delimited by ---) is widely used in static site generators (Hugo, Jekyll, etc.) and documentation tools. Currently, nextjournal/markdown parses front matter as regular markdown content, producing incorrect results.

Current behavior

(require '[nextjournal.markdown :as md])

(md/parse "---
title: My Post
date: 2024-01-01
---

## Actual Heading")

Produces:

  • A :ruler node (from the opening ---)
  • An h2 heading with text "title: My Post" (incorrectly parsed as markdown heading)
  • A paragraph with "date: 2024-01-01"
  • Another :ruler node (from the closing ---)
  • The actual h2 heading

Expected behavior

Front matter should be:

  1. Recognized and excluded from markdown parsing, OR
  2. Parsed and included as a separate :front-matter node with the YAML content

Example of option 2:

{:type :doc
 :front-matter {:title "My Post" :date "2024-01-01"}
 :content [{:type :heading :heading-level 2 :content [...]}]}

Workaround

Users currently need to strip front matter before calling parse, which is error-prone and loses the metadata.

Implementation note

The underlying commonmark-java library already has a YamlFrontMatterExtension in the commonmark-ext-yaml-front-matter artifact with YamlFrontMatterVisitor for extracting metadata. Using this extension at the commonmark-java level (rather than preprocessing the string) would have the advantage that other features like source line numbers (#64) would work correctly for free, since the parser would know where the actual content starts.

References:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions