Skip to content

Code snippets: Support "Outputs:" comments in runnable snippets - #297

Merged
sirreal merged 11 commits into
masterfrom
codex/inline-output-json
Sep 4, 2026
Merged

Code snippets: Support "Outputs:" comments in runnable snippets#297
sirreal merged 11 commits into
masterfrom
codex/inline-output-json

Conversation

@adamziel

@adamziel adamziel commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Core PR WordPress/wordpress-develop#13267 replaces a separate expected-output fence with a trailing output comment.

This PR adds support for the // Outputs: syntax to the runnable code snippets. That is in addition to the already supported expected-output syntax.

Output formats

Literal one-line output

echo esc_html( "<egg>" );
// Outputs: <egg>

Everything after // Outputs: is literal output. Quotes and other punctuation have no special meaning.

{
  "code": "echo esc_html( \"<egg>\" );",
  "expected_output": "<egg>"
}

Literal multiline output

print_r( array( "fruit" => "apple" ) );
// Outputs:
// Array
// (
//     [fruit] => apple
// )
//

An empty // Outputs: starts a multiline block. Each following // is one output line. One space after // is the comment delimiter; further indentation is output.

Empty final comments preserve final newlines. Quotes remain literal:

// Outputs:
// first
// "second "
//

This exports "first\n\"second \"\n".

JSON-encoded output

echo "done ";
// Outputs (JSON-encoded): "done "

// Outputs (JSON-encoded): requires one JSON string. Use it when otherwise invisible trailing whitespace, tabs, escaped quotes, or several encoded newlines must be explicit:

// Outputs (JSON-encoded): "first\nsecond \n\n"

The format is selected by the header, never by the output text. Unicode can remain literal in the DocBlock, including // Outputs (JSON-encoded): "✅ Complete ".

The expected-output fence remains supported for compatibility. It is still attached to the preceding interactive PHP fence and removed from rendered documentation. New examples should use one of the trailing comment forms above. The exported JSON shape stays the same, so consumers continue to receive expected_output as a string.

Testing

The expanded unit tests cover literal one-line and multiline output, literal quotes, indentation, trailing spaces, one and several final newlines, Unicode, valid JSON escapes, malformed JSON, non-string JSON values, non-final comments, strings, heredocs, block comments, same-line comments, text after a PHP closing tag, expected-output fence compatibility, output conflicts, and unattached metadata errors.

@github-actions

github-actions Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Corpus diff

0 hunks. No behavior change over WordPress 7.1, parser at 0926d54 (base) vs 5c2a1f6 (PR head, merged into base).

@adamziel
adamziel requested a review from sirreal August 31, 2026 16:08
@adamziel
adamziel requested a review from dmsnell August 31, 2026 17:08
@sirreal

sirreal commented Sep 2, 2026

Copy link
Copy Markdown
Member

The old expected-output fence form is removed.

The two examples that shipped in WordPress 7.1 use this. I suspect it's OK to remove, but those examples will lose the expected output.

For 7.2, I'd expect to use whatever updated form is expected everywhere.

@adamziel adamziel changed the title Export trailing Outputs comments as snippet metadata Code snippets: Support "Outputs:" comments as snippet metadata Sep 4, 2026
@sirreal
sirreal merged commit 6c7584f into master Sep 4, 2026
6 checks passed
@adamziel adamziel changed the title Code snippets: Support "Outputs:" comments as snippet metadata Code snippets: Support "Outputs:" comments in runnable snippets Sep 4, 2026
@sirreal
sirreal deleted the codex/inline-output-json branch September 4, 2026 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants