Skip to content

Instantly share code, notes, and snippets.

@xiaocuixt
Created July 6, 2021 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaocuixt/01712f4c2d8c3f758225b7c4b7585b64 to your computer and use it in GitHub Desktop.
Save xiaocuixt/01712f4c2d8c3f758225b7c4b7585b64 to your computer and use it in GitHub Desktop.
format json in textarea
// preview with hilight.js and pretty json format
$('[name="json_data"]').on('input change', function () {
let previewWrapper = $(this).siblings('.preview-wrapper');
const $code = previewWrapper.find('pre > code');
if ($(this).val()) {
try {
var val = JSON.parse($(this).val())
} catch (e) {
$(this).addClass("warning-bordered");
return false;
}
$(this).removeClass("warning-bordered");
previewWrapper.removeClass("hide");
$code.text(JSON.stringify(val, undefined, 2))
hljs.highlightBlock($code[0]);
} else {
previewWrapper.addClass("hide");
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment