Skip to content

Add FrontmatterExtension for parsing document metadata#122

Merged
dereuromark merged 3 commits intomasterfrom
feature/frontmatter-extension
Mar 24, 2026
Merged

Add FrontmatterExtension for parsing document metadata#122
dereuromark merged 3 commits intomasterfrom
feature/frontmatter-extension

Conversation

@dereuromark
Copy link
Copy Markdown
Contributor

@dereuromark dereuromark commented Mar 24, 2026

Summary

  • Adds FrontmatterExtension for parsing YAML/TOML/JSON frontmatter at document start
  • Uses ---format syntax (e.g., ---yaml, ---toml) to distinguish from thematic breaks
  • Implemented as a pure extension with no core changes needed
  • Supports optional djot attributes: ---yaml {.meta key="value"}

Based on discussion in jgm/djot#35 and the tree-sitter-djot grammar approach.

Usage

$ext = new FrontmatterExtension();
$converter = new DjotConverter();
$converter->addExtension($ext);

$html = $converter->convert($djot);

// Access frontmatter after parsing
if ($ext->hasFrontmatter()) {
    echo $ext->getFormat();  // 'yaml'
    echo $ext->getContent(); // Raw YAML string
}

// Parse with your preferred library
$metadata = $ext->getParsedContent(fn($content, $format) => 
    $format === 'yaml' ? Yaml::parse($content) : null
);

Features

  • Format-agnostic: raw content accessible for parsing with any library
  • Configurable rendering: hidden (default), HTML comment, or custom callback
  • Attribute support: ---python {kernel="myproject" #cell-1}
  • getParsedContent() helper for easy integration

Out of scope

  • This extension would not allow placing the blocks anywhere but the top.
  • It would by design then only allow a single one

Use cases for multiple blocks:

  • Jupyter-style notebooks: ---python {kernel="myproject" #cell-1}
  • Polyglot documents: mix of ---python, ---sql, ---bash
  • Section metadata: ---yaml for each section's config

But that could be its own custom extension if needed.

Implements frontmatter support as a pure extension without core changes.
Uses ---format syntax (e.g., ---yaml, ---toml, ---json) to distinguish
from thematic breaks (---).

Features:
- Parses frontmatter at document start only
- Supports optional djot attributes: ---yaml {.meta key="value"}
- Format-agnostic: raw content accessible for parsing with any library
- Configurable rendering: hidden (default), HTML comment, or custom callback
- getParsedContent() helper for easy integration with yaml/toml/json parsers

Based on discussion in jgm/djot#35 and tree-sitter-djot grammar approach.
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 24, 2026

Codecov Report

❌ Patch coverage is 93.61702% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.92%. Comparing base (9c563ba) to head (ad7c3c0).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/Extension/Frontmatter.php 63.63% 4 Missing ⚠️
src/Extension/FrontmatterExtension.php 97.59% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #122      +/-   ##
============================================
+ Coverage     93.83%   93.92%   +0.08%     
- Complexity     2369     2382      +13     
============================================
  Files            79       81       +2     
  Lines          6261     6303      +42     
============================================
+ Hits           5875     5920      +45     
+ Misses          386      383       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dereuromark dereuromark mentioned this pull request Mar 24, 2026
@dereuromark dereuromark added the enhancement New feature or request label Mar 24, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Djot extension to recognize and expose document-start frontmatter blocks (YAML/TOML/JSON/etc.) using a ---format opener to avoid clashing with thematic breaks, plus documentation and PHPUnit coverage.

Changes:

  • Introduces FrontmatterExtension to parse ---format ... --- blocks at document start and expose raw content/format/attributes.
  • Adds a Frontmatter block node type and render controls (hidden by default, HTML comment, or custom callback).
  • Adds user-facing docs and a comprehensive test suite for the new extension behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
src/Extension/FrontmatterExtension.php Implements the frontmatter block pattern, state storage, and render-time output handling.
src/Extension/Frontmatter.php Adds a new block node type to represent parsed frontmatter in the AST.
tests/TestCase/Extension/FrontmatterExtensionTest.php Adds PHPUnit tests for parsing, rendering options, attributes, and helper methods.
docs/extensions/index.md Documents FrontmatterExtension usage, options, and syntax.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Extension/FrontmatterExtension.php
Comment thread src/Extension/Frontmatter.php
Comment thread docs/extensions/index.md
Comment thread docs/extensions/index.md
Comment thread src/Extension/FrontmatterExtension.php
Comment thread src/Extension/FrontmatterExtension.php
- Auto-clear state between convert() calls (no need for manual reset())
- Make format non-nullable (always provided in ---format syntax)
- Return null for unclosed frontmatter (missing closing ---)
- Add missing use statements in documentation examples
- Document that attributes must be inline (not on preceding line)
- Add tests for unclosed frontmatter and extension reuse scenarios
@dereuromark dereuromark merged commit 36dbbcb into master Mar 24, 2026
5 of 6 checks passed
@dereuromark dereuromark deleted the feature/frontmatter-extension branch March 24, 2026 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants