Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit 17dd080

Browse files
author
Mateu Aguiló Bosch
committed
fix: pretty print the JSON in the node view
1 parent eb20e07 commit 17dd080

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

modules/contenta_enhancements/contenta_enhancements.libraries.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ contenta_enhancements:
2323
css:
2424
component:
2525
css/contenta_enhancements.css: {}
26+
27+
contenta_enhancements.node-preview:
28+
version: '1.0'
29+
js:
30+
https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=sunburst: { type: external, minified: true }

modules/contenta_enhancements/contenta_enhancements.module

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,12 @@ function contenta_enhancements_node_view_alter(array &$build, NodeInterface $ent
376376
$normalized = $entity_to_jsonapi->normalize($entity);
377377

378378
// Generate markup.
379-
$json = json_encode($normalized, JSON_PRETTY_PRINT);
380-
$markup = <<<HTML
381-
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
382-
<pre class="prettyprint"><code class="language-js">$json</code></pre>
383-
HTML;
379+
$flags = JSON_HEX_TAG
380+
| JSON_HEX_APOS
381+
| JSON_HEX_AMP
382+
| JSON_HEX_QUOT
383+
| JSON_PRETTY_PRINT;
384+
$json = json_encode($normalized, $flags);
384385

385386
// Add to render array.
386387
$build['jsonapi_prefix'] = [
@@ -389,7 +390,14 @@ HTML;
389390
];
390391
$build['jsonapi'] = [
391392
'#weight' => 100000,
392-
'#prefix' => '</div><div class="col s12 m8"><div class="node-jsonapi-preview">' . Markup::create($markup) . '</div></div></div>',
393+
'#prefix' => '</div><div class="col s12 m8"><div class="node-jsonapi-preview"><pre class="prettyprint"><code class="language-js">',
394+
'#suffix' => '</code></pre></div></div></div>',
395+
'#markup' => $json,
396+
'#attached' => [
397+
'library' => [
398+
'contenta_enhancements/contenta_enhancements.node-preview',
399+
],
400+
],
393401
];
394402
}
395403

0 commit comments

Comments
 (0)