Skip to content

Markdown preview may return error when the field is empty #4845

@TimeZHero

Description

@TimeZHero
  • Laravel Version: 9.31.0
  • Nova Version: 4.15.1
  • PHP Version: 8.1
  • Database Driver & Version: MySQL 8
  • Operating System and Version: Ubuntu 22.04
  • Browser type and version: Chrome, latest
  • Reproduction Repository: None

Description:

By clicking "Preview" on Markdown field (currently using default driver) when the field is empty, I receive an error stating that the value is expected to be string rather than null:
image

As the error states, the issue happens in Laravel\Nova\Fields\Markdown\DefaultPreset:15

<?php

namespace Laravel\Nova\Fields\Markdown;

use Illuminate\Support\Str;

class DefaultPreset implements MarkdownPreset
{
    /**
     * Convert the given content from markdown to HTML.
     *
     * @param  string  $content
     * @return string
     */
    public function convert(string $content) // if we change it to ?string it would just break later on
    {
        return Str::markdown($content);
    }
}

Obviously enough, this issue is shared among all the drivers, hence the optimal fix should be placed in Laravel\Nova\Fields\Markdown:105

/**
 * Return a preview for the given field value.
 *
 * @param  string  $value
 * @return string
 */
public function previewFor($value)
{
    // could add __( ) for allowing translations, default it to empty string or expose a method to set the default value
    return $this->renderer()->convert($value ?? 'Nothing to preview');
}

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Add a Markdown field inside any resource
  2. No specific driver is required
  3. Go to edit view and try to Preview the markdown while it is empty

Metadata

Metadata

Assignees

Labels

bugVerified bug by the Nova team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions