Skip to content

Retain newlines for code blocks when the code tag has attributes #234

@coffee2code

Description

@coffee2code

The parser fails to retain the newlines found within code blocks originally expressed using code fences that explicitly specify a language.

Per Core's inline documentation standard pertaining to code examples, inline code examples should be defined using an indentation of 4 spaces. And until recently, that's how they've been defined. But with WP_Theme_JSON::get_metadata_boolean() in WP 6.0 and more heavily with the introduction of the WP_HTML_Tag_Processor class and its methods in WP 6.2, some code examples have been defined using code fences (```). This is a syntax that Markdown also generally supports for denoting blocks of code.

Our Markdown importer does recognize the code fence syntax for denoting code. Bare code fences yield the same markup as through indentation, <pre><code>, so it's already fully supported. However, when a language is explicitly specified in the code fence, that language information is added to the resultant markup (e.g. <pre><code class="language-php">).

The problem is that the function fix_newlines() only performs a simple check, looking only for <pre><code> to find potential code blocks. If found, it retains the newlines that would otherwise be lost during parsing. Since it does not account for attributes to the code tag, the affected code blocks do not have their newlines preserved.

The solution is for fix_newlines() to accommodate the potential for attributes of the code tag.

Fix incoming.

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