Docs LATEST

ContentParser

API reference for VelvetCMS\Services\ContentParser.

Services

Namespace: VelvetCMS\Services\ContentParser


ContentParser is the service that turns raw page content into parsed frontmatter, normalized body text, and renderable HTML.

For authoring syntax and page format details, see Markdown & Frontmatter and VLT Format.

Definition #

class ContentParser
{
    public function __construct(CacheDriver $cache, ParserInterface $parser);

    public function parse(string $content, string $format = 'auto'): array;
    public function markdown(string $content, bool $useCache = true): string;
    public function extractFrontmatter(string $content): array;
}

Methods #

parse() #

Parse a full document.

public function parse(string $content, string $format = 'auto'): array

Returns an array with:

  • frontmatter
  • html
  • body

If format is markdown, the body is rendered as plain Markdown. Any other value uses the block parser used by .vlt content.

markdown() #

Render Markdown through the configured parser.

public function markdown(string $content, bool $useCache = true): string

The cache TTL comes from content.parser.cache_ttl.

extractFrontmatter() #

Extract YAML frontmatter without rendering the body.

public function extractFrontmatter(string $content): array

The return shape is:

[
    'frontmatter' => [...],
    'body' => '...'
]

Supported block directives #

The internal block parser recognizes:

  • @markdown
  • @md
  • @html
  • @text
  • @component('name') ... @endcomponent

@component() blocks are converted into @include() directives for the view layer.

Example #

$parser = app(ContentParser::class);

$parsed = $parser->parse($rawDocument);

$meta = $parsed['frontmatter'];
$html = $parsed['html'];